How To Implement Localization in Laravel 5.8

This laravel 5.8 tutorial help to create multilingual web application.We will create different languages messages and display to user based on selected locale information.We will create application that help to serve fr(France) and en(English) languages message.The default laravel language is english(en), Although not everyone speaks or understands English and many countries have more than one official language.

There are many third party packages available to create multilingual website using laravel, But laravel provides inbuilt localization support to create multilingual website.

Create Multilingual Website Using Laravel Localization

The laravel provides localization features for multilingual application.The language string message are stored into in the files resources/lang directory. Within that directory, there should be a subdirectory for each language supported by the application.

We will gone through following features into this tutorial-

  • We will create different languages folder into resources/lang like fr/en.
  • Create middleware to inject locale information into each request.
  • Set user selected locale option into the laravel session.
  • Display selected language message into blade view file.

We will create sample laravel application using below command –
laravel new laravel_localization_example

Now we will cd laravel_localization_example folder.By default, The Laravel application’s local language is English or en type.You can find it on the config/app.php file.

'locale' => 'en',

You can change this default as per your locale or personalize language.

Create Languages folder into Laravel Application

There is en type subdirectory already available into resources/lang folder.The resources/lang/en folder have some files like auth.php, pagination.php, passwords.php, validation.php.

All the files returns key:value pair array data.

we will add French(fr) language support for laravel application, Now create a fr folder into resources/lang folder.Also create constant.php file into en/ and fr/ folder, please make sure the key name will be same into across the different languages folder file.We just change the value of constant.

Create same constant into France language –

How to set and get Locale Into Laravel Application

The laravel have in-built helper method to get and set locale information in application.

How to set Locale Language Into Laravel

I can use set locale language using below method –

How to get Locale Language Into Laravel

I can use get locale language using below method –

App::getLocale($locale);

We can put Local information into session using below controller method –

Create Middleware to set Localization in Laravel For Request

We can use middleware to set localeization for laravel application.Created a new middleware using below command –
php artisan make:middleware Localization

Above command will create a Localization.php file into app/Http/Middleware folder.

Create a new method inside app/Http/Middleware/Localization.php file to set Locale lang of application, First we will check locale is set if not then set into session otherwise skip.

How To Register Middleware in Laravel application

After creation of middleware into laravel application, I need to register middleware with kernel as like below, I will make one entry of Localization into app/Http/Kernel.php file.

How To Display Locale Message In Laravel

We will create a new /resources/views/hello.blade.php file to test message in different language,

Here, I have used the trans() helper string function which will take one argument combination of filename and keyname, the params as like below – filename.array_key_name

I hope, You enjoyed Multilingual functionality integration using Laravel Localization.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.