Laravel Localization is a feature that provides support for multiple languages in your Laravel applications, allowing you to build multilingual applications easily. It enables you to translate application strings, manage language files, and define localized routes.
Key Features of Laravel Localization:
- Translation of Strings: Store translation strings in language files and retrieve them based on the user’s selected language.
- Localized Routes: Define routes that change based on the selected language, improving user experience.
- Date and Number Formatting: Automatically format dates, numbers, and currencies based on the locale.
- Middleware Support: Use middleware to set the application’s locale based on user preferences or session data.
Setting Up Laravel Localization
- Creating Language Files: Language files are typically stored in the
resources/lang
directory. Create directories for each language you want to support. For example, for English and Spanish:
12resources/lang/en/messages.phpresources/lang/es/messages.phpEach file returns an array of key-value pairs:
English (
resources/lang/en/messages.php
):12345<?phpreturn ['welcome' => 'Welcome to our application!','goodbye' => 'Goodbye!',];Spanish (
resources/lang/es/messages.php
):12345<?phpreturn ['welcome' => '¡Bienvenido a nuestra aplicación!','goodbye' => '¡Adiós!',]; - Using Translation Strings in Views: In your Blade views, you can retrieve translation strings using the
__
helper function:1<h1>{{ __('messages.welcome') }}</h1>This will display “Welcome to our application!” in English or “¡Bienvenido a nuestra aplicación!” in Spanish, depending on the current locale.
- Setting the Application Locale: You can set the application’s locale in the
config/app.php
file:1'locale' => 'en',You can also change the locale dynamically in your controllers or middleware:
123use Illuminate\Support\Facades\App;App::setLocale('es'); - Localizing Routes: You can define localized routes by grouping them under a language prefix. In
routes/web.php
, you can use route groups:1234567Route::group(['prefix' => 'en'], function () {Route::get('/home', [HomeController::class, 'index'])->name('home');});Route::group(['prefix' => 'es'], function () {Route::get('/inicio', [HomeController::class, 'index'])->name('home');});You can also use route model binding to localize routes based on the current locale.
- Middleware for Localization: Create a middleware to handle locale detection and switching based on user preferences or URL parameters.
You can create a middleware using:
1php artisan make:middleware LocalizationIn your middleware, set the locale based on the user’s preference or URL parameter:
123456789101112131415namespace App\Http\Middleware;use Closure;use Illuminate\Support\Facades\App;class Localization{public function handle($request, Closure $next){$locale = $request->get('lang', config('app.locale'));App::setLocale($locale);return $next($request);}}Register the middleware in
app/Http/Kernel.php
:123456protected $middlewareGroups = ['web' => [// ...\App\Http\Middleware\Localization::class,],];
Conclusion
Laravel Localization allows you to build applications that can support multiple languages effortlessly. By leveraging translation files, localized routes, and middleware, you can provide a seamless experience for users across different languages.
Additional Considerations
- Documentation: For more detailed information, advanced features, and configuration options, refer to the official Laravel Localization documentation.
- Language Switcher: Consider implementing a language switcher in your application to allow users to change their preferred language easily.