kargnas/laravel-ai-translator
Automatically translate Laravel language files with AI. Scans PHP and JSON localization files, finds missing translations, and generates them via OpenAI, Anthropic, and other providers, keeping multilingual apps in sync with minimal setup.
laravel-ai-translator is a Laravel package that automates translation of your lang/ files using LLM providers. In 1.7.22 the default provider switched to OpenRouter, which means you can point at one API key and swap between dozens of underlying models without touching config structure.
Minimal steps:
composer require kargnas/laravel-ai-translatorphp artisan vendor:publish --tag="ai-translator-config"OPENROUTER_API_KEY in .env (the new default)lang/en/messages.phpphp artisan ai:translate or similar — check php artisan list for the exact signature) targeting your other locales.First use case: take an existing lang/en/*.php tree and generate lang/es/*.php without hand-editing. Start with a single small file (e.g. auth.php) to validate your API key, model choice, and prompt behavior before running over the whole directory.
Where to look first:
config/ai-translator.php — provider, model, source/target locales, chunk sizes, prompt templates.php artisan list | grep -i translat).Provider/model selection. Because OpenRouter is now the default, the common pattern is one OPENROUTER_API_KEY plus a TRANSLATOR_MODEL env var. Pin a cheap model for bulk passes (e.g. translation of large validation.php files) and a stronger model for customer-facing strings. Keep model names in config, not hardcoded in code.
Source of truth workflow. Treat lang/en/ as canonical and never hand-edit generated files in other locales. Re-run the command after English changes; the package is designed around reconciling existing translated files rather than regenerating everything. Run it in CI as a check step (dry-run if supported) so missing translations fail the build rather than silently shipping.
Integration with existing pipelines. Commit generated locale files. Do not run the translator at deploy time — it's network-bound and non-deterministic. Instead, generate locally or in a scheduled job, review the diff, and commit. This keeps deploys fast and translations auditable.
Chunking large files. validation.php is huge; expect the package to chunk long arrays. Tune the chunk size in config if you hit timeouts or truncated output. For very large trees, translate locale-by-locale (or file-by-file) rather than all targets in one invocation, so a single failure doesn't discard the whole run.
Cost control with live pricing. Since 1.7.22 fetches live pricing, inspect the current per-model rates before choosing a default. For a mid-sized app (a few thousand strings × several locales), OpenRouter's cheap models make this near-free; a top-tier model can get expensive quickly across many locales.
Local overrides via env. Keep OPENROUTER_API_KEY, model, and target locales in .env per environment (local dev can point at a cheap model; a "translation" CI job can use a stronger one).
OPENROUTER_API_KEY may not exist in your .env yet. Existing installs upgrading to 1.7.22 may fail with "no API key" until you add it or explicitly set an alternate provider.symfony/process ^8.0) is now allowed — relevant if you're on Laravel 12 / Symfony 8 toolchains. If you previously had to pin symfony/process to ^7, you can drop that constraint.:attribute, {name}), and pluralization rules.:name, {0}, %s, and Laravel pluralization (|) survive translation. Add a small test that loads each locale file and asserts placeholder parity with English.lang/ layout (JSON or PHP arrays); generated output format must match what your __() calls expect..env with your OpenRouter key — the package reads it directly, so a leaked .env is a billed risk.How can I help you explore Laravel packages today?