TranslationService
-
class
TranslationService -
- Fully qualified name
-
\Netresearch\Nr Llm\ Service\ Feature\ Translation Service
Language translation with quality control.
translate(string $text, string $targetLanguage, ?string $sourceLanguage = null, ?TranslationOptions $options = null): TranslationResult-
Translate text to target language.
- param string $text
-
Text to translate
- param string $targetLanguage
-
Target language code (e.g., 'de', 'fr')
- param string|null $sourceLanguage
-
Source language code (auto-detected if null)
- param TranslationOptions|null $options
-
Translation options
TranslationOptions fields:
formality: 'formal', 'informal', 'default'domain: 'technical', 'legal', 'medical', 'marketing', 'general'glossary: array of term translationssite: site identifier (with). When noSite () glossaryis set, the glossary that site keeps for the language pair applies — in the prompt on the LLM paths, as a DeepL glossary on the DeepL path (ADR-208, Managing translation glossaries)preserve_: boolformatting provider,model: pin the provider / model for this callconfiguration: identifier of a storedLlmwhose translator is used on the specialized-translator path (Configuration translate)With Translator () tag_:handling htmlorxml(with). DeepL keeps the tags and translates the text between them; the LLM translator keeps tags whileTag Handling () preserve_is on (ADR-209)formatting cache_: seconds (ttl with). Opt-in cache forCache Ttl () translate: an identical request — translator, languages, text, glossary, options, and for the LLM translator the default configuration with its skills (no provider pinned) or the provider's default model (provider pinned, no model) — is answered from theWith Translator () nrllm_cache (tagresponses Translation) without reaching the translator, and therefore without a budget check or a usage row. Blank and truncated answers are never stored; saving or deleting a glossary, configuration, model, provider, skill or prompt snippet flushes the tag. Off by default (ADR-209)Service:: CACHE_ TAG max_: on the configuration path capped at the model'stokens max_when known (ADR-209)output_ tokens
- Returns
-
TranslationResult
translateForConfiguration(string $text, string $targetLanguage, LlmConfiguration $configuration, ?string $sourceLanguage = null, ?TranslationOptions $options = null): TranslationResult-
Translate with a stored
Llm's persona/tone.Configuration Unlike translate, this routes through
Llmso the configuration's storedService Manager:: chat With Configuration () system_, model, provider and skills apply.prompt translatesupplies its own system message and therefore short-circuits() Message, so a configuration'sShaper:: apply System Prompt () system_never reaches the model on that path. Here the translation task and constraints (target/source language, formality, glossary, "output only the translation") are layered into the user message instead, keeping the configuration'sprompt system_as the system message.prompt Mirrors
chatandWith Tools For Configuration () embed.For Configuration () - param string $text
-
Text to translate
- param string $targetLanguage
-
Target language code (e.g., 'de', 'fr')
- param LlmConfiguration $configuration
-
The configuration whose persona/model drive the call
- param string|null $sourceLanguage
-
Source language code (auto-detected if null)
- param TranslationOptions|null $options
-
Translation options;
temperature,max_andtokens modeloverride the configuration's stored defaults when set. Theproviderfield is ignored — the configuration selects the provider.
- Returns
-
TranslationResult
translateBatch(array $texts, string $targetLanguage, ?string $sourceLanguage = null, ?TranslationOptions $options = null): array-
Translate multiple texts.
- param array $texts
-
Array of texts
- param string $targetLanguage
-
Target language code
- param string|null $sourceLanguage
-
Source language code (auto-detected if null)
- param TranslationOptions|null $options
-
Translation options
- Returns
-
array<TranslationResult>
detectLanguage(string $text, ?TranslationOptions $options = null): string-
Detect the language of text.
- param string $text
-
Text to analyze
- param TranslationOptions|null $options
-
Translation options
- Returns
-
string Language code (ISO 639-1)
scoreTranslationQuality(string $sourceText, string $translatedText, string $targetLanguage, ?TranslationOptions $options = null): float-
Score translation quality.
- param string $sourceText
-
Original text
- param string $translatedText
-
Translated text
- param string $targetLanguage
-
Target language code
- param TranslationOptions|null $options
-
Translation options
- Returns
-
float Quality score (0.0 to 1.0)
Editor localization menu: translate with a chosen configuration
An editor localization menu that lets the user pick between
configurations with different tones/prompts resolves the chosen
Llm and hands it to
translationservice-translateforconfiguration — the
configuration's system_ (persona/tone) and model then drive
the call, while the translation task itself is layered in automatically.
use Netresearch\NrLlm\Service\Feature\TranslationServiceInterface;
use Netresearch\NrLlm\Service\LlmConfigurationServiceInterface;
public function __construct(
private readonly TranslationServiceInterface $translationService,
private readonly LlmConfigurationServiceInterface $configurationService,
) {}
// 1. Offer the configurations the current backend user may use.
$choices = $this->configurationService->getAccessibleConfigurations();
// 2. Resolve the one the editor selected in the menu.
$configuration = $this->configurationService->getConfiguration($selectedIdentifier);
// 3. Translate with that configuration's persona/tone and model.
$result = $this->translationService->translateForConfiguration(
$text,
'de',
$configuration,
// $sourceLanguage: null => auto-detected
);