wordpress-icu-i18n-translator
A WordPress plugin for eugene-erg/icu-i18n-translator: wp icu-i18n sync translates the .pot of a plugin or theme into .po, .mo and .l10n.php files with machine translation, and keeps what people wrote. The translations it has seen live in the site's database, so a text translated once is not sent to a provider again.
wp i18n make-pot wp-content/plugins/shop wp-content/plugins/shop/languages/shop.pot
wp icu-i18n sync wp-content/plugins/shop --locale=de_DE,fr_FRcreated languages/shop-de_DE.po (12 machine)
created languages/shop-de_DE.mo
created languages/shop-de_DE.l10n.php
…
Success: 6 file(s) written; 24 text(s) translated by a provider: review them (--show-machine).Installing
Without Composer: download wordpress-icu-i18n-translator.zip from the latest release and upload it in Plugins → Add New → Upload Plugin, or wp plugin install wordpress-icu-i18n-translator.zip --activate. It carries its libraries and the seven providers below. The libraries of others (Doctrine, PSR, Nyholm) are renamed inside it, so another plugin's copies, of another version, never meet these; the translator's own classes keep their names.
The zip updates itself as WordPress.org plugins do: a new release appears under Dashboard → Updates, and automatic updates work. It comes from the releases of this repository, and is installed only when its SHA-256 is the digest GitHub published for it.
With Composer — composer/installers puts it into wp-content/plugins:
composer require eugene-erg/wordpress-icu-i18n-translator
wp plugin activate wordpress-icu-i18n-translatorActivation makes the plugin's tables. Loaded without activation (as a must-use plugin), or after an update, make or update them with wp icu-i18n install.
Needs PHP 8.2+ with intl and mysqli, WordPress 6.5+ and its usual database, MySQL or MariaDB: the plugin connects to it as WordPress does, from DB_HOST, DB_USER, DB_PASSWORD, DB_NAME and MYSQL_CLIENT_FLAGS of wp-config.php. A database drop-in for another database (SQLite) is not supported.
On a page load the plugin does nothing: WordPress loads the .mo and .l10n.php files the command writes.
wp icu-i18n sync <path>
| Option | |
|---|---|
--locale=de_DE,fr_FR | the languages to write; by default the languages installed on the site |
--source=en_US | the language of the .pot (default en_US) |
--dry-run | list the files that would be written; translate nothing |
--show-machine | list the texts a provider translated |
By default <path>/languages/{domain}.pot gives languages/{domain}-{locale}.po, and from each .po its .mo and .l10n.php are written anew.
- A text a person translated in a
.pofile stays as it is. - A new text is translated by the providers and marked
fuzzy: open the.poin Poedit or Loco, check it, and remove the mark. Until then the.moand.l10n.phpcarry the machine translation, so the site shows it at once. - Run the command again after
make-pot: new texts are translated, the rest is kept.
Providers
Settings → ICU i18n Translator (Network Admin → Settings when the plugin is active for a network: one set for every site). For each provider: its keys, whether it is used and in what order, and Save and try — it translates "Hello" into German with what was saved and says what came back. Keys are kept encrypted with the salts of wp-config.php, so a dump of the database does not give them away, and are never shown again; new salts make them unreadable, and the page asks for them again.
| Provider | Settings |
|---|---|
| Claude | ICU_I18N_CLAUDE_KEY, ICU_I18N_CLAUDE_MODEL (claude-sonnet-5-5 by default) |
| An OpenAI-compatible model — OpenAI, OpenRouter, Mistral, Ollama, LM Studio… | ICU_I18N_LLM_URL, ICU_I18N_LLM_MODEL, ICU_I18N_LLM_KEY (not for a local server) |
| DeepL | ICU_I18N_DEEPL_KEY (a key ending in :fx goes to the free API) |
| Google Cloud Translation | ICU_I18N_GOOGLE_KEY |
| Azure Translator | ICU_I18N_AZURE_KEY, ICU_I18N_AZURE_REGION |
| Amazon Translate | ICU_I18N_AMAZON_ACCESS_KEY, ICU_I18N_AMAZON_SECRET_KEY, ICU_I18N_AMAZON_REGION |
| LibreTranslate | ICU_I18N_LIBRETRANSLATE_URL, ICU_I18N_LIBRETRANSLATE_KEY |
The same names are constants of wp-config.php, which win over the page and lock its fields — keys out of the database, different per environment:
define('ICU_I18N_DEEPL_KEY', '…');
define('ICU_I18N_PROVIDERS', 'llm,deepl'); // which ones, in this orderChosen neither way, every provider set up is used: the language models first — they translate whole messages — then the services, in the order of the table. Requests go through the HTTP API of WordPress (the site's proxy and certificates); the providers' language lists are kept a day in transients. Installed with Composer, a provider also needs its package (eugene-erg/deepl-icu-i18n-translator…); the page and the command say which is missing.
Without a provider, sync works only while every text has a translation already (in the .po files or the database), and stops at the first new one.
In code
The icu_i18n_translator_providers filter gets the providers of the settings and can reorder them or add any other — a model through a command-line tool, Google's Advanced edition with a service account, a provider of yours. Its second argument gives a PSR-18 client over the HTTP API of WordPress, the PSR-17 factories and a PSR-16 cache in transients:
use EugeneErg\DeepLIcuI18nTranslator\Client\DeepLClient;
use EugeneErg\DeepLIcuI18nTranslator\DeepLTranslator;
use EugeneErg\DeepLIcuI18nTranslator\Formality;
use EugeneErg\HttpProviderIcuI18nTranslator\Http\HttpJsonClient;
use EugeneErg\WordPressIcuI18nTranslator\Services;
// DeepL with options the constants do not have
add_filter('icu_i18n_translator_providers', static function (array $providers, Services $services): array {
$http = new HttpJsonClient($services->http(), $services->factory(), $services->factory());
return [new DeepLTranslator(new DeepLClient($http, DEEPL_KEY), Formality::PreferLess, cache: $services->cache())];
}, 10, 2);In the zip the PSR interfaces are renamed with the rest, so a provider that is not in it comes with Composer.
Other files
| Filter | Default | |
|---|---|---|
icu_i18n_translator_formatters | po, mo, l10n.php | the formats by name, FormatterInterface |
icu_i18n_translator_patterns | languages/{domain}-{locale}.po from languages/{domain}.pot, with its .mo and .l10n.php | FilePatterns of sync-icu-i18n-translator; gets the path of the plugin or theme |
use EugeneErg\SyncIcuI18nTranslator\DerivedFile;
use EugeneErg\SyncIcuI18nTranslator\FilePattern;
// a theme that keeps its translations in lang/, without .l10n.php files
add_filter('icu_i18n_translator_patterns', static fn (): array => [
new FilePattern('lang/{locale}.po', 'po', 'theme', 'lang/theme.pot', [new DerivedFile('lang/{locale}.mo', 'mo')]),
]);A filter that gives something else stops the command with what is wrong.
The database
Four tables next to the site's, under its prefix: wp_icu_i18n_groups, _translates, _group_translates, _paths (wp_2_icu_i18n_… on the second site of a network), in the charset and collation of WordPress. Activated for a network, the plugin makes them on every site, and on each site made later. Deleting the plugin drops them, and its settings, on every site; the .po files keep their translations.
Testing
The tests run in WordPress itself, with its test suite, on a real MySQL or MariaDB — the suite empties that database, so give it one of its own:
composer install
mysql -e "CREATE DATABASE wordpress_tests; CREATE USER wp@localhost IDENTIFIED BY 'wp'; GRANT ALL ON wordpress_tests.* TO wp@localhost"
composer check # pint --test, phpstan (level max), phpunit on a single site
WP_MULTISITE=1 vendor/bin/phpunit # on a networkThe zip — built for PHP 8.2, and tried on a WordPress of its own beside a plugin that brings its own Doctrine and Nyholm, installed, activated, used and deleted:
composer install --working-dir=tools/scoper
bin/build-zip.sh 1.1.0 # dist/wordpress-icu-i18n-translator.zip
bin/smoke-zip.sh 1.1.0 # on the database of the tests, under the prefix zip_WP_TESTS_DB_HOST, WP_TESTS_DB_NAME, WP_TESTS_DB_USER, WP_TESTS_DB_PASSWORD and WP_CORE_DIR change the defaults (127.0.0.1, wordpress_tests, wp, wp, vendor/roots/wordpress-no-content). php bin/coverage.php joins the coverage of both runs (--coverage-php).
License
MIT
From the README.md of EugeneErg/wordpress-icu-i18n-translator · composer require eugene-erg/wordpress-icu-i18n-translator