andywer/js-localization
Bring Laravel's translations to JavaScript. An artisan command exports your PHP lang files, and a lightweight client-side translator looks up keys and replaces placeholders at runtime, so backend and frontend share the same strings with no duplication. Works with any frontend stack.
Install the package and let Laravel discover it (5.5+); on older versions register the provider and the JS facade alias manually:
composer require andywer/js-localization
php artisan vendor:publish --provider="Andywer\JsLocalization\JsLocalizationServiceProvider"
Publishing gives you config/js-localization.php (where the generated file goes, typically public/js/localization.js) and a JS stub. First working use case — expose one translation group to the browser:
// app/Providers/AppServiceProvider.php (boot)
use JS;
public function boot()
{
JS::put(['messages' => 'messages']);
}
<script src="{{ asset('js/localization.js') }}"></script>
<script>
console.log(Lang.get('messages.welcome'));
</script>
Look first at the published config for js_path, then verify the file exists at public/js/localization.js after a request and that the web server can write to that directory. That single file is the whole integration surface — everything else is Laravel translations plus a JS Lang helper.
JS::put([...]) in a service provider or a tiny middleware rather than scattered across controllers. Calling it on every request keeps the file current, but pair it with an environment guard (e.g., only in local/staging) if you don't want runtime writes in production.JS::put() with only the groups the frontend actually renders keeps localization.js small. If your version supports a bulk method (putAll() / passing bare group names), use it only for small apps; otherwise list groups explicitly.Lang.get and Lang.choice. Lang.get('messages.welcome') for strings and Lang.choice('messages.apples', count) for pluralization mirror Laravel's trans()/trans_choice() closely enough for UI copy, but test placeholder and plural syntax rather than assuming parity.Lang.get() in your bundle/Blade, instead of mixing @lang() and Lang.get() for the same key — drift is otherwise inevitable.localization.js. Append a version query (?v={{ filemtime(public_path('js/localization.js')) }}), run it through Mix/Vite, or generate at deploy time.php artisan app:export-lang) during deploy instead of writing on boot. The package writes a file; treat it as a build artifact when you can.Lang from it. The package's value is then mainly the Lang helper contract.public/. Heroku, read-only Docker images, Lambda and similar environments will fail or silently no-op. Ensure public/js is writable in dev and switch to deploy-time generation in production.JS::put() runs. Apps that switch locale per user/session need either per-locale output files or to regenerate on each request with the correct locale applied first.trans() structures won't round-trip cleanly. Validate plural ranges and :placeholder replacement against Laravel instead of trusting them blindly.Lang undefined? Almost always a missing <script> include, an empty generated file, or JS::put() never being called. Check public/js/localization.js directly before debugging the JS.JS:: needs the alias (or a fully-qualified import) on older Laravel versions.vendor:publish.Lang helper if you need multi-locale output, caching headers, or modern build integration.How can I help you explore Laravel packages today?