Introduction
Last updated:
On this page
JS Localization
JS Localization puts your Laravel translations in JavaScript, with @javaabu/i18n's __(), trans() and trans_choice(). They take the same arguments and give the same output as Laravel's:
<button onclick="alert(__('Saved, :name!', { name: 'އަލީ' }))">{{ __('Save') }}</button>
const { __, trans_choice } = useTranslation()
return <p>{trans_choice("messages.apples", count)}</p>
It is the successor of Laravel-JS-Localization, and does more than export files:
- Blade pages need no setup. Every HTML page gets
__(),trans()andtrans_choice()onwindow, loading the page's locale from a cached endpoint. Delivered through javaabu/manifest, so it works with or without Vite. - Inertia and SPA bundles get the lines from a Vite plugin that manifest's
manifest()runs, one lazy chunk per locale. In development, editinglang/updates the page without a reload. - The same lines PHP sees. Lines are read through Laravel's translation loader:
lang/{locale}/*.php, nested files likeforum/thread,lang/{locale}.json, packages' JSON lines and namespaced files (shop::cart), with yourlang/vendoroverrides. php artisan lang:jswrites them as JSON for@javaabu/i18n's loaders, or as a<script>that setswindow.Lang.php artisan lang:missinglists keys your PHP, Blade, JS and TSX use that a locale doesn't translate, and lines your base locale has that others lack. It fails in CI when something is missing.php artisan lang:translate --to=dvfills in missing lines with AI (Gemini, OpenAI, Anthropic and others through laravel/ai), keeping:placeholders, plurals and tags intact, and writes a report to review.
| You want to | Read |
|---|---|
| Set it up | Installation & Setup |
| Translate in Blade pages' scripts | Blade pages |
| Translate in React or Vue with Inertia | Inertia and bundled apps |
| Move from Laravel-JS-Localization | Migrating |
Using an AI coding assistant? See AI assistants.