Skip to content

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.

bash
composer require eugene-erg/dbal-icu-i18n-translator
php
use 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 database

Requires PHP 8.2+, doctrine/dbal 4.5+.

Tables ​

TableHolds
icu_i18n_groupsmessages: one row per text, context and language
icu_i18n_translatestranslations: one row per text and language, shared by every message that has it
icu_i18n_group_translateswhich translation a message has for each variant and plural form, and whether a provider made it (source_id)
icu_i18n_pathsthe 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 DBAL Table objects and addTo($schema) returns your schema with them, for Doctrine Migrations or doctrine: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 ​

bash
composer check                                                      # pint, phpstan, phpunit on SQLite
DBAL_TEST_URL=pdo-pgsql://user:password@localhost/db vendor/bin/phpunit   # the same tests on a server

CI 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