yii-icu-i18n-translator
eugene-erg/icu-i18n-translator in Yii 3:
./yii icu-i18n:synctranslatesmessages/into the languages you name. A file someone already wrote is written over itself: what a person changed is kept, and only what is missing is translated.Translatorin the container translates ICU messages and text at run time, remembering every translation in a database so nothing is sent to a provider twice../yii icu-i18n:installcreates the translator's tables.
The files are those of yiisoft/translator-message-php (messages/{locale}/{category}.php) with ICU messages, as IntlMessageFormatter reads them, so yiisoft/translator reads the written files as they are.
Installation
composer require eugene-erg/yii-icu-i18n-translator
./yii icu-i18n:installyiisoft/config merges the package's params and di: the services go in the container and the commands in yiisoft/yii-console. Without a URL the memory is SQLite in @runtime/icu-i18n.sqlite (pdo_sqlite needed), enough to start; on a server name your database.
Configuration
In config/common/params.php:
'eugene-erg/yii-icu-i18n-translator' => [
'url' => 'pdo-mysql://app:secret@localhost/app', // Doctrine DBAL; null: SQLite in sqlitePath
'tablePrefix' => 'icu_i18n_',
'source' => null, // null: $params['yiisoft/translator']['locale']
'locales' => ['de', 'fr'], // the languages icu-i18n:sync writes
'providers' => [ // tried in order: objects or container ids
\EugeneErg\DeepLIcuI18nTranslator\DeepLTranslator::class,
],
],There is no provider by default: a text nobody translated stays as it is. Providers: llm, claude, deepl, google-cloud; a container id needs a definition of yours (its key, for example).
Other files, for example XLIFF for translators, take a formatter and a pattern:
'formatters' => ['xlf' => \EugeneErg\XliffFormatterIcuI18nTranslator\XliffFormatter::class],
'patterns' => [
['translations/{locale}/{path}.xlf', 'xlf'],
],yiisoft/config appends lists, so this pattern is added to the default one, messages/{locale}/{path}.php.
A pattern is [template, format, name] relative to root (@root); the name is what the translator keeps the file under, the same for every language. A wrong value is reported with its key when the container is built.
Translating messages/
./yii icu-i18n:sync # the locales of the params
./yii icu-i18n:sync --locale=de --locale=fr # only these
./yii icu-i18n:sync --source=en-US # the language the files are written in
./yii icu-i18n:sync --dry-run # which files would be written; translates nothing
./yii icu-i18n:sync --show-machine # list the keys a provider translatedcreated messages/de/app.php (2 machine)
1 file(s) written; 2 text(s) translated by a provider: review them (--show-machine).What a person changed in a written file becomes theirs and is never asked of a provider again. A text a provider wrote stays marked as machine until someone approves it with Translator::approveMachineTranslations().
At run time
use EugeneErg\IcuI18nTranslator\Translator;
public function __construct(private Translator $translator) {}
$this->translator->translateMessage('{count, plural, one {# file} other {# files}}', ['count' => 5], 'ru', 'en');
// "5 файлов": translated once, then read from the databaseDbalStorage (its connection and schema()) and CatalogSync are in the container too.
Testing
composer install
composer check # pint --test, phpstan (level max, strict and deprecation rules), phpunitThe tests build a yiisoft/di container from the package's own params.php and di.php.
License
MIT
From the README.md of EugeneErg/yii-icu-i18n-translator · composer require eugene-erg/yii-icu-i18n-translator