Work Inquiries
[email protected]

Adding multi-language functionality in a website developed in laravel.

Run the following command in order to create the required middleware.

composer php artisan make:middleware Localization

project_name/app/Http/Middleware/Localization.php

public function handle($request, Closure $next)
{
   if(\Session::has('locale'))
   {
       \App::setlocale(\Session::get('locale'));
   }
   return $next($request);
}

Create a file name “messages.php” to “project_name/resources/lang/en/

<?php
return [
    'welcome'       => 'Welcome to our application'
];

Add the below codes in “project_name/app/Http/Kernel.php” in “protected $middlewareGroups section web”.

\App\Http\Middleware\Localization::class,

Add routes “project_name/routes/web.php

Route::get('locale/{locale}', function ($locale){
    Session::put('locale', $locale);
    return redirect()->back();
});

Create Template Blade File.

<html lang="{{ app()->getLocale() }}">
<ul>
<li><a href="{{ url('locale/en') }}" ><i class="fa fa-language"></i> EN</a></li>
<li><a href="{{ url('locale/fr') }}" ><i class="fa fa-language"></i> FR</a></li>
</ul>
<h1>{{ __('messages.welcome') }}</h1>

CR Source : Adding multi-language functionality in a website developed in laravel. – DEV

Author avatar
Devvv
© copyright 2020 - 2021 Devvv.net
All right reserved.
We use cookies to give you the best experience.