eugene-erg / l10n-php-formatter-icu-i18n-translator
WordPress .l10n.php translation files for eugene-erg/icu-i18n-translator: the PHP arrays WordPress 6.5+ loads before MO, read safely and written with plural forms and contexts
Package info
github.com/EugeneErg/l10n-php-formatter-icu-i18n-translator
pkg:composer/eugene-erg/l10n-php-formatter-icu-i18n-translator
Requires
- php: ^8.2
- ext-intl: *
- 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:39:44 UTC
README
WordPress .l10n.php files for eugene-erg/icu-i18n-translator:
the PHP arrays WordPress 6.5+ loads before a MO file, as wp i18n make-php writes them. Plural forms, contexts and
printf placeholders are handled as in PO files,
whose formatter this one builds on.
composer require eugene-erg/l10n-php-formatter-icu-i18n-translator
use EugeneErg\L10nPhpFormatterIcuI18nTranslator\L10nPhpFormatter; use EugeneErg\PoFormatterIcuI18nTranslator\PoFormatter; $translator = new Translator(/* … */, formatters: ['po' => new PoFormatter(), 'l10n.php' => new L10nPhpFormatter()]); $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.l10n.php', $translator->getFile('l10n.php', 'shop', 'de_DE'));
Keep the PO file as the one that is edited
A .l10n.php file is what a PO file holds without its comments and flags, and less:
- It cannot say which translations wait for a person (
fuzzyin PO). Written, machine translations are included by default, so a site shows them at once; passnew L10nPhpFormatter(machine: false)to write only the checked ones. Read, every translation is taken as a person's. - It keeps no plural text: WordPress looks a plural up by its singular. Read, a plural has its singular as its plural text too.
So edit the PO file and make this one from it. With eugene-erg/sync-icu-i18n-translator it 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'), new DerivedFile('languages/{domain}-{locale}.l10n.php', 'l10n.php'), ]);
The file
<?php return [ 'language' => 'de_DE', 'plural-forms' => 'nplurals=2; plural=(n != 1);', 'messages' => [ 'Hello' => 'Hallo', 'verb' . "\4" . 'Post' => 'Veröffentlichen', '%d item' => '%d Artikel' . "\0" . '%d Artikel', ], ];
- A context is joined to its text by EOT (
\x04), the plural forms by NUL, as WordPress does. - The headers are the translation's own,
languageandplural-forms, from the PO formatter; WordPress takes the plural forms from them. Other headers stay in the PO file. - The file keeps no
php-formatflag: a text read back is printf when it has a printf conversion (%s,%1$d…).
A file is read without running it. Running a translation file would run whatever code it holds, so the file is
tokenized and only <?php return …; with arrays ([] or array()), strings, whole numbers and . between strings is
taken — what var_export and WP-CLI write. Anything else — a call, a variable, a constant, code after the return —
throws L10nPhpException (TranslatorExceptionInterface), as does an array without messages or a header or message
that is not a text.
Testing
composer install
composer check # pint --test, phpstan (level max, strict and deprecation rules), phpunit
License
MIT