Create your laravel project in multi language

69 0 0 0

Last Updated : 2022-05-23 08:22:58

In this snippet I will teach you how to make your laravel project multi language

Hello guys, 


In this snippet I will teach you how to make your laravel project multi language.


The whole idea of this method is to access session and put locale in session with value of the language you want. and make a middleware to check if session has locale value and then change the app locale based on the session locale value, and this middleware will be applied on all web routes. So let's get started in details 


1- go to config/app then add the following array like this


'languages' => [
'ar' => 'العربيه',
'en' => 'english',
],

2- go to your blade in which you will select the language you want, so select the language selection will be a form with post method and in a script to submit that form when change the selected language like this


<div class="text-center m-2">
<form action="{{route('language.change')}}" method="POST" id="language_form">
@csrf
<select name="language" id="language" class="border-0">
@foreach (config('app.languages') as $locale => $language)
<option value="{{$locale}}" {{ session()->get('locale') == "$locale" ? 'selected' : '' }}><i class="flag-icon flag-icon-us"></i>{{$language}}</option>
// check if session has locale and it equals to $locale value to select it
@endforeach
</select>
</form>
</div>

3- create a post route for changing the app locale value like this


Route::post('/lang/change', [LangController::class, 'change'])->name('language.change');  

4- create LangController and provide change method to it like this


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class LangController extends Controller
{
/**
* Change Locale Value
*
* @return \Illuminate\Http\Response
*/
public function change(Request $request)
{
App::setLocale($request->language);
session()->put('locale', $request->language);
return redirect()->back();
}

}

5- at this stage the app locale changed and locale value put to the session, now its time to create a middleware to check if the session has locale then it will change app locale to the session value, So you will create LanguageManager middleware like this


<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class LanguageManager
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if (session()->has('locale')) {
App::setLocale(session()->get('locale')); //it will change app locale to the session value
}

return $next($request);
}
}

6- then you have to publish this middleware to all web routes or to specific routes as you want like this


protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\LanguageManager::class, // note that we call LanguageManager middleware here to apply to all routes
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];

7- at the last step of that process you have to create lang folders based on the lang you want and apply translation to it, then display it in the blade view like this


{{ __('words.text') }}

I hope this will help you guys

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.