Search by

eugene-erg / amazon-icu-i18n-translator

EugeneErg

Amazon Translate as a translation provider of eugene-erg/icu-i18n-translator: placeholders kept, Signature V4 without the AWS SDK

Package info

github.com/EugeneErg/amazon-icu-i18n-translator

pkg:composer/eugene-erg/amazon-icu-i18n-translator

Statistics

Installs: 92

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

2.0.0 2026-12-03 09:12 UTC

This package is auto-updated.

Last update: 2026-12-03 09:12:43 UTC


README

Amazon Translate as a translation provider of eugene-erg/icu-i18n-translator, without the AWS SDK: the requests are signed with Signature Version 4 by the package itself (checked against the example of AWS).

  • Placeholders survive. The arguments of a message (and the tags of markup) travel as <span translate="no">, which Amazon leaves untranslated and moves where the target language needs them. The translator checks every text that comes back; a text whose placeholders were lost goes to the next provider.
  • Formality, profanity, brevity, custom terminologies.
  • Languages from Amazon. Which directions it translates is asked from Amazon, and the closest code is picked: zh_Hant/zh_TW → zh-TW, zh_CN → zh, pt_PT → pt-PT, pt_BR → pt, fr_CA → fr-CA.
  • Texts without words ({count} %, —) are not sent; temporary errors (throttling, 5xx) asked again; keys and signatures never in exceptions.

Amazon takes no context, and one text per request (at most 10,000 bytes): the texts of a file are sent one by one.

Installation

composer require eugene-erg/amazon-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

use EugeneErg\AmazonIcuI18nTranslator\AmazonTranslator;
use EugeneErg\AmazonIcuI18nTranslator\Auth\EnvironmentCredentials;
use EugeneErg\AmazonIcuI18nTranslator\Client\AmazonClient;
use EugeneErg\AmazonIcuI18nTranslator\Formality;
use EugeneErg\HttpProviderIcuI18nTranslator\Http\HttpJsonClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;

$factory = new Psr17Factory();
$amazon = new AmazonTranslator(
    client: new AmazonClient(
        new HttpJsonClient(new Psr18Client(), $factory, $factory),
        credentials: new EnvironmentCredentials(),   // AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
        region: 'eu-central-1',
    ),
    formality: Formality::Formal,     // optional, for the languages that tell
    terminologies: ['brand-names'],   // optional: custom terminologies
    cache: $psr16Cache,               // optional: the language list, a day by default
);

// one of the providers of the translator, tried in order
$translator = new Translator(..., translators: [$amazon], formatters: [...]);

The IAM user or role needs translate:TranslateText and translate:ListLanguages.

Credentials

EnvironmentCredentials AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN; Lambda sets them
StaticCredentials new StaticCredentials(new Credentials($id, $secret, $sessionToken))
your own implement CredentialsProviderInterface: the role of an EC2 instance or an ECS task, a secret manager

Options

Default
AmazonClient(endpoint:) https://translate.{region}.amazonaws.com a VPC endpoint, LocalStack
AmazonTranslator(formality:) none Formality::Formal or Informal
maskProfanity, brevity false profanity masked; shorter translations, where Amazon has it
terminologies [] names of custom terminologies, applied to every text
cache, cacheTtl, cacheKey none, 86400, amazon-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 Amazon's error type and message (UnsupportedLanguagePairException: …, AccessDeniedException: …); its code is the status.
  • InvalidResponseException: an answer this package does not understand.
  • CredentialsException: no credentials where they were looked for.

A direction Amazon 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:

  • AmazonIcuI18nTranslator\Http\HttpJsonClient and RetryPolicy are EugeneErg\HttpProviderIcuI18nTranslator\Http\…;
  • AmazonIcuI18nTranslator\Exceptions\… are EugeneErg\HttpProviderIcuI18nTranslator\Exceptions\…, and AmazonExceptionInterface is ProviderExceptionInterface; TransportException keeps 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), phpunit

The tests answer from a fake PSR-18 client; the signature is checked against the example of AWS. Nothing is sent to AWS.

License

MIT