Make use of Laravel features and best packages for localization

Laravel localization and multi-language functionality in web

A step by step guide to implement multi-language functionality in your web projects

Amir Yousefi
Jun 30 · 3 min read

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 post, I will explain how to implement multi-language functionality.


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 locales.

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.


Although this method is easy and enough, 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 post.

Keep in touch and share your ideas about Laravel localization and how you implement multi-language functionality in your web projects. What other methods and Laravel packages do you use in your multi-language projects?

Also, you can read my other post about Laravel authorization and user’s permission management in Laravel.


If you find this multi-language functionality method useful in Laravel and you may want to implement this on your Laravel projects, share your ideas with me. Follow me on Twitter, Let’s connect on LinkedIn and give me a visit to amiryousefi.com

The Startup

Medium's largest active publication, followed by +524K people. Follow to join our community.

Amir Yousefi

Written by

Making Impacts on Individuals and Organizations using Information Technology

The Startup

Medium's largest active publication, followed by +524K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade