Skip to content

sync-icu-i18n-translator ​

Keeps a folder of translation files in step with its source language, through eugene-erg/icu-i18n-translator: every file of the source language is given to the translator, and the file of every other language is written — a new one from the translator, an existing one over itself, so what a person wrote there is kept and only what is missing is translated.

It knows nothing of frameworks: a layout is a list of path templates. The Laravel and Symfony packages are built on it.

bash
composer require eugene-erg/sync-icu-i18n-translator
php
use EugeneErg\SyncIcuI18nTranslator\CatalogSync;
use EugeneErg\SyncIcuI18nTranslator\FilePattern;

$sync = new CatalogSync($translator, __DIR__, [
    new FilePattern('lang/{locale}/{path}.php', 'php', 'lang/{path}.php'),   // Laravel
    new FilePattern('lang/{locale}.json', 'json', 'lang/*.json'),
    new FilePattern('translations/{domain}+intl-icu.{locale}.yaml', 'yaml'), // Symfony
]);

foreach ($sync->sync('en', ['de', 'fr']) as $file) {
    echo $file->path, ' ', $file->action->name, ' ', count($file->machineKeys), " machine\n";
}

Patterns ​

A template is a path relative to the folder, with placeholders:

PlaceholderMatches
{locale}the language; every template has it once
{path}any part of a path, folders included: admin/users
any other, e.g. {domain}one part of a file or folder name, without /

The second argument is the name of the formatter in the translator (formatters: ['php' => …]). The third is the name the translator keeps the file under — the same for every language, so it has every placeholder of the template but {locale}. By default it is the template with * for the locale (translations/messages+intl-icu.*.yaml). Keep it stable: the translator keeps the keys of a file under its name.

Only the folder before the first placeholder is scanned (lang/, translations/). A file that matches several patterns belongs to the first.

A source of its own ​

When the source language has a file of its own, not where the template puts the others — a gettext .pot beside the .po files — the fourth argument is its template, with the placeholders of the template but {locale}:

php
new FilePattern('languages/{domain}-{locale}.po', 'po', '{domain}', source: 'languages/{domain}.pot');

The source file is read and never written; the files of the other languages are written from it. A new file is written over it, as msginit makes a .po of a .pot: the references, notes for translators and flags of the template stay, in its layout (a format that cannot write over a file writes it anew).

Derived files ​

A file made from another one for a program to read — a gettext .mo beside its .po, WordPress's .l10n.php — is a DerivedFile of the pattern, with the placeholders of its template:

php
new FilePattern('languages/{domain}-{locale}.po', 'po', '{domain}', 'languages/{domain}.pot', [
    new DerivedFile('languages/{domain}-{locale}.mo', 'mo'),
    new DerivedFile('languages/{domain}-{locale}.l10n.php', 'l10n-php'),
]);

It is written anew from the translator after its file in every language, and never read back: a compiled format may not say which texts a person checked, so reading it would take machine texts for theirs. An edit of it is replaced on the next sync; edit its file instead.

What a sync does ​

For each file of the source language:

  1. the file is given to the translator (addFile()): its messages are what the translator keeps for the name;
  2. for each language:
    • no file yet: it is written from the translator (getFile()), missing texts translated first;
    • a file: what a person changed in it is read in first (importTranslations() with ImportMode::Changes), so it becomes theirs and is never sent to a provider; then the file is written over itself (updateFile()): comments, order and layout stay. A format that cannot write over a file is written anew, its texts kept.

Each SyncedFile says its path, its action (Created, Updated, Unchanged) and the machineKeys: keys (joined with dots) whose text a provider made and no person has approved — review them before you commit.

sync($source, $locales, dryRun: true) only says which files would be created or updated: nothing is read into the translator and nothing is written.

Errors are a SyncException naming the file or the pattern: no files of the source language, a file that cannot be read or written, a format the translator has no formatter for.

Development ​

bash
composer check    # pint --test, phpstan (level max, strict rules) and phpunit
composer coverage

License ​

MIT


From the README.md of EugeneErg/sync-icu-i18n-translator · composer require eugene-erg/sync-icu-i18n-translator