pod-point/laravel-javascript-lang

Laravel package that exposes translations and routes to JavaScript

v3.0.2 2024-07-01 13:23 UTC

README

Packagist

A Laravel package that exposes translations to JavaScript.

Installation

Require the package in composer:

"require": {
    "pod-point/laravel-javascript-lang": "^1.0"
},

Add the service provider to your config/app.php providers array:

'providers' => [
    PodPoint\JsLang\Providers\ServiceProvider::class
]

Then finally, publish the config files:

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:

<body data-jslang='{{ $jslang }}'>

There is a provided JavaScript module with a helper method you can use to retrieve the translations:

import jsLang from '../lib/jslang';

const string = jsLang.get('orders.form.error');

Or you can get the data yourself:

JSON.parse(document.body.getAttribute('data-jslang'))