Search by

eugene-erg / mo-formatter-icu-i18n-translator

EugeneErg

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

Statistics

Installs: 111

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-12-03 09:47 UTC

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, as msgfmt does.
  • 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-Forms and Content-Type as 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-format flag: 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