How to create multi language website by laravel 9 | WebJourney
Welcome to everyone in a new lesson. Today we will learn how to create a multi language website using latest version of laravel 9. Also we will see how to change language and entire website contents of that language. we have the following steps to do this.
1. Download freash laravel project
2. Create folders and files inside resources folder
3. Create routes in web.php
4. Create controller
5. Create view file inside views folder
6. Create middleware
Step-1: Download freash laravel project
At the first setp we need to create a fresh laravel project using composer. You may also install by laravel installer. Let's build an app multi-lang
//BY laravel installer
laravel new multi-lang
//By composer
composer create-project laravel/laravel multi-lang
Step-2: Create folders and files inside resources folder
Now we create a folder lang inside resources and inside lang we again create three folders named en for English, sp for Spanish and bn for Bengali. After create folders successfully we create a .php file name text.php inside those three folder show in bellow image.
Open text.php file from each directory and paste bellow code.
resources/lang/en/text.php
<?php
return [
'content'=>'Hello Devs, Welcome To WebJourny',
];
resources/lang/sp/text.php
<?php
return [
'content'=>'Hola desarrolladores, bienvenidos a WebJourny',
];
resources/lang/bn/text.php
<?php
return [
'content'=>'হ্যালো দেবগণ, ওয়েবজার্নিতে স্বাগতম',
];
Step-3: Create routes in web.php
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LangController;
Route::get('/', function () {
return view('welcome');
});
Route::get('lang', [LangController::class, 'lang']);
Route::get('lang/change', [LangController::class, 'lang_change'])->name('lang.change');
Step-4: Create controller
Now we will create controller and implements logics in controller
php artisan make:controller LangController
LangController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App;
class LangController extends Controller
{
public function lang()
{
return view('lang');
}
public function lang_change(Request $request)
{
App::setLocale($request->lang);
session()->put('lang_code',$request->lang);
return redirect()->back();
}
}
Step-5: Create view file inside views folder
views\lang.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Multi Language Website with Laravel 9</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="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container text-center">
<h2>Multi Language Website with Laravel 9</h2>
<hr>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<strong>Select Language: </strong>
<select class="form-control lang-change">
<option value="en" {{ session()->get('lang_code')=='en' ? 'selected' : ''}}>English</option>
<option value="sp" {{ session()->get('lang_code')=='sp' ? 'selected' : ''}}>Spanish</option>
<option value="bn" {{ session()->get('lang_code')=='bn' ? 'selected' : ''}}>Bengali</option>
</select>
</div>
</div>
<h4 class="mt-5">{{ __('text.content') }}</h4>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
var url = "{{ route('lang.change') }}";
$('.lang-change').change(function(){
let lang_code = $(this).val();
window.location.href = url + "?lang="+ lang_code;
});
</script>
</html>
Step-6. Create middleware
Finally create a middleware name LangManage and write some logics also we have to register this middleware in App\Http\Kernel.php
php artisan make:middleware LangManage
App\Http\Middleware\ LangManage.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use App;
class LangManage
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if(session()->has('lang_code')){
App::setLocale(session()->get('lang_code'));
}
return $next($request);
}
}
App\Http\Kernel.php
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\LangManage::class,
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Ok Now run artisan server using the bellow command and test your app.
php artisan serve
Paste this url in browser http://127.0.0.1:8000
Hope this will help you. Thanks
If you like what you are reading, please think about buying us a coffee as a token of appreciation.
We appreciate your support and are committed to providing you useful and informative content.
We are thankful for your ongoing support