Skip to content

LLM ICU i18n Translator ​

A translation provider for eugene-erg/icu-i18n-translator that works with any language model: a hosted API, a local server, or a command-line tool.

The library keeps two concerns apart:

  • the translation core (LlmTranslator): prompts, batching, the answer format and its checks. Same for every model;
  • the transport: how to reach a model. Adding a model means writing a transport, nothing else.
LlmTranslator ── StructuredCompletionInterface ──┬─ OpenAiCompatible       (understands JSON Schema)
                                                 ├─ AnthropicApi …          (eugene-erg/claude-icu-i18n-translator)
                                                 └─ PromptedJson ── TextCompletionInterface ─┬─ Cli  (claude -p, codex exec, gemini, ollama run)
                                                    (schema in the prompt,                   └─ OpenAiCompatible without structured output
                                                     JSON taken out of the text)

Installation ​

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

Requires PHP 8.2+, ext-intl, eugene-erg/icu-i18n-translator ^4.0 and any PSR-18 HTTP client for HTTP transports.

Usage ​

OpenAI and compatible servers ​

The same transport covers OpenAI, OpenRouter, DeepSeek, Mistral and local servers (Ollama, LM Studio, vLLM, llama.cpp server):

php
use EugeneErg\LlmIcuI18nTranslator\Http\HttpJsonClient;
use EugeneErg\LlmIcuI18nTranslator\LlmTranslator;
use EugeneErg\LlmIcuI18nTranslator\Transport\OpenAiCompatible;

$http = new HttpJsonClient($psr18Client, $requestFactory, $streamFactory);

// hosted
$model = new OpenAiCompatible($http, 'https://api.openai.com/v1', 'gpt-4.1-mini', getenv('OPENAI_API_KEY'), ['temperature' => 0]);

// local, no key
$model = new OpenAiCompatible($http, 'http://localhost:11434/v1', 'qwen2.5:7b');

$translator = new LlmTranslator($model);

A server without structured output (older Ollama, some llama.cpp builds) is used as a text model:

php
use EugeneErg\LlmIcuI18nTranslator\Completion\PromptedJson;

$translator = new LlmTranslator(new PromptedJson(new OpenAiCompatible($http, 'http://localhost:8080/v1', 'local')));

Command-line tools ​

The tool reads the prompt from stdin and prints the answer. It is run without a shell, and it signs in on its own, so the library never sees credentials:

php
use EugeneErg\LlmIcuI18nTranslator\Transport\Cli;

$translator = new LlmTranslator(new PromptedJson(new Cli(['ollama', 'run', 'qwen2.5:7b'], timeoutSeconds: 120)));

Options ​

php
new LlmTranslator(
    $model,
    locales: ['en', 'ru', 'de'],          // languages the model may translate between; null = any
    instructions: 'Address the user informally ("ты"). Never translate "Artel".',
    maxBatchTexts: 50,                     // texts per request
    maxBatchCharacters: 8000,              // characters per request
);

Put it into the translator alone or as one of the providers of a ProviderChain.

How a translation is checked ​

  • Variables become {{0}}, {{1}} and the model never sees ICU syntax, so it cannot break it.
  • Texts travel as JSON with ids, so quotes, new lines and markup in a text are not confused with the instructions.
  • Every answer is checked: the same placeholders as many times as in the original, nothing empty, no unknown or duplicated ids. Spaces around the text are restored from the original.
  • A text that fails the check is asked again on its own; if it fails again, the call throws and a ProviderChain moves on to the next provider. A network error is not retried by the translator: the HTTP client already retried rate limits and overloads (429, 5xx, 529, honouring Retry-After), see RetryPolicy.
  • Texts without letters ({{0}} %, 42) are not sent at all.
  • A text that already contains {{N}} cannot be told from a variable and is refused (UntranslatableTextException).

Whole messages ​

LlmTranslator is also a MessageTranslatorInterface provider: for files and translateMessage() the translator sends whole ICU messages, and the model writes every plural form and select branch at once, agreed with the target language. Russian gets one, few, many and other for an English one/other; the prompt lists the categories of the target language with sample numbers (CLDR, from php-intl), and the ordinal ones when a message uses selectordinal.

  • Messages travel as JSON with ids, their context and the note the file has for a translator (PO #., XLIFF <note>, a String Catalog comment); the answer has a JSON Schema of its own.
  • An answer that is not a valid ICU message, is empty or comes twice is left out; the missing ones are asked once more in one request.
  • What is still missing is left to the translator: it checks every answer against the original (the same arguments and selectors, the plural forms of the language) and translates the rest variant by variant, as above.

Nothing to configure: registering LlmTranslator as a provider enables both paths.

Trying it with a local model ​

bash
ollama pull qwen2.5:7b && ollama serve
php
$translator = new LlmTranslator(new OpenAiCompatible($http, 'http://localhost:11434/v1', 'qwen2.5:7b'));
var_dump($translator->translate(['You have ', new Variable(0), ' new messages'], 'en', 'ru'));

Small models (under 3B parameters) translate poorly and lose placeholders more often; the checks catch that, but expect more retries. Placeholders survive in mid-size models too, but instructions such as the form of address are followed less reliably: in our run Claude Haiku mixed "ты" and the infinitive where Claude Sonnet did not. For production translations use a strong model.

Development ​

bash
composer check    # pint --test, phpstan (level max, strict rules) and phpunit
composer fix      # apply the code style
composer coverage

License ​

MIT


From the README.md of EugeneErg/llm-icu-i18n-translator · composer require eugene-erg/llm-icu-i18n-translator