Laravel multi language site

Authors

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.

Overview

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.

Localization middleware

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

Localization.php
<?php

namespace AppHttpMiddleware;

use Closure;

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

Set laravel app language

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.

Localization.php
<?php

namespace AppHttpMiddleware;

use Closure;

class Localization
{
    /**
    * Handle an incoming request.
    *
    * @param IlluminateHttpRequest $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.

Add 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

Kernel.php
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
    'web' => [
        AppHttpMiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
        IlluminateSessionMiddlewareStartSession::class,
        // IlluminateSessionMiddlewareAuthenticateSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class,
        AppHttpMiddlewareVerifyCsrfToken::class,
        IlluminateRoutingMiddlewareSubstituteBindings::class,
        AppHttpMiddlewareLocalization::class,
    ],
];

Add route method to change languages

web.php
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.

Use in laravel

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';