deepl-icu-i18n-translator
DeepL as a translation provider of eugene-erg/icu-i18n-translator.
- Placeholders survive. The arguments of a message (and the tags of markup) travel as XML tags DeepL is told to leave alone; DeepL puts them where the target language needs them. The translator checks every text that comes back, so a text whose placeholders were lost fails alone and goes to the next provider.
- Context. The context of a message (PO
msgctxtand the like) goes to DeepL'scontext: it steers the translation and is not billed. - Batches. The texts of a file go in as few requests as DeepL takes (50 texts, about 100 KiB each). Texts without words (
{count} %,—) are not sent. - Languages from DeepL. Which directions it translates is asked from DeepL, not hard-coded, and the closest variant is picked:
en_GB→EN-GB,pt_BR→PT-BR,zh_TW→ZH-HANT,zh→ZH-HANS,en→EN-US. - Formality and the model type, temporary errors asked again, the free and the paid API.
Installation
composer require eugene-erg/deepl-icu-i18n-translatorIt speaks HTTP through PSR-18: bring any client, for example symfony/http-client or guzzlehttp/guzzle, and PSR-17 factories such as nyholm/psr7.
Usage
use EugeneErg\DeepLIcuI18nTranslator\Client\DeepLClient;
use EugeneErg\DeepLIcuI18nTranslator\DeepLTranslator;
use EugeneErg\DeepLIcuI18nTranslator\Formality;
use EugeneErg\HttpProviderIcuI18nTranslator\Http\HttpJsonClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;
$factory = new Psr17Factory();
$deepl = new DeepLTranslator(
client: new DeepLClient(new HttpJsonClient(new Psr18Client(), $factory, $factory), getenv('DEEPL_AUTH_KEY')),
formality: Formality::PreferLess, // optional
cache: $psr16Cache, // optional: the language lists, a day by default
);
// one of the providers of the translator, tried in order
$translator = new Translator(..., translators: [$deepl], formatters: [...]);A key of the free plan (it ends in :fx) goes to api-free.deepl.com, any other to api.deepl.com; the third argument of DeepLClient sets another base URL (a proxy, a test server).
Options
| Argument | Default | |
|---|---|---|
formality | Formality::Default | More/Less are sent only to the languages that have formality, so they never fail on the others; PreferMore/PreferLess are sent everywhere |
modelType | DeepL's choice | ModelType::QualityOptimized, PreferQualityOptimized, LatencyOptimized |
cache | none | a PSR-16 cache for the language lists; without one they are asked once per object |
cacheTtl | 86400 | seconds |
HttpJsonClient and its RetryPolicy come from eugene-erg/http-provider-icu-i18n-translator, shared by the HTTP providers: by default three attempts on 408, 429, 5xx and throttling errors, waits of 1 s, 2 s … or the server's Retry-After, at most 30 s. RetryPolicy::none() turns it off.
In Laravel and Symfony
$this->app->singleton(TranslatorInterface::class . '[]', fn () => [
app(DeepLTranslator::class), // bound as above
]);services:
EugeneErg\DeepLIcuI18nTranslator\Client\DeepLClient:
arguments: { $authKey: '%env(DEEPL_AUTH_KEY)%' }
EugeneErg\DeepLIcuI18nTranslator\DeepLTranslator:
tags: [icu_i18n.provider]Language models translate whole ICU messages, plural forms agreed with each other; DeepL translates the texts of the forms one by one. Put a model (llm, claude) first and DeepL after it, or DeepL alone.
Errors
Everything thrown about the service implements ProviderExceptionInterface (namespace EugeneErg\HttpProviderIcuI18nTranslator\Exceptions):
TransportException: no connection, or an HTTP error after the retries; its code is the status.456means the character quota of the plan is used up, and is not asked again.InvalidResponseException: an answer this package does not understand.
A direction DeepL does not translate throws the translator's UnexpectedTranslateDirectionException; canTranslate() tells it beforehand. The translator moves on to the next provider in every case.
Upgrading to 2.x
The HTTP client, the retries, the exceptions and the placeholders moved to eugene-erg/http-provider-icu-i18n-translator:
DeepLIcuI18nTranslator\Http\HttpJsonClientandRetryPolicyareEugeneErg\HttpProviderIcuI18nTranslator\Http\…;DeepLIcuI18nTranslator\Exceptions\…areEugeneErg\HttpProviderIcuI18nTranslator\Exceptions\…, andDeepLExceptionInterfaceisProviderExceptionInterface;TransportExceptionkeeps the type of the error the service named in$errorType.
The behaviour is the same, but for simplified Chinese: the target is ZH-HANS, DeepL's explicit code, where it was the plain ZH.
Testing
composer install
composer check # pint --test, phpstan (level max, strict and deprecation rules), phpunitThe tests answer from a fake PSR-18 client; nothing is sent to DeepL.
License
MIT
From the README.md of EugeneErg/deepl-icu-i18n-translator · composer require eugene-erg/deepl-icu-i18n-translator