eugene-erg / mo-formatter-icu-i18n-translator
gettext MO files for eugene-erg/icu-i18n-translator: the binary catalogs gettext and WordPress load, read and written with plural forms and contexts
Package info
github.com/EugeneErg/mo-formatter-icu-i18n-translator
pkg:composer/eugene-erg/mo-formatter-icu-i18n-translator
Requires
- php: ^8.2
- eugene-erg/icu-i18n-translator: ^4.3
- eugene-erg/po-formatter-icu-i18n-translator: ^1.2
Requires (Dev)
- laravel/pint: ^1.32
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.2
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.1
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 10:41:00 UTC
README
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-translator
use 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), phpunit
License
MIT