How to create a multilingual project in Laravel 10

This article will show you how to set up a language system and change the language in the menu with the necessary translations.

Step 1: First, you need to create a controller App/Http/Controllers to record the language in session and be able to retrieve it with the middleware we will create just after :

$ php artisan make:controller LocalizationController
namespace App\Http\Controllers;

use App;
use Illuminate\Http\RedirectResponse;

class LocalizationController extends Controller
{
    /**
     * @param $locale
     * @return RedirectResponse
     */
    public function index($locale)
    {
        App::setLocale($locale);
        session()->put('locale', $locale);
        return redirect()->back();
    }
}

Step 2: Now, you can create a middleware App/Http/Middleware to retrieve the language that was stored in the session :

$ php artisan make:middleware Localization
namespace App\Http\Middleware;

use App;
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 (session()->has('locale')) {
            App::setLocale(session()->get('locale'));
        }
        return $next($request);
    }
}

Step 3: After that, you must save the middleware in app/http/Kernel.php :

/**
     * The application's route middleware groups.
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            (...)
            \App\Http\Middleware\Localization::class,
        ],

        (...)
    ];

Step 4: Finally, create a route to change the language :

use App\Http\Controllers\LocalizationController;

Route::get('lang/{locale}', [App\Http\Controllers\LocalizationController::class, 'index']);

Step 5: Now add the following code to your menu, It will enable you to switch from one language to another :

@php $locale = session()->get('locale'); @endphp

   @switch($locale)
        @case('en')
            <a class="dropdown-item" href="lang/ur">Urdu</a>
            @break
        @case('ur')
            <a class="dropdown-item" href="lang/en">English</a>
            @break
        @default
            <a class="dropdown-item" href="lang/en">English</a>
    @endswitch
   

Step 6: Then, to finish, in the resources/lang folder, you have to create a folder for each language you want (for example, ur for Urdu and en for English), and for each folder, do the following to take into account the text of the homepage to translate: file path: resources/lang/en/sidebar.php

<?php

return [

    'User Configuration' => 'User Configuration',
    'Basic Setting' => 'Basic Setting',
    'Student Record' => 'Student Record',
    'Users' => 'Users',
    'Roles' => 'Roles',
    'Permissions' => 'Permissions',
    'Country Setup' => 'Country Setup',
    'Province Setup' => 'Province Setup',
    'District Setup' => 'District Setup',
    'Department Setup' => 'Department Setup',
    'Sessions Setup' => 'Sessions Setup',
    'Student Type Setup' => 'Student Type Setup',
    'Classes Setup' => 'Classes Setup',
    'Sections Setup' => 'Sections Setup',
    'Student Registration' => 'Student Registration',
    'Generate Cards' => 'Generate Cards',

];

Then translate the above terms into Urdu in the following file: file path: resources/lang/ur/sidebar.php

<?php

return [

    'User Configuration' => 'صارف کی ترتیب',
    'Basic Setting' => 'بنیادی ترتیب',
    'Student Record' => 'طلباء کا ریکارڈ',
    'Users' => 'صارفین',
    'Roles' => 'کردار',
    'Permissions' => 'اجازتیں',
    'Country Setup' => 'ملک کی ترتیب',
    'Province Setup' => 'صوبائی  ترتیب',
    'District Setup' => 'ضلعی ترتیب',
    'Department Setup' => 'شعبہ ترتیب',
    'Sessions Setup' => 'دورہ تنظیم ',
    'Student Type Setup' => 'طالب علم کی قسم کی ترتیب دینا',
    'Classes Setup' => ' اجماعات کی ترتیب',
    'Sections Setup' => 'حصے کی ترتیب',
    'Student Registration' => 'طالب علم کے اندراج ',
    'Generate Cards' => 'کارڈز بنائیں ',
    
];

Step 7: To test that everything works fine, insert this code on your homepage or any other page:

{{__('users.User')}}   or @lang("users.User")

Step 8: If you want your application should be opened in the Urdu language instead of default “English” then you can change in config\app.php

 'locale' => 'ur',

Leave a Comment