Laravel

Use Laravel localization key into your Javascript code

Use Laravel localization key into your Javascript code

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.

				
					<script src="{{ asset('messages.js') }}"></script>
				
			

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');
				
			
Share On: