Po is a set of objects to assist in reading, manipulating and creating GNU gettext style PO files.
The recommended installation method is using composer. Include "geekwright/po" in the "require" section of your project's composer.json.
"require": {
"geekwright/po": "1.0.*"
}
All Po classes are in the Geekwright\Po namespace.
Po provides the capability to create, read, and modify PO and POT files, including the ability to scan PHP sources for gettext style calls to build a POT file. You can connect the pieces however you need, but here are a few examples for common situations.
try {
$poFile = new PoFile();
$poFile->readPoFile('test.po');
// list all the messages in the file
$entries = $poFile->getEntries();
foreach($entries as $entry) {
echo $entry->getAsString(PoTokens::MESSAGE);
}
} catch (UnrecognizedInputException $e) {
// we had unrecognized lines in the file, decide what to do
} catch (FileNotReadableException $e) {
// the file couldn't be read, nothing happened
}
$pluralRule = $poFile->getHeaderEntry()->getHeader('plural-forms');
$entry = new PoEntry;
$entry->set(PoTokens::MESSAGE, 'This is a message.');
$entry->set(PoTokens::FLAG, 'fuzzy');
$poFile->addEntry($entry);
The translation for an entry can be a string, or an array of strings if the Entry is a plural form. This code fragment will assign the translation to $msgstr
appropriate for either case.
$msgid_plural = $entry->get(PoTokens::PLURAL);
if (empty($msgid_plural)) {
$msgstr = $entry->getAsString(PoTokens::TRANSLATED);
} else {
$msgstr = $entry->getAsStringArray(PoTokens::TRANSLATED);
}
try {
$poFile->writePoFile('test.po');
} catch (FileNotWriteableException $e) {
// the file couldn't be written
}
$poFile = new PoFile();
$poInit = new PoInitPHP($poFile);
foreach (glob("*.php") as $filename) {
try {
$poInit->msginitFile($filename);
} catch (FileNotReadableException $e) {
// the souce file couldn't be read, decide what to do
}
}
try {
$poFile->writePoFile('default.pot');
} catch (FileNotWriteableException $e) {
// the file couldn't be written
}
For more information, see the full Po API documentation.