This article will provide some of the most important examples of how to create multilingual website in laravel 9. This article goes in detail on laravel 9 multilingual website example. This tutorial will give you a simple example of how to add multiple language in laravel 9. This article will give you a simple example of laravel 9 multi language with language dropdown. Let's get started with laravel 9 multiple language website.
In this example, we will create three languages English, Spanish and French website with translation. we will use laravel localization to add a translation for label. Then we will create middleware to manage dynamic language. We will create one blade file with a dropdown where we can select a language and it will work dynamically. so let's see the below steps:
So, let's follow a few steps to adding multi language support in laravel.
Step 1: Install Laravel 9
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
In this step, we will create two routes one for display dashboard page with language dropdown and another for you can change language logic.
so add bellow routes.
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\LangController; /*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/ Route::get('lang/home', [LangController::class, 'index']);Route::get('lang/change', [LangController::class, 'change'])->name('changeLang');
Step 4: Create LangController Controller
In this point, now we should create new controller as LangController. this controller will manage layout and change language dynamically logic, so put bellow content in controller file:
app/Http/Controllers/LangController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request;use App; class LangController extends Controller{ /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('lang'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function change(Request $request) { App::setLocale($request->lang); session()->put('locale', $request->lang); return redirect()->back(); }}
Step 5: Create View
In Last step, let's create lang.blade.php(resources/views/lang.blade.php) for layout and we will write design code here and put following code:
In this file we need to create one middleware that will manage dynamic language that we selected on dropdown. so let's create middleware using bellow language.
php artisan make:middleware LanguageManager
Now you have to update middleware file like bellow:
app/Http/Middleware/LanguageManager.php
<?php namespace App\Http\Middleware; use Closure;use App; class LanguageManager{ /** * 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); }}
Now we need to register it to kernel file. so let's add it as bellow: