mo-formatter-icu-i18n-translator
gettext MO files for eugene-erg/icu-i18n-translator: the compiled catalogs gettext, PHP's gettext extension and WordPress load. Plural forms, contexts and printf placeholders are handled as in PO files, whose formatter this one builds on.
composer require eugene-erg/mo-formatter-icu-i18n-translatoruse EugeneErg\MoFormatterIcuI18nTranslator\MoFormatter;
use EugeneErg\PoFormatterIcuI18nTranslator\PoFormatter;
$translator = new Translator(/* … */, formatters: ['po' => new PoFormatter(), 'mo' => new MoFormatter()]);
$translator->addFile('po', 'shop', file_get_contents('languages/shop.pot'), 'en');
file_put_contents('languages/shop-de_DE.po', $translator->getFile('po', 'shop', 'de_DE'));
file_put_contents('languages/shop-de_DE.mo', $translator->getFile('mo', 'shop', 'de_DE'));Keep the PO file as the one that is edited
A MO file is what a PO file holds without its comments and flags. It cannot say which translations wait for a person (fuzzy in PO), so:
- Written, machine translations are included by default, so a site shows them at once; pass
new MoFormatter(machine: false)to write only the checked ones, asmsgfmtdoes. - Read, every translation is taken as a person's.
So edit the PO file and make the MO file from it. With eugene-erg/sync-icu-i18n-translator the MO file is a derived file: written anew after its PO file, never read back.
new FilePattern('languages/{domain}-{locale}.po', 'po', '{domain}', 'languages/{domain}.pot', [
new DerivedFile('languages/{domain}-{locale}.mo', 'mo'),
]);The file
- Written little-endian, the messages sorted as gettext looks them up, without the optional hash table (gettext and WordPress do without it); read in either byte order.
- The header is the entry of the empty text:
Language,Plural-FormsandContent-Typeas the PO formatter writes them. - A context is joined to its text by EOT (
\x04), a plural text and the plural forms by NUL, as gettext does. - MO keeps no
php-formatflag: a text read back is printf when it has a printf conversion (%s,%1$d…).
Not a MO file — too short, no magic number, a table or a string beyond its end — throws MoException (TranslatorExceptionInterface).
Testing
composer install
composer check # pint --test, phpstan (level max, strict and deprecation rules), phpunitLicense
MIT
From the README.md of EugeneErg/mo-formatter-icu-i18n-translator · composer require eugene-erg/mo-formatter-icu-i18n-translator