Skip to content

wesleyhf/laravel-localization-js

Repository files navigation

laravel-localization-js

CI

Easy way to handle Laravel's lang.json in Javascript. 🌎

Inspired by Ziggy and based on Laravel way to Retrieving Translation Strings.

Why

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?

Contents

Installation

Just run:

npm install @wesleyhf/laravel-localization-js --save

# or using yarn

yarn add @wesleyhf/laravel-localization-js

Setup

import { createLaravelLocalization } from 'laravel-localization-js';
import lang from '../../lang/pt-br.json';

const __ = createLaravelLocalization(lang);

console.log(__('Hello world')); // output: Olá mundo

Usage

// 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'

License

MIT License © Wesley Francisco

About

Easy way to handle Laravel's lang.json in Javascript 🌎

Resources

License

Stars

Watchers

Forks

Packages