Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Js Localization Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Export in one place. Put 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.
  • Export deliberately, not everything. 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.
  • Consume with 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.
  • Prefer data over server-rendered JS. Keep the exported file as the single source of strings and read Lang.get() in your bundle/Blade, instead of mixing @lang() and Lang.get() for the same key — drift is otherwise inevitable.
  • Cache-bust the asset. Browsers and CDNs happily serve yesterday's localization.js. Append a version query (?v={{ filemtime(public_path('js/localization.js')) }}), run it through Mix/Vite, or generate at deploy time.
  • Deploy-time generation. For immutable/read-only environments, call the same export logic from a custom artisan command (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.
  • SPA alternative. If file-writing is awkward, wrap the same translation lookup in a route/controller returning JSON and have the frontend hydrate Lang from it. The package's value is then mainly the Lang helper contract.

Gotchas and Tips

  • Runtime writes need a writable 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.
  • It's a global, single-locale file. Generation captures whichever locale is active when 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.
  • Overwrite races. Concurrent requests rewriting the same file can produce partial content or last-writer-wins behavior for different group sets. Stick to a stable superset of groups, and avoid varying exports per page.
  • Only flat string groups survive. Nested arrays and complex 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.
  • Facade alias still matters. Auto-discovery registers the provider, but code calling JS:: needs the alias (or a fully-qualified import) on older Laravel versions.
  • Republishing overwrites customizations. If you customize the published JS stub, keep it under version control and re-apply after vendor:publish.
  • Mind what you export. Validation messages and internal error strings end up in a public static file; don't export groups containing sensitive operational detail.
  • Maturity signal. With 143 stars and no dependents, treat this as aging glue code: pin the version, and be ready to replace it with a small JSON endpoint plus a ~30-line Lang helper if you need multi-locale output, caching headers, or modern build integration.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
takashato/laravel-vietqr
aitor24/laralang
kraenzle-ritter/entity-relations
dindigital/din-image
pomirleanu/image-colors
sawmainek/laravel-api-generator
delatbabel/jsonresponses
mouhamedfd/permissions-generator
eduplus/qr
laravel2580/idehelpers
delatbabel/viewpages
anavel/gettext
muhamadrezaar/highcharts
delatbabel/keylists
frankkessler/salesforce-laravel-oauth2-rest
anavel/foundation
wdmtech/enviricons
eii/laravel-captcha
muka/shape-reader
tdt/json