eugene-erg / libretranslate-icu-i18n-translator
LibreTranslate (your own server or libretranslate.com) as a translation provider of eugene-erg/icu-i18n-translator
Package info
github.com/EugeneErg/libretranslate-icu-i18n-translator
pkg:composer/eugene-erg/libretranslate-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:24 UTC
README
LibreTranslate as a translation provider of eugene-erg/icu-i18n-translator: open-source machine translation on your own server (texts never leave it), or libretranslate.com with a key.
- Placeholders survive. The arguments of a message (and the tags of markup) travel as
<span translate="no">that the server leaves untranslated. The translator checks every text that comes back, so a text whose placeholders were lost fails alone and goes to the next provider. - Batches within the limits of your server (
--batch-limit,--char-limit): 25 texts and 5,000 characters per request by default, both settings. Texts without words ({count} %,—) are not sent. - Languages from the server. A language translates into its own targets, as the server lists them; the
closest code is picked:
pt_BR→pt-BR,zh_TW→zh-Hant,de_AT→de. - Temporary errors asked again; the key never in URLs or exceptions.
LibreTranslate takes no context, so the context of a message is not sent. Its quality is below DeepL's and the language models': it fits as the provider of a closed network, or as the last of a chain.
Installation
composer require eugene-erg/libretranslate-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. A server of your own:
docker run -p 5000:5000 libretranslate/libretranslate.
Usage
use EugeneErg\LibreTranslateIcuI18nTranslator\Client\LibreTranslateClient; use EugeneErg\HttpProviderIcuI18nTranslator\Http\HttpJsonClient; use EugeneErg\LibreTranslateIcuI18nTranslator\LibreTranslateTranslator; use Nyholm\Psr7\Factory\Psr17Factory; use Symfony\Component\HttpClient\Psr18Client; $factory = new Psr17Factory(); $libre = new LibreTranslateTranslator( client: new LibreTranslateClient( new HttpJsonClient(new Psr18Client(), $factory, $factory), baseUrl: 'http://localhost:5000', // or https://libretranslate.com apiKey: getenv('LIBRETRANSLATE_API_KEY') ?: null, ), maxTexts: 25, // the --batch-limit of the server, if lower maxCharacters: 5_000, // the --char-limit of the server, if lower cache: $psr16Cache, // optional: the language list, a day by default ); // one of the providers of the translator, tried in order $translator = new Translator(..., translators: [$libre], formatters: [...]);
| Argument | Default | |
|---|---|---|
maxTexts, maxCharacters |
25, 5000 |
per request; a request the server refuses as too big fails with its message |
cache, cacheTtl |
none, 86400 |
the language list in a PSR-16 cache; without one it is asked once per object |
cacheKey |
libretranslate-icu-i18n-translator.languages |
change it when one cache serves two servers |
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 the server's message; its code is the status (403for a wrong key,429for too many requests).InvalidResponseException: an answer this package does not understand.
A direction the server 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:
LibreTranslateIcuI18nTranslator\Http\HttpJsonClientandRetryPolicyareEugeneErg\HttpProviderIcuI18nTranslator\Http\…;LibreTranslateIcuI18nTranslator\Exceptions\…areEugeneErg\HttpProviderIcuI18nTranslator\Exceptions\…, andLibreTranslateExceptionInterfaceisProviderExceptionInterface;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; nothing is sent to a server.
License
MIT