Easy way to handle Laravel's lang.json in Javascript. 🌎
Inspired by Ziggy and based on Laravel way to Retrieving Translation Strings.
Manipulating translations strings at Javascript when you are using Laravel should not be so tricky. So, why not use the lang.json
file that is already at your back-end?
Just run:
npm install @wesleyhf/laravel-localization-js --save
# or using yarn
yarn add @wesleyhf/laravel-localization-js
import { createLaravelLocalization } from 'laravel-localization-js';
import lang from '../../lang/pt-br.json';
const __ = createLaravelLocalization(lang);
console.log(__('Hello world')); // output: Olá mundo
// example of lang.json
{
'My name is Wesley': 'Meu nome é Wesley',
'My name is :name': 'Meu nome é :name',
};
// undefined key
__('Hello world'); // 'Hello world'
// defined key
__('My name is Wesley'); // 'Meu nome é Wesley'
// peplacing parameters
__('My name is :name', { 'name': 'Wesley' }); // 'Meu nome é Wesley'
MIT License © Wesley Francisco