laravel-localization

Laravel made it so easy to implement a multi-language website. You can implement it with Laravel localization and just some tricks. Also, there is plenty of Laravel translation packages which you can use in your project. In this blog post, I will explain how to implement multi-language functionality. I used these methods in my web development projects and they are all tested and worked pretty well.

Creating a multi-language website requires two steps. Firstly, you need to detect user local language setting and change it bu user choice. Secondly, you need to translate messages and strings into user local language, in which we use Laravel localization.

Detecting and setting user locale

In order to detect user language setting, we need to create a language middleware. this middleware checks for locale setting in the user session. If there was no locale setting, the middleware sets a default locale setting. Then, it sets system locale by the user session setting.

if(is_null(session('locale')))
{
  session(['locale'=> "en"]);
}
app()->setLocale(session('locale'));

Setting locale is enough for Laravel localization to work. After that, we need a simple function To change the system language. This function gets a locale string and sets the user locale session.

public function change_lang($lang){
 if(in_array($lang,['en','tr','fa'])){
   session(['locale'=> $lang]);
 }
 return back();
}

In order to make sure the given string is a locale string, we check the language string against an array of locale.

language selector

Any route to that function, like a drop down to select language will work perfectly and will show your website multi-language functionality for users. So they can easily choose their languages.


Using Laravel localization to translate strings

Every string that needed to be translated must be in Laravel lang directive or __ function. For example, you can manage all message strings with inside messages.

@lang('messages.successful_login')

In addition, you can find more useful information about localization like how to put variables inside translation strings in Laravel documentation.

Laravel Langman package is one of the useful packages for translation. In order to translate strings, every time you updated views with new strings, you just need to run Langman sync command:

php artisan langman:sync

Laravel Langman has a lot more commands that would help you in your Laravel project localization. Reading through its documentation will add a lot.


I used this method in some of my Laravel projects. For example, a B2B construction directory and inquiry system. Although, I realized that for SEO purposes and to share localized links to your website, you better consider concatenating user locale in your projects routes. Then, you can check user locale from the query string and the rest is just as same as I explained in this blog post.

Keep in touch and share your ideas about Laravel localization and how you implement multi-language functionality in your web projects.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

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.