Laravel 9 Create Multi Language Website Tutorial
Creating a Multilingual Website in Laravel 9
Welcome to this comprehensive guide on creating a multilingual website using Laravel 9. In this tutorial, we'll delve into the intricacies of building a multilingual web application. By the end, you'll have a clear understanding of how to implement multiple languages, integrate dynamic language selection, and create a seamless user experience.
Introduction
In this tutorial, we'll demonstrate how to develop a multilingual website in Laravel 9. We'll walk you through the process step by step, from setting up the project to creating language files, implementing routes, controllers, and middleware, and finally creating the user interface with language selection functionality.
Prerequisites
Before we dive into the tutorial, ensure you have Laravel 9 installed. If not, you can create a new Laravel app using the following command:
composer create-project laravel/laravel example-app
Step 1: Install Laravel 9
If you haven't already, create a new Laravel 9 project using the command mentioned above. This step is optional if you already have an existing Laravel app.
Step 2: Create Language Files
In this step, let's create the necessary language files for English, French, and Spanish translations. We'll store these files in the `lang` folder.
Create the following files:
- `resources/lang/en/messages.php`
<?php
return [
'title' => 'This is the English Language Title.',
];
- `resources/lang/fr/messages.php`
<?php
return [
'title' => 'Ceci est le titre en langue anglaise.',
];
- `resources/lang/sp/messages.php`
<?php
return [
'title' => "Este es el título en español.",
];
Step 3: Define Routes
In this step, we'll define two routes. The first route will display the dashboard page with a language dropdown, and the second route will handle language changes.
Define the following routes in `routes/web.php`:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LangController;
Route::get('lang/home', [LangController::class, 'index']);
Route::get('lang/change', [LangController::class, 'change'])->name('changeLang');
Step 4: Create LangController
Create a new controller named `LangController` using the command:
php artisan make:controller LangController
Then, update the `app/Http/Controllers/LangController.php` file with the following content:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App;
class LangController extends Controller
{
public function index()
{
return view('lang');
}
public function change(Request $request)
{
App::setLocale($request->lang);
session()->put('locale', $request->lang);
return redirect()->back();
}
}
Step 5: Create the View
Now, let's create the view `resources/views/lang.blade.php`. This view will serve as the layout for our multilingual website. Copy and paste the following code:
<!DOCTYPE html>
<html>
<head>
<title>Creating a Multilingual Website in Laravel 9</title>
<!-- Include your CSS and JavaScript assets here -->
</head>
<body>
<div class="container">
<h1>Creating a Multilingual Website in Laravel 9</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' : '' }}>French</option>
<option value="sp" {{ session()->get('locale') == 'sp' ? 'selected' : '' }}>Spanish</option>
</select>
</div>
</div>
<h1>{{ __('messages.title') }}</h1>
</div>
<script type="text/javascript">
var url = "{{ route('changeLang') }}";
$(".changeLang").change(function(){
window.location.href = url + "?lang="+ $(this).val();
});
</script>
</body>
</html>
Step 6: Create Middleware
Next, let's create a middleware to manage dynamic language selection. Run the following command:
php artisan make:middleware LanguageManager
Update the `app/Http/Middleware/LanguageManager.php` file as follows:
<?php
namespace App\Http\Middleware;
use Closure;
use App;
class LanguageManager
{
public function handle($request, Closure $next)
{
if (session()->has('locale')) {
App::setLocale(session()->get('locale'));
}
return $next($request);
}
}
Finally, register the middleware in `app/Http/Kernel.php`:
protected $middlewareGroups = [
'web' => [
// ...
\App\Http\Middleware\LanguageManager::class,
],
// ...
];
Running the App
With all the steps completed, you're ready to run the Laravel app. Open your terminal and execute:
php artisan serve
Visit the following URL in your browser to experience the multilingual website:
http://localhost:8000/lang/home
Conclusion
Congratulations! You've successfully created a multilingual website in Laravel 9. By following this tutorial, you've learned how to implement language translation, dynamic language switching, and create a seamless user interface for a diverse audience. This skill will be invaluable as you develop websites to cater to users from around the world.
For more advanced features and improvements, consider exploring Laravel's documentation and additional packages.
Leave A Comment