eugene-erg / sync-icu-i18n-translator
Keeps a folder of translation files in step with its source language through eugene-erg/icu-i18n-translator: any layout, any format, what a person wrote is kept
Package info
github.com/EugeneErg/sync-icu-i18n-translator
pkg:composer/eugene-erg/sync-icu-i18n-translator
Requires
- php: ^8.2
- eugene-erg/icu-i18n-translator: ^4.4
Requires (Dev)
- eugene-erg/json-formatter-icu-i18n-translator: ^1.2
- eugene-erg/po-formatter-icu-i18n-translator: ^1.2
- eugene-erg/yaml-formatter-icu-i18n-translator: ^1.1
- laravel/pint: ^1.32
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.2
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.1
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 13:28:34 UTC
README
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.
composer require eugene-erg/sync-icu-i18n-translator
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:
| Placeholder | Matches |
|---|---|
{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}:
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:
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:
- the file is given to the translator (
addFile()): its messages are what the translator keeps for the name; - 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()withImportMode::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.
- no file yet: it is written from the translator (
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
composer check # pint --test, phpstan (level max, strict rules) and phpunit
composer coverage
License
MIT