Search by

eugene-erg / php-array-formatter-icu-i18n-translator

EugeneErg

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

Statistics

Installs: 11

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.1.0 2026-12-01 18:01 UTC

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 [...] or return array(...), optionally after declare(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