Overview
TheTranslationAgent class extends NeuronAI’s Agent class to provide specialized AI-powered translation capabilities. It handles communication with various AI providers (OpenAI, Anthropic, Gemini) and processes translation requests in optimized chunks.
Namespace: Mateffy\AiTranslations
Extends: NeuronAI\Agent
Class Definition
Constructor
The source translation file containing original translations
The target translation file where translations will be generated
Whether to use fast model variants (e.g., GPT-4o-mini instead of GPT-4o)
Number of translation keys to process per AI request. Lower values reduce token usage but increase API calls.
Properties
The source translation file
The target translation file
Fast mode flag
Maximum number of keys processed per request
Methods
provider()
Resolves and returns the configured AI provider instance.An instance of the configured AI provider (OpenAI, Anthropic, or Gemini)
Supported Providers
- OpenAI: Uses models from
ai-translations.openai.modelorai-translations.openai.fast_model - Anthropic: Uses models from
ai-translations.anthropic.modelorai-translations.anthropic.fast_model - Gemini: Uses models from
ai-translations.gemini.modelorai-translations.gemini.fast_model
Configuration
Set the provider in yourconfig/ai-translations.php:
instructions()
Generates the system prompt that instructs the AI on how to perform translations.The system prompt used to instruct the AI model
Key Instructions
The generated prompt instructs the AI to:- Translate Laravel language files from source to target language
- Output only structured JSON via the
translatetool - Reuse existing translations when available for consistency
- Handle dot-notated keys properly
- Avoid double-escaping characters
- Translate ALL keys without skipping
getOutputClass()
Returns the DTO class used for structured output.Returns
TranslationsDTO::classrunTranslation()
Executes the translation workflow using AI, processing keys in chunks.Array of dot-notated translation keys that need translation (e.g.,
['auth.failed', 'validation.required'])Whether this is a retry attempt. When
true, sends a simplified prompt without full file context to save tokens.Optional callback function with signature:
function(int $index, int $total): void$index: Current chunk index being processed$total: Total number of chunks
Associative array of translated keys and values in dot notation format
Process Flow
- Splits missing keys into chunks (default: 35 keys per chunk)
- For each chunk:
- Generates a JSON schema requiring all keys in the chunk
- Sends prompt to AI with context (full file on first request, minimal on retries)
- Extracts structured JSON response
- Accumulates translations
- Calls progress callback if provided
- Returns all accumulated translations
Example
The
runTranslation() method uses a custom JSON schema that enforces translation of all requested keys. This ensures the AI cannot skip or omit translations.Custom Chunk Size
ThechunkSize parameter allows you to control the trade-off between API efficiency and token usage:
Integration with Translator
TheTranslationAgent is typically used by the Translator class rather than directly:
TranslationAgent directly for more fine-grained control:
Provider-Specific Behavior
Different AI providers may exhibit different characteristics:Related Classes
- Translator - High-level orchestrator that uses TranslationAgent
- TranslationFile - Manages translation file data