My blog

posts

Laravel Multiple Language Support using Localization

In this tutorial, you will learn by example how to perform Laravel localization and present your application in multiple languages.

We will be using Laravel version 8.x in this tutorial.
This tutorial has been created with the assumption that you have the necessary knowledge of the PHP programming language and the Laravel framework.
Your domain is localhost. If not, then replace localhost with your domain name or IP address (depending on your installation).

So, in Laravel, just like in many other frameworks, you will store translations for different languages in separate files. There are two ways to organize Laravel translation files:

An old approach that involves storing your files under the following path: resources/lang/{en,np}/{myfile.php}.
A new approach to having resources/lang/{en.json, np.json} files.

For languages that differ by territory, you should name the language directories/files according to ISO 15897. For example, for British English, you would use en_GB rather than en-gb. In this article, we will focus on the second approach, but the same applies to the first one (with the exception of how translation keys are named and retrieved). Also, we’ll be using the default welcome.blade.php file as our playground.

Step 1: Create Localization files inside resources/lang

First, create the required files inside the resources/lang folder and name them according to the locale usage. For English, we are using en and for Nepali, we are using np. Inside the lang folders, we will use messages.php file to store the data.

Step 2: Add Data to messages.php

resources/lang/en/messages.php

<?php
return [
    // Navigation and Menu section'home' => 'HOME',
   'contact' => 'CONTACT US',
];
?>

resources/lang/np/messages.php

<?php
return [
    // Navigation and Menu section'home' => 'घर ',
   'contact' => 'सम्पर्क ',
];
?>

Step 3: Using the data in the blade files 

We will use those data in our blade file welcome.blade.php like below:

resources/views/welcome.blade.php

<nav><ul><li>{{ __('messages.home')}}</li>  
    <li>{{ __('messages.contact')}}</li>  
  </ul>
</nav>

Step 4: Creating Middleware to set the Localization 

Create a new file to set the localization. This is only created once for a project.

app/Http/Middleware/Localization.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Localization
{
    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param  \Closure  $next
     * @return mixed
     */public function handle($request, Closure $next){
        if (\Session::has('locale'))
        {
            \App::setlocale(\Session::get('locale'));
        }
        return $next($request);
    }
}

Step 4: Set Kernel to use the Localization middleware created 

Add a new line to represent the middleware just created to the Kernel file inside middlewareGroups.

app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
       ......
        \App\Http\Middleware\UserActivity::class,   //add this line
     .....
    ],
];

Step 5: Adding the routes

routes/web.php

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

Step 6: Switching the locales

In your blade, use this link to redirect and use the locale.

<a href="{{ url('locale/np') }}">Nepal</a>
<a href="{{ url('locale/en') }}">English</a>

Now just create the front end for switching the routes to various locales and it will use the messages we just created in various languages,

Hope this laravel localization tutorial will help you.


/

347 Laravel February 2, 2023 0 comments

0 Comments

Add a comment