eugene-erg / google-informal-icu-i18n-translator
Text translation using multiple neural network APIs
Package info
github.com/EugeneErg/google-informal-icu-i18n-translator
pkg:composer/eugene-erg/google-informal-icu-i18n-translator
Requires
- ext-intl: *
- eugene-erg/icu-i18n-translator: ^2.0 || ^3.0 || ^4.0
- 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
- 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-02 14:35:54 UTC
README
A translation provider for eugene-erg/icu-i18n-translator
backed by the free (informal) Google Translate endpoint. The translator hands the provider plain texts with
{{_N_}} placeholders, never ICU structure: plural and select forms are handled by the translator itself.
The informal endpoint has no SLA and can change or rate-limit at any time. Use it for development and drafts, and put a reliable provider first in a
ProviderChainfor production.
Installation
composer require eugene-erg/google-informal-icu-i18n-translator
Requires PHP 8.2+, ext-intl and eugene-erg/icu-i18n-translator 2.x, 3.x or 4.x.
Then a PSR-18 HTTP client with PSR-17 factories, for example guzzlehttp/guzzle with guzzlehttp/psr7, or
symfony/http-client with nyholm/psr7.
Up to 2.x the package was named eugene-erg/google-informal-icu-i18n-translate, unlike its repository; from 3.0 it
is eugene-erg/google-informal-icu-i18n-translator. The code is the same: replace the name in composer.json.
Usage
use EugeneErg\GoogleInformalIcuI18nTranslator\Client\Client; use EugeneErg\GoogleInformalIcuI18nTranslator\Client\PsrClient; use EugeneErg\GoogleInformalIcuI18nTranslator\GoogleInformalTranslator; use EugeneErg\ICUMessageFormatParser\Parser; use EugeneErg\IcuI18nTranslator\DataTransferObjects\Variable; $google = new GoogleInformalTranslator( client: new Client(new PsrClient($httpClient, $requestFactory, $streamFactory), 'https://translate.googleapis.com'), parser: new Parser(), cache: $psr16Cache, // caches the list of supported languages maxBatchLength: 4000, // optional, characters per batch request ); $google->translate(['Hello, ', new Variable(0)], 'en', 'ru'); // ['Привет, ', Variable(0)]
Pass it to the translator as a provider (alone or inside a ProviderChain).
translateWithDetect() lets Google detect the source language and returns it with the translation;
canTranslate($to, $from) says whether Google supports the languages. The list of languages is asked once and
cached for a day in the PSR-16 cache (key GoogleInformalTranslator:getSupportedLanguages).
The client
Client is the endpoint itself, for what the translator does not need: alternative translations, the
dictionary, romanization.
use EugeneErg\GoogleInformalIcuI18nTranslator\Client\ValueObjects\GoogleTranslateType; $response = $client->single( text: 'Hello', targetLanguage: 'ru', types: [GoogleTranslateType::Translation, GoogleTranslateType::Dictionary], sourceLanguage: 'en', // null: detect ); $response->translates[0]->translatedText; // 'Привет' $response->detectedSourceLanguage; // 'en' $response->dictionary; // null unless asked for $client->getSupportedLanguages()->languages; // code => Language(name, source, target)
GoogleTranslateType chooses what Google returns: Translation (t), AlternativeTranslations (at),
Romanization (rm), Dictionary (bd), QualityCheck (qc), Examples (ex), Synonyms (ss),
RelatedWords (rw), Morphology (md); fields of the types not asked for are null.
The second argument of Client is the base URL (https://translate.googleapis.com), for a proxy or a test
server. The transport is ClientInterface::sendRequest($method, $uri, $body, $headers): PsrClient implements it
over PSR-18, and a decorator of it adds logging, retries or authentication.
Errors are under EugeneErg\GoogleInformalIcuI18nTranslator\Client\Exceptions, all extending Exception
(a RuntimeException):
| Exception | When |
|---|---|
ClientException |
a 4xx response or another PSR-18 error |
NetworkException (a ClientException) |
a 5xx response or a PSR-18 network error |
TimeoutException (a ClientException) |
a PSR-18 request error: timeout or abort |
ResponseJsonException |
a 2xx response whose body is not JSON |
Locales
ICU locales are mapped to Google language codes:
| Locale | |
|---|---|
en, EN, en_US, en-GB |
en |
pt_BR |
pt |
zh, zh_CN, zh_Hans |
zh-CN |
zh_TW, zh_HK, zh_MO, zh_Hant… |
zh-TW |
Languages detected by Google come back as ICU locales (zh-CN → zh_CN), and Google's legacy codes are
understood both ways (iw = he, jw = jv, in = id).
Batches
GoogleInformalTranslator implements BatchTranslatorInterface: the translator sends all texts of a message
in one call. They are joined with numbered separators into requests of at most maxBatchLength characters.
Texts are sent in a POST form body (Google rejects URLs longer than about 16 KB, which is only ~3 000
Cyrillic characters). If Google damages or reorders a separator, that request falls back to one request per text, so a batch never
mixes up translations.
Development
composer check # pint --test, phpstan (level max, strict rules) and phpunit composer fix # apply the code style composer coverage
License
MIT