symfony-icu-i18n-translator
A Symfony bundle for eugene-erg/icu-i18n-translator: the translator as a service, its memory in your database through Doctrine DBAL, providers and formats by tags, and bin/console icu-i18n:sync, which translates translations/ into every language you configure.
composer require eugene-erg/symfony-icu-i18n-translator
bin/console icu-i18n:install # the tables, on the Doctrine connectionRequires PHP 8.2+, Symfony 6.4, 7 or 8 and DoctrineBundle. Register the bundle in config/bundles.php if Flex does not:
EugeneErg\SymfonyIcuI18nTranslator\IcuI18nTranslatorBundle::class => ['all' => true],Providers
Every service that implements TranslatorInterface (texts) or MessageTranslatorInterface (whole messages: language models) is a provider; they are tried by tag priority. For example, Claude and the free Google endpoint:
# config/services.yaml
services:
EugeneErg\LlmIcuI18nTranslator\LlmTranslator:
arguments: ['@EugeneErg\ClaudeIcuI18nTranslator\AnthropicApi'] # defined as you need it
tags: [{ name: icu_i18n.provider, priority: 10 }] # firstFormats
yaml and xlf are registered, for Symfony's ICU files (messages+intl-icu.en.yaml, ….xlf). Add others with the icu_i18n.formatter tag and a format name:
services:
EugeneErg\PoFormatterIcuI18nTranslator\PoFormatter:
tags: [{ name: icu_i18n.formatter, format: po }]or, for a formatter of your own, with the #[AsIcuFormatter('name')] attribute on its class.
Configuration
# config/packages/icu_i18n.yaml
icu_i18n:
connection: ~ # a Doctrine DBAL connection name; the default one when empty
table_prefix: icu_i18n_
source: '%kernel.default_locale%'
locales: [en, de, fr, ru] # the languages icu-i18n:sync writes
sync:
root: '%kernel.project_dir%'
patterns: # where the files are; these two are the default
- { template: 'translations/{domain}+intl-icu.{locale}.yaml', format: yaml }
- { template: 'translations/{domain}+intl-icu.{locale}.xlf', format: xlf }A pattern is a path with {locale}, {path} (folders included) and other placeholders such as {domain}; see sync-icu-i18n-translator.
Translating translations/
bin/console icu-i18n:sync # every language of icu_i18n.locales
bin/console icu-i18n:sync --locale=de --dry-run # which files would be written
bin/console icu-i18n:sync --show-machine # which keys a provider translatedA missing file is created. An existing one is read first: what a person changed there becomes theirs and is never sent to a provider again, and only missing texts are translated. XLIFF files are written over themselves (notes, states and layout stay) and mark machine translations needs-review-translation; clear the mark in your XLIFF editor to approve a text. YAML has no such mark: review its machine translations before you commit.
In code
use EugeneErg\IcuI18nTranslator\Translator;
public function __construct(private Translator $translator) {}
$this->translator->translateMessage('{count, plural, one {# file} other {# files}}', ['count' => 3], 'ru', 'en');CatalogSync (the work behind icu-i18n:sync) and DbalStorage are services too.
Tables
bin/console icu-i18n:install creates the tables or brings them up to date, touching no other table; --dump-sql prints the statements instead, for a Doctrine migration of yours.
Development
composer check # pint --test, phpstan (level max, strict rules) and phpunit
composer coverageLicense
MIT
From the README.md of EugeneErg/symfony-icu-i18n-translator · composer require eugene-erg/symfony-icu-i18n-translator