PHP array formatter
PHP array translation files, as Laravel keeps them in lang/en/*.php, for eugene-erg/icu-i18n-translator.
composer require eugene-erg/php-array-formatter-icu-i18n-translatoruse EugeneErg\PhpArrayFormatterIcuI18nTranslator\PhpArrayFormatter;
$translator = new Translator(/* … */, formatters: ['php' => new PhpArrayFormatter()]);
$translator->addFile('php', 'messages', file_get_contents('lang/en/messages.php'), 'en');
file_put_contents('lang/ru/messages.php', $translator->getFile('php', 'messages', 'ru'));<?php
return [
'welcome' => 'Hello, {name}!',
'cart' => [
'items' => '{count, plural, one {# item} other {# items}}',
],
];The file is never executed
A translation file can come from anyone: a translator, an agency, an upload. Including it would run its code. So it is parsed with nikic/php-parser, and only data is accepted:
return [...]orreturn array(...), optionally afterdeclare(strict_types=1);and comments- string and integer keys, items without keys (
['Mon', 'Tue']) - string literals in single or double quotes, nowdoc, heredoc without variables, and
'a' . 'b'concatenation - nested arrays
Anything else is refused with the key and line instead of being run or skipped: a function call, a constant, a variable inside a string, a statement before return, a computed key.
Writing
The output is formatted like Laravel's own files: short arrays, 4 spaces, single quotes, trailing commas, keys in their order. PHP reads it back exactly (checked by a test). Pass strictTypes: true to add declare(strict_types=1);.
Writing over a file
updateFile() (and php artisan i18n:sync of the Laravel package) writes the texts into an existing file instead of writing it anew:
file_put_contents('lang/ru/messages.php', $translator->updateFile('php', 'messages', file_get_contents('lang/ru/messages.php'), 'ru'));- only the values that changed are replaced (a changed concatenation or heredoc becomes one literal; comments inside it go after the new literal);
- missing keys are added at the end of their array, with its indentation; in a list, without keys;
- keys the source no longer has are removed with their comma and line; comments above them stay;
- everything else — comments, order, quotes, alignment,
declare,array()— stays as it is.
The result is read back and compared with what was meant before it is returned; if they differ, nothing is written and PhpArrayFormatException says so.
Message syntax
Messages are ICU by default: {name}, {count, plural, …}. Laravel's own syntax — :name placeholders and apple|apples pluralization — needs LaravelSyntax (package eugene-erg/laravel-syntax-icu-i18n-translator):
new PhpArrayFormatter(new LaravelSyntax());It turns Laravel messages into ICU for the translator and back, so the files stay native Laravel.
Development
composer check # pint --test, phpstan (level max, strict rules) and phpunit
composer coverageLicense
MIT
From the README.md of EugeneErg/php-array-formatter-icu-i18n-translator · composer require eugene-erg/php-array-formatter-icu-i18n-translator