Create mutli language website in laravel 9 apps; Through this tutorial, we will learn how to create multi language website in laravel 9 apps.
Laravel 9 Create Multi Language Website Example Tutorial
Follow the following steps to create multi language website in laravel 9 apps:
- Step 1 – Install Laravel
- Step 2 – Create Lang Files
- Step 3 – Create Routes
- Step 4 – Create LangController Controller
- Step 5 – Create View
- Step 6 – Create Middleware
- Step 7 – Start Laravel App
Step 1 – Install Laravel
Execute the following command on command prompt to install laravel app in your system:
composer create-project laravel/laravel example-app
Step 2 – Create Lang Files
Create the following folders and files for english, france and spanish language files in lang folder; is as follows:
resources/lang/en/messages.php
<?php return [ 'title' => 'This is English Language Title.', ];
resources/lang/fr/messages.php
<?php return [ 'title' => 'Ceci est le titre fr langue anglaise.', ];
resources/lang/sp/messages.php
<?php return [ 'title' => "Il s'agit du titre en langue espagnole.", ];
Step 3 – Create Routes
Visit the routes directory and open web.php. Then create two routes one for displaying the dashboard page with language dropdown and another for you can change language logic; is as follows:
<?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
Execute the following command on command prompt to create LangController file:
php artisan make:controller LangController
Then visit app/http/controllers directorya and open LangController.php file. Then add the following code into it:
<?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
Visit resources/views directory and create lang.blade.php file. And add the following code into lang.blade.php file:
<!DOCTYPE html>
<html>
<head>
<title>How to Create Multi Language Website in Laravel - Tutsmake.com</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h1>How to Create Multi Language Website in Laravel - Tutsmake.com</h1>
<div class="row">
<div class="col-md-2 col-md-offset-6 text-right">
<strong>Select Language: </strong>
</div>
<div class="col-md-4">
<select class="form-control changeLang">
<option value="en" {{ session()->get('locale') == 'en' ? 'selected' : '' }}>English</option>
<option value="fr" {{ session()->get('locale') == 'fr' ? 'selected' : '' }}>France</option>
<option value="sp" {{ session()->get('locale') == 'sp' ? 'selected' : '' }}>Spanish</option>
</select>
</div>
</div>
<h1>{{ __('messages.title') }}</h1>
</div>
</body>
<script type="text/javascript">
var url = "{{ route('changeLang') }}";
$(".changeLang").change(function(){
window.location.href = url + "?lang="+ $(this).val();
});
</script>
</html>
Step 6 – Create Middleware
Execute the following command on command prompt to create one middleware that will manage dynamic language that we selected on dropdown; is as follow:
php artisan make:middleware LanguageManager
Then visit app/Http/Middleware/ directory and open LanguageManager.php file. And add the following code into it:
<?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);
}
}
Then visit app/Http/ directory and open Kernel.php file. And register middleware; is as follows:
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\LanguageManager::class,
],
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Step 7 – Start Laravel App
Execute the following command on command prompt to start larave app; is as follows:
php artisan serve