Laravel Localization made simple

Orie Chinedu Emmanuel
3 min readDec 2, 2018

Localization in Laravel in it’s simplest term means changing the application’s default language to the language preferred by the user.

In this article, we’ll walk through the process of implementing Localization in a very simple way. We’ll implement an app that supports four languages namely, German, Chinese, French and English language with English as the default language.

Requirements

  • Knowledge of Laravel

The sample project used in this article is on Github. Clone the project and set it up let’s get started.

Getting Started

There are two different ways we can achieve localization in Laravel:

  1. Using Short Keys
  2. Using Translation Strings as Keys

To understand what each of the types above entails in the general sense, see the official Laravel docs here. For the purpose of this article, our focus will be on Using Translation Strings as Keys and how to implement dynamically setting the language.

Step 1

The first step is to create the translation strings. In the resources/lang directory, create the following files:

  • zh.json // for Chinese language
  • fr.json //for French
  • ge.json // for German

Let’s assume that the text below is the text on the web page we want to translate:

Hi there!

I hope this meets you well.

Let’s talk about Localization in Laravel, trust me, it’s an awesome topic. Notwithstanding, not many have come to realize this awesomeness.

Localization according to the dictionary, means adapting something to a fixed position or location. It therefore suffices to say that Laravel localization involves adapting the language used in the app to the given location of the user.

This might not make much sense to you but you’ve got no reason to worry. Nothing said here should be argued about because the idea is just to make up words for the purpose of testing this tutorial.

How the text appears on the web page

For each of the language JSON files above, copy the text you want to translate and paste into Google translator to get its equivalent of the desired language. For example, the ge.json, fr.json, and zh.json files for the above text will look like below:

Note:

The entire text that would be placed in the @lang() or __() blade directive for translation must be translated together. For example, if there are two sentences in a paragraph, the two sentences must be translated together.

In the view, the translation is done as shown below:

As u can see, you can choose to use either @lang() or __(), whichever you find easier to use will work fine.

Step 2

Next up, we are going to create a LocalizationController and a middleware for dynamically changing the language.

$ php artisan make:controller LocalizationController

$ php artisan make:middleware Localization

Now edit the LocalizationController.php as follows:

Next up, let’s create the route, add the route below to the web.php

Route::get('lang/{locale}', 'LocalizationController@index');

How the route looks is all your choice, For this article, the dropdown that switches the locale looks as shown below and is located in resources/layouts/app.blade.php

Step 3

Let’s edit the Localization middleware. The Localization middleware forces the selected locale for every request. I said ‘force’ because setting the locale in the index method of the LocalizationController is not enough to do the trick.

Edit the Localization middleware as follows:

For every incoming request, the middleware checks the current locale in the session and set it accordingly.

Next up, add the middleware in the Kernel.php. It should be added to the $middlewareGroups array as shown below.

That is it. If you followed the steps correctly, then it should be working perfectly by now.

Conclusion

We’ve carefully gone through the processes involved in implementing a multi-language localization in Laravel. Feel free to share your opinion about the topic. Should you need any further clarification, please don’t hesitate to reach out here or via Twitter

--

--