Laravel 5.4 #22 : Multilanguage application

Today we are going to make our project multilanguage, that is, it is in several languages (Spanish and English). Although we will simply apply it to static texts, not to those that are stored in the database.

The strings that will be displayed according to the language we select are saved in resources/lang . There we will see a en folder, which is where the English translations are. Create a folder called es, in my case, if you want another language, simply, create the folder with the language you want.

Once this is done, create both in en folder and es folder a file called  header.php and will contain:

These will be the strings that will translate into the views. To access to these strings we can do it in different ways, I have used this one:

First we indicate the name of the file and then the name of the text string.

 

Modify the header in  layouts/app.blade.php to make it multilanguage:

 

Now we are going to create a middleware that will determine the language:

Open it:

This file what it does is checking if we have a session called lang, if we don’t have it, will take the browser language and will set it as app language.

Now, to apply it, we go to  app/Http/Kernel.php :

 

The only thing we need is to create the route with the middleware web (previously modified):

And we add the links to change the language where we want:

 

 

-

https://laravel.com/docs/5.4/localization

https://laravel.com/docs/5.4/session