Today I’ll share my knowledge in laravel localization. setting up a multi-language site is not an easy task. somehow laravel makes it very easy for you. so let’s dive into further in laravel localization.

Laravel Translation File

As you can see below directory structure. we have already resources/land directory. This directory has all the translation files. There are two ways you can add/store translation string.

  1.  you can create a language_code.json file.
    • For example you want to add the French language. Then add fr.json in  resources/lang/fr.json
  2. You can manage translation string your resources/views directory structure-wise
    • We need to create language code name-based directory then add your translation file

You can check more details about the translation file in laravel official documentation: https://laravel.com/docs/7.x/localization

Add Multilanguage Feature

Now let’s see how we can add multilanguage features to the site.

Step 1:

Create a Localization middleware. to create via command use below command
php artisan make:middleware Localization
it’ll create Localization middleware in /app/Http/Middleware/Localization.php

<?php

namespace App\Http\Middleware;

use Closure;

class Localization
{
    /**
    * Handle an incoming request.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Closure $next
    * @return mixed
    */
    public function handle($request, Closure $next)
    {
        return $next($request);
    }
}

 

Step 2:

Laravel has two methods to manage the current language getLocale() and setLocale(). also we can put locale keyword on session and store current language code.

<?php

namespace App\Http\Middleware;

use Closure;

class Localization
{
    /**
    * Handle an incoming request.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Closure $next
    * @return mixed
    */
    public function handle($request, Closure $next)
    {
        if(!empty($request->la)) {

            App::setlocale($request->la);
            Session::put('locale', $request->la);

        }

        return $next($request);
    }
}

Here $request->la, where la will have language code on which need to be translated.

Step 3:

Add this middleware in kernel file
now we need to add Localization middleware to application’s route middleware groups. so every time request is sent we check for language-related parameters and set language.
in /app/Http/Kernel.php

/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\Localization::class,
    ],
];
Step 4:

Add route method to change language

Route::get('locale/{locale}', function ($locale){

    Session::put('locale', $locale);

    return redirect()->back();

})->name('lang.switcher');

Here I have written a clouser function but you shouldn’t write a clouser function.

Now your site become multi-language. you can switch language by

route('lang.switcher', [ 'locale' => 'fr' ]);
route('lang.switcher', [ 'locale' => 'hi' ]);

or you can pass la query parameter as we have taken care of middleware task.
route('home').'?la=fr';
route('home').'?la=hi';

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.