Implementing multilingual routes in a Laravel application can enhance user experience by allowing users to access content in their preferred language. .
Step 1: Install Required Packages
To manage translations easily, you can use the laravel-localization
package. Install it using Composer:
1 |
composer require mcamara/laravel-localization |
Step 2: Configure the Package
After installing, publish the configuration file:
1 |
php artisan vendor:publish --provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider" |
Step 3: Set Up Language Files
Create language files for the languages you want to support. For example, you can create resources/lang/en/routes.php
and resources/lang/es/routes.php
for English and Spanish routes.
Example for English (resources/lang/en/routes.php
):
1 2 3 4 5 |
return [ 'home' => 'home', 'about' => 'about', 'contact' => 'contact', ]; |
Example for Spanish (resources/lang/es/routes.php
):
1 2 3 4 5 |
return [ 'home' => 'inicio', 'about' => 'acerca-de', 'contact' => 'contacto', ]; |
Step 4: Define Routes with Localized URLs
In your routes/web.php
, use the localization functionality to define your routes. Use the LaravelLocalization
facade to create translatable routes.
1 2 3 4 5 6 7 |
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; Route::group(['prefix' => LaravelLocalization::setLocale()], function () { Route::get(LaravelLocalization::transRoute('routes.home'), [HomeController::class, 'index'])->name('home'); Route::get(LaravelLocalization::transRoute('routes.about'), [AboutController::class, 'index'])->name('about'); Route::get(LaravelLocalization::transRoute('routes.contact'), [ContactController::class, 'index'])->name('contact'); }); |
Step 5: Add Language Switcher
You can create a language switcher to allow users to select their preferred language. This could be a simple dropdown or links in your layout.
Example in a Blade file (e.g., resources/views/layouts/app.blade.php
):
1 2 3 4 |
<ul> <li><a href="{{ LaravelLocalization::getLocalizedURL('en') }}">English</a></li> <li><a href="{{ LaravelLocalization::getLocalizedURL('es') }}">Español</a></li> </ul> |
Step 6: Middleware for Locale Management
To manage the locale dynamically, add a middleware to set the application’s locale based on the route:
- Create a Middleware:
1 |
php artisan make:middleware LocalizationMiddleware |
- Update the Middleware:
In LocalizationMiddleware.php
, set the locale:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\App; use Mcamara\LaravelLocalization\Facades\LaravelLocalization; class LocalizationMiddleware { public function handle($request, Closure $next) { $locale = LaravelLocalization::getCurrentLocale(); App::setLocale($locale); return $next($request); } } |
- Register the Middleware:
Register your middleware in app/Http/Kernel.php
:
1 2 3 4 5 6 |
protected $middlewareGroups = [ 'web' => [ // Other middleware... \App\Http\Middleware\LocalizationMiddleware::class, ], ]; |
Step 7: Update Views for Localized Routes
In your Blade views, use the localized route names to generate URLs:
1 2 3 |
<a href="{{ route('home') }}">{{ __('routes.home') }}</a> <a href="{{ route('about') }}">{{ __('routes.about') }}</a> <a href="{{ route('contact') }}">{{ __('routes.contact') }}</a> |
Conclusion
With these steps, you have implemented multilingual route handling in your Laravel application. Users can switch between languages, and the application will respond with routes and content in their preferred language. This setup enhances accessibility and user experience. You can further customize this implementation based on your application’s requirements.