How To Create Multi-language Website using Laravel Localization

Akash Kumar Verma
May 14 · 2 min read

hello guys,

This is my first article about laravel localization.

#step 1

Firstly when user choose a language we put user selected locale(language) in session [example en, fr, hi ]

return redirect()->back();
for example - http://localhost/setlocale/en

#step 2

Second We need a middleware for translating pages according to our session locale (user selected)


namespace App\Http\Middleware;

use Closure;
use Session;
use App;
use Config;
class LanguageSwitcher
* Handle an incoming request.
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
public function handle($request, Closure $next)
if (!Session::has('locale'))
return $next($request);

#Step 3

Third we put this middleware in kernal.php routeMiddleware (app/Http/kernal.php)

protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'language' => \App\Http\Middleware\LanguageSwitcher::class,

Here is Language Switcher is our middleware.

#step 4

Fourth, For serving this middleware we need route group. so each request goes through this middleware.

Route::group(['middleware'=>'language'],function ()
//your translation routes

Logic in middleware

In middleware for every request we check if locale set in current session or not. if Session not having locale then for current request we set locale to config locale means in your app configuration locale.

'locale' => 'en',  // config/app.php Session::put('locale',Config::get('app.locale')); // in middleware code we use Config method to get app configuration localeApp::setLocale(Session::get('locale')); // finally set this to app locale

else if we have locale in current session then we set that to App locale

App::setLocale(Session::get('locale')); // set this to app locale

Finally by using translation method we will got our translation.

{{ __('messages.welcome') }}
{{ trans('messages.welcome') }}

__() or trans() gives you same result.

__() takes 3 arguments. other 2 is optional.

__('messages.welcome',[ 'name' => 'akash' ], 'fr')

If we need to pass any dynamic data into our translation then we need second argument

// suppose messages.php<?php

return [
'welcome' => 'welcome :name',

trans('messages.welcome', [ 'name' => 'akash' ]);
__('messages.welcome', [ 'name' => 'akash' ]);
both gives you "Hello akash"

If you need any specific locale translation then we use third argument

// suppose we need french translationtrans('messages.welcome', [ 'name' => 'akash' ], 'fr'); 
__('messages.welcome', [ 'name' => 'akash' ], 'fr');
both gives you "Bonjour akash"

If you facing any issue. please post comment. i will happy to help you,


Akash Kumar Verma

Akash Kumar Verma

Written by

Software Engineer at Xicom Technologies