codeigniter-icu-i18n-translator
eugene-erg/icu-i18n-translator in CodeIgniter 4:
php spark icu-i18n:synctranslatesapp/Languageinto every language of the app. A file someone already wrote is written over itself: what a person changed is kept, and only what is missing is translated.service('icuI18nTranslator')translates ICU messages and text at run time, remembering every translation in your database so nothing is sent to a provider twice.php spark icu-i18n:installcreates the translator's tables in the database CodeIgniter already uses.
CodeIgniter's language files hold ICU messages already ({name}, {count, plural, …}), so lang() reads the written files as they are.
Installation
composer require eugene-erg/codeigniter-icu-i18n-translator
php spark icu-i18n:installCodeIgniter finds the commands and services of the package by itself (Config\Modules::$discoverInComposer, on by default). The tables go in the default database group through Doctrine DBAL, so the PDO extension of your database must be installed: pdo_mysql, pdo_pgsql, pdo_sqlite or pdo_sqlsrv. To write them into a migration of your own instead, print the statements with php spark icu-i18n:install --dump-sql.
Configuration
Create app/Config/IcuI18n.php extending the package's config; CodeIgniter takes it first:
<?php
namespace Config;
use EugeneErg\CodeIgniterIcuI18nTranslator\Config\IcuI18n as BaseIcuI18n;
use EugeneErg\DeepLIcuI18nTranslator\DeepLTranslator;
class IcuI18n extends BaseIcuI18n
{
public string $dbGroup = 'default'; // or a group of its own
public ?string $url = null; // a Doctrine DBAL URL instead of the group
public string $tablePrefix = 'icu_i18n_';
public ?string $source = null; // null: App::$defaultLocale
public array $locales = []; // empty: App::$supportedLocales without the source
public function providers(): array
{
return [
// tried in order: language models first, text translators after them
new DeepLTranslator(/* see its README */),
];
}
}There is no provider by default: a text nobody translated stays as it is. Providers: llm, claude, deepl, google-cloud.
formatters() gives the file formats ('php', CodeIgniter's language files, by default) and patterns() where the files are: app/Language/{locale}/…/*.php by default. Add a format and a pattern to translate other files, for example XLIFF for translators:
public function formatters(): array
{
return [...parent::formatters(), 'xlf' => new XliffFormatter()];
}
public function patterns(): array
{
return [...parent::patterns(), new FilePattern('Translations/{locale}/{path}.xlf', 'xlf')];
}Translating app/Language
php spark icu-i18n:sync # every language of the config
php spark icu-i18n:sync --locale de,fr # only these
php spark icu-i18n:sync --source en # the language the files are written in
php spark icu-i18n:sync --dry-run # which files would be written; translates nothing
php spark icu-i18n:sync --show-machine # list the keys a provider translatedcreated app/Language/de/Shop.php (2 machine)
updated app/Language/fr/Shop.php
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: service('icuI18nTranslator')->approveMachineTranslations($groupId, 'de').
At run time
$translator = service('icuI18nTranslator');
$translator->translateMessage('{count, plural, one {# file} other {# files}}', ['count' => 5], 'ru', 'en');
// "5 файлов": translated once, then read from the database
$translator->translateText('Your order has shipped', 'de', 'en');service('icuI18nStorage') is the storage (its connection and schema()), service('icuI18nSync') the sync the command runs.
Testing
composer install
composer check # pint --test, phpstan (level max, strict and deprecation rules), phpunitThe tests run CodeIgniter's own test bootstrap on SQLite in memory.
License
MIT
From the README.md of EugeneErg/codeigniter-icu-i18n-translator · composer require eugene-erg/codeigniter-icu-i18n-translator