azure-icu-i18n-translator
Azure AI Translator as a translation provider of eugene-erg/icu-i18n-translator.
- Placeholders survive. The arguments of a message (and the tags of markup) travel as
<span translate="no">withtextType=html, which Azure leaves untranslated and moves 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. - Batches. The texts of a file go in requests of 100 texts and 10,000 characters by default, what a container of Azure takes; the cloud takes up to 1,000 and 50,000, both are settings. Texts without words (
{count} %,—) are not sent. - Languages from Azure. Which directions it translates is asked from Azure, and the closest code is picked:
zh_Hant/zh_TW→zh-Hant,zh→zh-Hans,pt_PT→pt-pt,pt_BR→pt,sr_Latn→sr-Latn. - Regional resources, custom domains, containers and Custom Translator models; temporary errors asked again; the key never in URLs or exceptions.
Azure takes no context, so the context of a message is not sent.
The package speaks Text Translation 3.0, the generally available version. Version 2026-06-06 changes the request and the answer and adds language models; it can be added once it is the general one.
Installation
composer require eugene-erg/azure-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\AzureIcuI18nTranslator\AzureTranslator;
use EugeneErg\AzureIcuI18nTranslator\Client\AzureClient;
use EugeneErg\HttpProviderIcuI18nTranslator\Http\HttpJsonClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;
$factory = new Psr17Factory();
$azure = new AzureTranslator(
client: new AzureClient(
new HttpJsonClient(new Psr18Client(), $factory, $factory),
key: getenv('AZURE_TRANSLATOR_KEY'),
region: 'westeurope', // the region of a regional or multi-service resource; null for a global one
),
cache: $psr16Cache, // optional: the language list, a day by default
);
// one of the providers of the translator, tried in order
$translator = new Translator(..., translators: [$azure], formatters: [...]);AzureClient | Default | |
|---|---|---|
region | null | Ocp-Apim-Subscription-Region, needed by a regional or multi-service resource |
category | null | a model of Custom Translator; the general one by default |
endpoint | https://api.cognitive.microsofttranslator.com | a custom domain (https://my-resource.cognitiveservices.azure.com/translator/text/v3.0), Azure China, or a container |
AzureTranslator | Default | |
|---|---|---|
maxTexts, maxCharacters | 100, 10000 | per request |
cache, cacheTtl, cacheKey | none, 86400, azure-icu-i18n-translator.languages | the language list in a PSR-16 cache; without one it is asked once per object |
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, Symfony, CodeIgniter and Yii
Put the translator in the list of providers of the integration: Laravel (TranslatorInterface::class . '[]'), Symfony (tag icu_i18n.provider), CodeIgniter (IcuI18n::providers()), Yii (providers of the params).
Errors
Everything thrown about the service implements ProviderExceptionInterface (namespace EugeneErg\HttpProviderIcuI18nTranslator\Exceptions):
TransportException: no connection, or an HTTP error after the retries, with Azure's message; its code is the status (401for a wrong key or region,403for a used-up free tier,429for too many requests).InvalidResponseException: an answer this package does not understand.
A direction Azure 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:
AzureIcuI18nTranslator\Http\HttpJsonClientandRetryPolicyareEugeneErg\HttpProviderIcuI18nTranslator\Http\…;AzureIcuI18nTranslator\Exceptions\…areEugeneErg\HttpProviderIcuI18nTranslator\Exceptions\…, andAzureExceptionInterfaceisProviderExceptionInterface;TransportExceptionkeeps the type of the error the service named in$errorType.
The behaviour is the same.
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 Azure.
License
MIT
From the README.md of EugeneErg/azure-icu-i18n-translator · composer require eugene-erg/azure-icu-i18n-translator