Skip to content

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"> with textType=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 ​

bash
composer require eugene-erg/azure-icu-i18n-translator

It 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 ​

php
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: [...]);
AzureClientDefault
regionnullOcp-Apim-Subscription-Region, needed by a regional or multi-service resource
categorynulla model of Custom Translator; the general one by default
endpointhttps://api.cognitive.microsofttranslator.coma custom domain (https://my-resource.cognitiveservices.azure.com/translator/text/v3.0), Azure China, or a container
AzureTranslatorDefault
maxTexts, maxCharacters100, 10000per request
cache, cacheTtl, cacheKeynone, 86400, azure-icu-i18n-translator.languagesthe 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 (401 for a wrong key or region, 403 for a used-up free tier, 429 for 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\HttpJsonClient and RetryPolicy are EugeneErg\HttpProviderIcuI18nTranslator\Http\…;
  • AzureIcuI18nTranslator\Exceptions\… are EugeneErg\HttpProviderIcuI18nTranslator\Exceptions\…, and AzureExceptionInterface is ProviderExceptionInterface; TransportException keeps the type of the error the service named in $errorType.

The behaviour is the same.

Testing ​

bash
composer install
composer check      # pint --test, phpstan (level max, strict and deprecation rules), phpunit

The 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