Overview
AI Translations for Laravel uses a sophisticated architecture that combines PHP language file parsing, intelligent chunking, and LLM-powered translation to maintain high-quality, context-aware translations across your Laravel application.Architecture Components
The package consists of three core components that work together:1. TranslationFile
TheTranslationFile class is responsible for reading and writing Laravel’s PHP language files.
Key responsibilities:
- Loads existing PHP array-based translation files from disk
- Converts nested PHP arrays into flat dot-notation for LLM processing
- Merges translated keys back into the nested structure
- Writes updated translations back to PHP files
- Reads:
/lang/{language}/{domain}.php - Returns empty array if file doesn’t exist (for new languages)
- Preserves nested array structure when writing back
The
TranslationFile class uses flatten() to convert nested arrays to dot notation for the LLM, and apply() to merge translations back using Laravel’s Arr::set() helper.2. TranslationAgent
TheTranslationAgent extends NeuronAI’s Agent class and orchestrates the LLM interaction.
Configuration (TranslationAgent.php:17-22):
- OpenAI (GPT-4, GPT-3.5)
- Anthropic (Claude)
- Google Gemini
- Configurable model selection for speed vs accuracy
- Define the translation task and target languages
- Instruct the LLM to use existing translations as context
- Require flat dot-notation output format
- Prevent escaping unicode characters
- Ensure proper quote escaping in translations
3. Translator
TheTranslator class manages the overall translation workflow and retry logic.
Core workflow (Translator.php:40-96):
Translation Flow
Here’s how the complete translation process works:Load Translation Files
Both source and target
TranslationFile objects are loaded. If the target file doesn’t exist, an empty structure is created.Identify Missing Keys
The
compare() method identifies which translation keys exist in the source but are missing in the target.Chunk Missing Keys
Missing keys are split into chunks of 35 keys (configurable) to avoid overwhelming the LLM and stay within token limits.
Provide Full Context to LLM
For the first chunk only, both complete files are sent to provide context:This maintains conversation context across chunks without repeatedly sending full files.
Validate and Flatten
If the LLM accidentally returns nested arrays, they’re automatically flattened:
Verify Completion
After each chunk, the system checks if all keys were translated. Missing keys trigger a retry (up to 3 attempts).
Selective Updates
One of the key features is selective updating - the ability to update only missing or changed translations without regenerating the entire file.How selective updates work
How selective updates work
- Compare source and target files to find missing keys
- Send full context (both complete files) to the LLM
- Receive only missing translations in dot notation
- Merge new translations into existing structure
- Preserve all existing translations unchanged
- Existing UI text remains stable
- Only outdated translations are updated
- New features get translated without affecting old ones
- Translation history is preserved
Full file translation
Full file translation
When translating to a completely new language:
- Target file is empty (or doesn’t exist)
- ALL source keys become “missing keys”
- LLM translates entire file in chunks
- All chunks are merged to create complete target file
Data Flow Diagram
Key Design Decisions
Dot Notation
Using flat dot notation (
auth.failed) instead of nested arrays makes LLM responses more reliable and easier to validate with JSON schemas.Chunking
Processing 35 keys at a time balances token efficiency with translation quality, preventing context window overflow.
Full Context
Sending complete source and target files ensures the LLM understands existing terminology and maintains consistency.
Retry Logic
Up to 3 automatic retries ensure all keys are translated, handling LLM inconsistencies gracefully.
Performance Considerations
Token optimization: Only the first chunk receives full file context. Subsequent chunks in the same translation session continue the conversation, relying on the LLM’s context window to remember the full files.
- Small file (50 keys): ~10-30 seconds
- Medium file (200 keys): ~1-2 minutes
- Large file (500+ keys): ~3-5 minutes
- LLM provider and model speed
- Number of missing keys
- Network latency
- Chunk size configuration