eugene-erg / php-array-formatter-icu-i18n-translator
PHP array translation files (Laravel lang/*.php) for eugene-erg/icu-i18n-translator, read without executing them
Package info
github.com/EugeneErg/php-array-formatter-icu-i18n-translator
pkg:composer/eugene-erg/php-array-formatter-icu-i18n-translator
Requires
- php: ^8.2
- eugene-erg/icu-i18n-translator: ^4.0
- eugene-erg/icu-message-format-parser: ^2.0.3
- nikic/php-parser: ^5.0
Requires (Dev)
- ext-intl: *
- 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-01 18:05:37 UTC
README
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-translator
use 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);.
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 coverage
License
MIT