Search by

eugene-erg / llm-icu-i18n-translator

EugeneErg

LLM translation provider for eugene-erg/icu-i18n-translator: one translation core, any model behind it (HTTP API, OpenAI-compatible local servers, command-line tools)

Package info

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

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

Statistics

Installs: 7

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.1 2026-12-01 09:38 UTC

This package is auto-updated.

Last update: 2026-12-01 09:53:24 UTC


README

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

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

Requires PHP 8.2+, ext-intl, eugene-erg/icu-i18n-translator ^2.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):

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:

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:

use EugeneErg\LlmIcuI18nTranslator\Transport\Cli;

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

Options

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).

Trying it with a local model

ollama pull qwen2.5:7b && ollama serve
$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

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

License

MIT