2017 © Pedro Peláez
 

library po-parser

Gettext *.po parser for PHP

image

degola/po-parser

Gettext *.po parser for PHP

  • Sunday, April 27, 2014
  • by Degola
  • Repository
  • 1 Watchers
  • 0 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 52 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

PoParser

Gettext *.po files parser for PHP., (*1)

This package is compliant with PSR-0, PSR-1, and PSR-2. If you notice compliance oversights, please send a patch via pull request., (*2)

Forked from https://github.com/MAXakaWIZARD/PoParser regarding ignored pull requests., (*3)

Usage

Read file content

$parser = new PoParser\Parser();
$parser->read('my-pofile.po');
$entries = $parser->getEntriesAsArrays();
// Now $entries contains every string information in your pofile

echo '

    '; foreach ($entries as $entry) { echo '
  • '. 'msgid: '.$entry['msgid'].'
    '. // Message ID 'msgstr: '.$entry['msgstr'].'
    '. // Translation 'reference: '.$entry['reference'].'
    '. // Reference 'msgctxt: ' . $entry['msgctxt'].'
    '. // Message Context 'tcomment: ' . $entry['tcomment'].'
    '. // Translator comment 'ccomment: ' . $entry['ccomment'].'
    '. // Code Comment 'obsolete?: '.(string)$entry['obsolete'].'
    '. // Is obsolete? 'fuzzy?: ' .(string)$entry['fuzzy']. // Is fuzzy? '
  • '; } echo '
';

Modify content

$parser = new PoParser\Parser();
$parser->read('my-pofile.po');
// Entries are stored in array, so you can modify them.

// Use updateEntry method to change messages you want.
$parser->updateEntry('Write your email', 'Escribe tu email');
$parser->write('my-pofile.po');

Convert po file to jsgettext content

For usage with the jsgettext library from http://jsgettext.berlios.de/., (*4)

Shell script converting

po2json.php like the perl script of the jsgettext library but without further dependency., (*5)

bin$ po2json.php messages.po messages >messages.json

Online converting with caching

It's also possible to use the library for live conversion as soon as the po file changes on the server. Gives you way more flexibility while developing. To avoid the overhead of compiling the po files again and again there is also an simple caching functionality included which uses just last-modified headers and filemtime checks., (*6)

<?php

define('DOMAIN', 'messages');
define('PO_FILE', '../app/locales/en_US.utf-8/'.DOMAIN.'.po');
define('PO_PARSER_CACHE_DIR', '/tmp/po-parser-converter-cache/');

require '../src/PoParser/Entry.php';
require '../src/PoParser/Parser.php';
require '../src/PoParser/Converter/Cache.php';
require '../src/PoParser/Converter/Driver/jsgettext.php';

header('Content-Type: application/json');

// first load caching class
$ppc = new \PoParser\Converter_Cache(
    // path to po file
    PO_FILE,
    // prefix for caching file, if you use more than one converter driver you have to differentiate here
    'jsgettext-'.DOMAIN,
    // caching path where the caching files are placed
    PO_PARSER_CACHE_DIR
);

// send http header last-modified and if client already cached the file a 304 not modified response
$ppc->setCachingHeaders();
// if client didn't cache the file
if($ppc->isRequestCached() === false) {
    // if the web server didn't cache already the converted po file
    if($ppc->isChanged()) {
        // load, convert and output json file as expected by jsgettext
        $pp = new \PoParser\Converter_Driver_jsgettext(PO_FILE, DOMAIN);
        $localisationData = $pp->getContent();
        $ppc->updateContent($localisationData);

        echo $localisationData;
        unset($localisationData);
        unset($pp);
    } else {
        echo $ppc->getContent();
    }
}
unset($ppc);

Todo

  • Improve interface to edit entries.
  • Discover what's the meaning of "#@ " line.

License

This library is released under MIT license., (*7)

The Versions

27/04 2014

dev-master

9999999-dev http://github.com/Degola/PoParser

Gettext *.po parser for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

i18n gettext l10n po

08/08 2013

1.0.1

1.0.1.0 http://github.com/MAXakaWIZARD/PoParser

Gettext parser for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

i18n gettext l10n po

04/03 2013

1.0

1.0.0.0 http://github.com/MAXakaWIZARD/PoParser

Gettext parser for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

i18n gettext l10n po