Laravel 5 6 7 Adding multi-language functionality in a website developed


#laravel #multilanguage #localization #middleware

So...here we go!

First of all, I assume that you already have a working version of laravel. Navigate to that particular folder and open your terminal or command prompt.

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

php artisan make:middleware Localization

Then open it. The file should be in "project_name/app/Http/Middleware/Localization.php"

Update the code that is present with the one below :

public function handle($request, Closure $next)
{
   if(\Session::has('locale'))
   {
       \App::setlocale(\Session::get('locale'));
   }
   return $next($request);
}
Lets say you want your website to be in English and French.
  1. Go to "project_name/resources/lang/en/"

  2. Create a file name "messages.php"

  3. Create folder "fr" in "project_name/resources/lang/"

  4. Create a file name "messages.php" in "project_name/resources/lang/fr/"


  5. Open "messages.php" in both folder and insert the following codes.

    "messages.php" in 'en' folder.

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

    "messages.php" in 'fr' folder.

    
    return [
        'welcome'       => 'Bienvenue sur notre application'
    ];
    

    Most important...

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

    \App\Http\Middleware\Localization::class,
    So your codes should be like this :
    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,
            ],
    
            'api' => [
                'throttle:60,1',
                'bindings',
            ],
        ];
    
    Now all this code in the route, "project_name/routes/web.php" on top of other defined routes.
    Route::get('locale/{locale}', function ($locale){
        Session::put('locale', $locale);
        return redirect()->back();
    });
    
    So in order to know the language that is being use, you can simply use :
    <html lang="{{ app()->getLocale() }}">

    Changing language en/fr

    To change the language, just create an anchor tag in html like the following :
    
    <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>
    To call the desired text :
    <h1>{{ __('messages.welcome') }}</h1>
    Original Content Copy By https://dev.to/fadilxcoder/adding-multi-language-functionality-in-a-website-developed-in-laravel-4ech

No comments

Note: only a member of this blog may post a comment.