dbal-icu-i18n-translator
Storage for eugene-erg/icu-i18n-translator on Doctrine DBAL: the translation memory in MySQL, MariaDB, PostgreSQL or SQLite, in any PHP project. The Symfony bundle uses it; Laravel has its own Eloquent storage.
composer require eugene-erg/dbal-icu-i18n-translatoruse Doctrine\DBAL\DriverManager;
use EugeneErg\DbalIcuI18nTranslator\DbalStorage;
use EugeneErg\JsonFormatterIcuI18nTranslator\JsonFormatter;
$connection = DriverManager::getConnection(['driver' => 'pdo_pgsql', 'host' => 'localhost', 'dbname' => 'app', /* … */]);
$storage = new DbalStorage($connection);
$storage->schema()->install($connection); // creates the tables, once
$translator = $storage->translator(
providers: [$claude, $google], // tried in order
formatters: ['json' => new JsonFormatter()],
);
$translator->translateMessage('{count, plural, one {# file} other {# files}}', ['count' => 5], 'ru', 'en');
// "5 файлов": translated once, then read from the databaseRequires PHP 8.2+, doctrine/dbal 4.5+.
Tables
| Table | Holds |
|---|---|
icu_i18n_groups | messages: one row per text, context and language |
icu_i18n_translates | translations: one row per text and language, shared by every message that has it |
icu_i18n_group_translates | which translation a message has for each variant and plural form, and whether a provider made it (source_id) |
icu_i18n_paths | the key trees of files: a root per file, keys under it, messages at the leaves |
Long texts — messages, translations and keys of files, which can be whole sentences — are found by their md5 hashes, so no index limit is hit. new Tables('app_i18n_') puts the tables under another prefix; pass it to DbalStorage.
Creating the tables
- By itself:
$storage->schema()->install($connection)creates the missing tables and brings existing ones up to date; other tables are not looked at.sql($connection)returns the statements without running them. - With your migrations:
$storage->schema()->tables()are DBALTableobjects andaddTo($schema)returns your schema with them, for Doctrine Migrations ordoctrine:schema:update.
On MySQL and MariaDB the tables are utf8mb4 whatever the database's default: a latin1 database would refuse or mangle every text outside it. $storage->schema(['charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci']) gives other DBAL table options; other platforms ignore them.
Deleting
Deletes clean up after themselves in code, not only through foreign keys (which SQLite enforces only when told to): a deleted translation leaves no links, and messages translated from it keep no source; a deleted message leaves its file keys without a message; a deleted file key takes its subtree. The translator itself never deletes messages: they are memory shared by every file.
Development
composer check # pint, phpstan, phpunit on SQLite
DBAL_TEST_URL=pdo-pgsql://user:password@localhost/db vendor/bin/phpunit # the same tests on a serverCI runs them on SQLite, PostgreSQL 16, MySQL 8.4 and MariaDB 11.
License
MIT
From the README.md of EugeneErg/dbal-icu-i18n-translator · composer require eugene-erg/dbal-icu-i18n-translator