Translation Modes
AI Translations for Laravel supports two primary translation strategies that are automatically selected based on your files’ state.Partial Translation (Selective Update)
Partial translation updates only the missing or outdated keys in an existing translation file, leaving all other translations untouched. When it’s used:- Target language file already exists
- You’ve added new keys to the source language
- You want to preserve existing translations
Translate Only Missing
Only the identified missing keys are sent to the LLM for translation, along with full context of both files.
Full Translation
Full translation creates a complete translation file from scratch, translating every key in the source language. When it’s used:- Target language file doesn’t exist
- Translating to a completely new language
- Target file is empty or missing
array_diff() returns all source keys as “missing,” triggering a complete translation.
Command usage:
Full translation processes the entire source file in chunks, which may take several minutes for large translation files (500+ keys).
Chunking Strategy
To handle large translation files efficiently and stay within LLM token limits, the package uses an intelligent chunking strategy.Chunk Size Configuration
The default chunk size is 35 keys per chunk, defined in theTranslationAgent class:
- Balances token usage with translation quality
- Prevents context window overflow
- Allows LLM to maintain focus on each subset
- Provides reasonable progress increments
Chunking Implementation
chunk() method:
Context Management Across Chunks
An optimization is applied to avoid sending the full context repeatedly:First Chunk
First Chunk
Includes:
- Complete source language file (as JSON)
- Complete target language file (as JSON)
- List of missing keys for this chunk
- Translation task instructions
Subsequent Chunks
Subsequent Chunks
Includes:
- List of missing keys for this chunk
- Translation task instructions
- Full file contents (already in conversation context)
This approach significantly reduces token usage for large translation jobs while maintaining consistent quality across all chunks.
Progress Tracking
Chunking enables granular progress reporting:Retry Logic
The package implements robust retry logic to handle LLM inconsistencies and ensure all keys are translated successfully.Retry Mechanism
Retry Flow
Attempt 2 (counter = 1)
If keys are missing, retry with
retry: true flag. Only the still-missing keys are processed.Common Retry Scenarios
Nested Array Response
Problem: LLM returns nested arrays instead of flat dot notationSolution: Auto-flatten and retry
Incomplete Response
Problem: LLM skips some keys in the chunkSolution: Automatic retry with only missing keysThe verification step identifies missing keys and retries just those specific translations.
Malformed JSON
Problem: LLM returns invalid JSON structureSolution: JSON schema validation fails, triggering retryStructured output ensures valid JSON or automatic retry.
Network Timeout
Problem: API request times out or failsSolution: Exception is thrown, but can be retried manuallyUse
--interactive mode to handle failures gracefully.Retry Optimization
The retry system is optimized to minimize wasted effort:- Incremental retry: Only missing keys are retried, not the entire chunk
- Context preservation: Conversation history is maintained across retries
- Max 3 attempts: Prevents infinite loops from persistent LLM issues
- Merged results: Successfully translated keys from attempt 1 are preserved
Model Selection Strategy
The package supports two model tiers for different use cases:Standard Models (Default)
- Production translations
- Customer-facing content
- Complex or nuanced language
- First-time translations to new languages
Fast Models
- Development and testing
- Updating a few missing keys
- Non-critical translations
- Quick iterations
Fast models are typically 5-10x cheaper and 2-3x faster, but may produce slightly lower quality translations, especially for idiomatic expressions.
Strategy Selection Guide
Adding a new language
Adding a new language
Strategy: Full translation with standard modelWhy: Creates complete, high-quality translations for all keys.
Updating existing translations
Updating existing translations
Strategy: Partial translation with fast modelWhy: Quickly updates only new keys, preserving existing translations.
Large translation file (500+ keys)
Large translation file (500+ keys)
Strategy: Full translation with standard model, monitor progressWhy: Chunking handles large files efficiently while maintaining quality.
Development/testing
Development/testing
Strategy: Partial translation with fast modelWhy: Rapid iterations without significant cost.
Production deployment
Production deployment
Strategy: Validate first, then partial translation with standard modelWhy: Ensures quality and only translates what’s necessary.
Best Practices
Regular Validation
Run
translate:validate before deploying to catch missing translations early.Incremental Updates
Translate frequently as you add features rather than accumulating hundreds of missing keys.
Source Language Consistency
Keep your source language (usually
en) complete and well-written - it directly affects translation quality.Interactive Mode
Use
--interactive for critical translations to review and retry individual files.