eugene-erg / amazon-icu-i18n-translator
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
Requires
- php: ^8.2
- ext-intl: *
- eugene-erg/http-provider-icu-i18n-translator: ^1.0
- 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 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\HttpJsonClientandRetryPolicyareEugeneErg\HttpProviderIcuI18nTranslator\Http\…;AmazonIcuI18nTranslator\Exceptions\…areEugeneErg\HttpProviderIcuI18nTranslator\Exceptions\…, andAmazonExceptionInterfaceisProviderExceptionInterface;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), 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