Laravel 5.5 – Localization from scratch

Hi, in this post I will show how you can use localization, remember that there more than one way to do it. For example, you can make a Middleware to handle the localization from User preferences, you can get the localization from specific route, from GeoLocalization, etc…

In our example we will make a middleware to check if a session has locale and if had, the middleware will change the locale of your app and we will make some routes only to test it.

First step, create a fresh installation of Laravel let’s do it, open your terminal and type:

A fresh installation of Laravel has in your config/app.php file the default language of your application as you can see:

By default all the translation strings of your application are in resources/lang/LOCALE FOLDER

What documentation says:

Typically, translation strings are stored in files within the resources/lang directory. Within this directory there should be a subdirectory for each language supported by the application:

Translation files

As many other files in Laravel, these files will return an array with key pair values. Laravel also use the dot notation to access it files in your helpers.

See a example of translation file, open your /resources/lang/en/pagination.php you will see it:

If you want to retrieve the previous value in your blade, you will use:

At this point you can test it changing the welcome view to test, change the line 82 to:

Now start your server to see it 😉

I expect that you see this:

What is happening here?

If user doesn’t setted your locale, Laravel will search for the default locale in config/app.php

In our example, Laravel will find the default locale in /config/app (  ‘en‘  as we saw before), now as the @lang directive uses dot notation, searching for file > name of variable it will search for variable previous in file pagination at en folder on resources/lang.


Helpers to retrieve translation strings

It’s important to know that you can use the helper trans() in your controllers and @lang in blade templates to show a translation string. I advise you to learn more in documentation HERE.


Backing to our example

Routes to get locale and set locale

Let’s start with the route to show what the locale our app are using. Add it to your routes/web.php file

Now when you visit /locale you will see the current locale of your application.

We still need to create a route to set the locale and put it in session. Very simple! Improve your routes/web.php file adding:


The middleware

After it you need to change the locale of your app, and a Middleware it’s the best choice for our example, create it:

And maybe it’s the most simple middleware that you ever seen lol! 😛

Change the app/Http/Middleware/Localization.php to:

Don’t forget to add the middleware in middlewareGroups in Kernel file 😉

Open your /app/Http/Kernel.php


Using the translation / localization system

Create a view to show links to change your current locale and show a simple message. Create a new file in /resources/views/hello.blade.php

Register this view as route in routes/web.php

If you visit this page you see that links doesn’t work yet because we don’t have the translation files

Create these files





Well, it’s done, one of many ways of use of Laravel 5.5 Localization. If you want to see the source of this example, visit github this repository


Thank you and stay tuned! 🙂

Share this!

No Comments

You can leave the first : )

Leave a Reply

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