Skip to content

Getting started ​

Install ​

bash
composer require eugene-erg/icu-i18n-translator

PHP 8.2+ with ext-intl. Add a provider and the formats of your files.

On a framework, install its package instead: it brings the storage, the container binding and a command that translates the language files — Laravel, Symfony, CodeIgniter or Yii, WordPress.

Try it in memory ​

Testing\InMemoryTranslator is the whole translator with storage in memory and a dictionary as its last provider. It is meant for tests, and is the quickest way to see what the translator does:

php
use EugeneErg\IcuI18nTranslator\Testing\InMemoryTranslator;
use EugeneErg\JsonFormatterIcuI18nTranslator\JsonFormatter;

$memory = new InMemoryTranslator(
    formatters: ['json' => new JsonFormatter()],
    dictionary: [' item' => ' товар', ' items' => ' товаров'],
);
$translator = $memory->translator;

echo $translator->translateMessage('{count, plural, one {# item} other {# items}}', ['count' => 5], 'ru', 'en');
// 5 товаров

Wire it for real ​

The translator stores what it translates behind seven repository interfaces (groups of messages, translations, the links between them, and the key trees of files). Take them over Doctrine DBAL (dbal: MySQL, MariaDB, PostgreSQL, SQLite), implement them over your database, or take the Eloquent ones of the Laravel package — and give the translator your providers and formats:

php
use EugeneErg\IcuI18nTranslator\Translator;
use EugeneErg\ICUMessageFormatParser\Parser;

$translator = new Translator(
    readGroupRepository:           $readGroupRepo,
    writeGroupRepository:          $writeGroupRepo,
    readTranslateRepository:       $readTranslateRepo,
    writeTranslateRepository:      $writeTranslateRepo,
    writeGroupTranslateRepository: $writeGroupTranslateRepo,
    readPathRepository:            $readPathRepo,
    writePathRepository:           $writePathRepo,
    parser:                        new Parser(),
    translators:                   [$claude, $google],          // tried in order
    formatters:                    ['json' => new JsonFormatter()],
);

The core readme describes the repository contracts.

Messages from code ​

php
$translator->translateText('Hello', 'ru', 'en');                                    // Привет
$translator->translateMessage('Hello, {name}!', ['name' => 'Ivan'], 'ru', 'en');   // Привет, Ivan!

The first call asks a provider; every later one reads the stored translation. Without fromLocale the provider detects the language.

Files ​

php
// the source file: its messages become what the translator keeps for "messages"
$translator->addFile('json', 'messages', file_get_contents('lang/en.json'), 'en');

// a new file in another language: what is missing is translated first
file_put_contents('lang/ru.json', $translator->getFile('json', 'messages', 'ru'));

// or the existing file, written over: its layout, comments and key order stay
$current = file_get_contents('lang/ru.json');
file_put_contents('lang/ru.json', $translator->updateFile('json', 'messages', $current, 'ru'));

// translations made elsewhere (by a person, an agency, another tool)
$result = $translator->importTranslations('json', 'messages', file_get_contents('lang/de.json'), 'de');
$result->skipped; // key => why a text was not taken (other arguments, other markup…)

Formats that hold the source and its translations in one file (PO, XLIFF, String Catalogs) give both to addFile(); see Choosing a format.