Use Laravel localization key into your Javascript code

Laravel
October 05, 20221 minuteuserPankaj Makwana
Use Laravel localization key into your Javascript code

Convert all your localization messages from your Laravel app to JavaScript.

Generally, we cannot use the laravel localization key in its javascript. is that possible to use laravel localization in javascript?

Yes, now you can use laravel localization into laravel, thanks to the rmariuzzo/Laravel-JS-Localization package.

In this tutorial, we will learn how to use laravel localization in javascript, so let's get started.

Install the package

 composer require mariuzzo/laravel-js-localization

In your Laravel app go to config/app.php and add the following service provider:

Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider::class

Usage

The Laravel-JS-Localization package provides a command that generates the JavaScript version of all your messages found in app/lang (Laravel 4) or resources/lang (Laravel 5) directory.

Generating JS messages

 php artisan lang:js

The resulting JavaScript file will contain all your messages plus messages.js in your public directory.

Link that js file into your main layout.

 

Documentation

Getting a message

 Lang.get('messages.home');

Getting a message with replacements

 Lang.get('messages.welcome', { name: 'Joe' });

Changing the locale

 Lang.setLocale('es');

Our Laravel Products

InfyHMSInfyHMS-SaaSInfyJobsInfyChatInfyCareInfyVCards-SaaSInfyInvoices-SaaSInfyCal