eugene-erg / http-provider-icu-i18n-translator
What the HTTP translation providers of eugene-erg/icu-i18n-translator share: a JSON client with retries, placeholders as markup, the closest language code, batches, a cached language list
Package info
github.com/EugeneErg/http-provider-icu-i18n-translator
pkg:composer/eugene-erg/http-provider-icu-i18n-translator
Requires
- php: ^8.2
- ext-intl: *
- eugene-erg/icu-i18n-translator: ^4.4
- psr/http-client: ^1.0
- psr/http-factory: ^1.1
- psr/http-message: ^2.0
- psr/simple-cache: ^3.0
Requires (Dev)
- laravel/pint: ^1.32
- nyholm/psr7: ^1.8
- 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 08:44:10 UTC
README
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-translator
The 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), phpunit
License
MIT