http-provider-icu-i18n-translator
What the HTTP translation providers of eugene-erg/icu-i18n-translator share, so a fix lands in one place: DeepL, Google Cloud, Azure, Amazon, LibreTranslate. An application takes one of them; this package comes along. It is for writing a provider of your own.
composer require eugene-erg/http-provider-icu-i18n-translatorThe HTTP client
Http\HttpJsonClient sends through any PSR-18 client and decodes the JSON answer: post() (JSON), postRaw() (a body the caller signs), postForm(), get(). A provider names it after its service, so its errors say whose they are:
$this->http = $http->named('DeepL');
// TransportException: "DeepL answered HTTP 456: Quota Exceeded", code 456- Temporary errors are asked again (
Http\RetryPolicy): 408, 429, 5xx and 529, and the throttling errors Amazon answers with 400 (by the error type). Waits double each time, the server'sRetry-Afterwins, at most 30 s;RetryPolicy::none()turns retries off. - The message of an error answer is read in the shapes the services use (
Http\ErrorAnswer): Google and Azure{"error": {"message"}}, OAuth{"error", "error_description"}, LibreTranslate{"error"}, DeepL{"message"}, Amazon{"__type", "message"}. The type is kept inTransportException::$errorType. - Headers never get into exceptions: keys, tokens and signatures stay out of logs.
Placeholders
A text provider gets arguments (and markup tags) as variables; Markup\PlaceholderCodecInterface turns them into markup the service leaves untranslated, and back:
Encodes Hello, {0}! as | For | |
|---|---|---|
HtmlPlaceholders | Hello, <span translate="no">0</span>! | Google, Azure, Amazon, LibreTranslate (translate="no" in HTML) |
XmlPlaceholders | Hello, <x id="0"/>! | DeepL (tag_handling=xml, ignore_tags=x) |
The answer is read leniently: what the service did to the placeholders is checked by the translator, text by text, so one bad text of a batch fails alone and goes to the next provider. The spaces around the source are kept, line breaks survive HTML, entities are decoded. Markup\Words::in($pattern) tells whether a text has words at all; one of digits and punctuation is not worth a request.
Languages
Languages\LocaleMatcher picks the closest code a service has for an ICU locale, case aside: by script (sr_Latn → sr-Latn), by region (pt_PT → pt-PT), else the language (de_AT → de), else a fallback of the service (new LocaleMatcher(['en' => 'EN-US']) for DeepL). Chinese is traditional in Hant or in Taiwan, Hong Kong and Macau (zh-Hant, else zh-TW), simplified otherwise (zh-Hans, zh-CN, zh). LocaleMatcher::locale() turns a code back into a locale.
Cache\CachedValue asks for a value (the language list) once per object, and once per TTL with a PSR-16 cache, so the processes of a web server share it.
Batches
Batch\Chunks splits texts into requests a service takes: at most so many texts and so much size, the keys kept.
Testing
composer install
composer check # pint --test, phpstan (level max, strict and deprecation rules), phpunitLicense
MIT
From the README.md of EugeneErg/http-provider-icu-i18n-translator · composer require eugene-erg/http-provider-icu-i18n-translator