eugene-erg / yii-icu-i18n-translator
Yii 3 integration for eugene-erg/icu-i18n-translator: the translator in the container, its memory in your database, console commands to install the tables and translate messages/
Package info
github.com/EugeneErg/yii-icu-i18n-translator
pkg:composer/eugene-erg/yii-icu-i18n-translator
Requires
- php: ^8.2
- eugene-erg/dbal-icu-i18n-translator: ^1.0
- eugene-erg/icu-i18n-translator: ^4.4
- eugene-erg/php-array-formatter-icu-i18n-translator: ^1.2
- eugene-erg/sync-icu-i18n-translator: ^1.0
- psr/container: ^2.0
- symfony/console: ^6.4 || ^7.0 || ^8.0
- yiisoft/aliases: ^3.0
- yiisoft/definitions: ^3.0
Requires (Dev)
- 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
- yiisoft/di: ^1.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 08:13:29 UTC
README
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:install
yiisoft/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 translated
created 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 database
DbalStorage (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), phpunit
The tests build a yiisoft/di container from the package's own params.php and di.php.
License
MIT