Laravel JavaScript Lang
, (*1)
A Laravel package that exposes translations to JavaScript., (*2)
Installation
Require the package in composer:, (*3)
"require": {
"pod-point/laravel-javascript-lang": "^1.0"
},
Add the service provider to your config/app.php
providers array:, (*4)
'providers' => [
PodPoint\JsLang\Providers\ServiceProvider::class
]
Then finally, publish the config files:, (*5)
php artisan vendor:publish --provider="PodPoint\JsLang\Providers\ServiceProvider"
Usage
Now the varable $jslang
is available in your view. We recommend attaching it to a data tag on your body element:, (*6)
<body data-jslang='{{ $jslang }}'>
There is a provided JavaScript module with a helper method you can use to retrieve the translations:, (*7)
import jsLang from '../lib/jslang';
const string = jsLang.get('orders.form.error');
Or you can get the data yourself:, (*8)
JSON.parse(document.body.getAttribute('data-jslang'))