Laravel Localization
Laravel Localization stats
- Downloads
- 5.3M
- Stars
- 3,218
- Open Issues
- 69
- Forks
- 487
Easy localization for Laravel
Laravel Localization Package Summary
The Laravel Localization package provides a comprehensive solution for managing localization in Laravel applications, enhancing the framework's built-in localization capabilities. This package simplifies the implementation of multilingual routes, content, and automatic language detection.
Key Features
- Automatic Language Detection: Detects user's language preferences from the browser settings.
- Smart Redirection: Supports session and cookie-based redirection for remembering user language choices.
- Intelligent Routing: Allows defining routes once and automatically handles language-based route variations.
- Translatable Routes: Enables URL translations for different languages, maintaining the same controller logic.
- Caching and Testing Support: Enhances performance and reliability with route caching and built-in testing capabilities.
- Locale Management: Includes utilities to hide the default locale in URLs, customize locale order, and map locales for URLs.
- Helpers and Utilities: Provides multiple helpers for generating localized URLs, getting current locale details, and creating language selectors.
Installation and Configuration
Install the package via Composer:
composer require mcamara/laravel-localization
For Laravel versions 5.4 and below, manually register the service provider as detailed in the package's additional documentation.
Publish the configuration file with:
php artisan vendor:publish --provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider"
This will create a config/laravellocalization.php file where you can set supported locales, language detection settings, and other preferences.
Usage
Wrap routes within a localized group to enable automatic language prefixing and apply middleware for locale redirections:
Route::group(['prefix' => LaravelLocalization::setLocale()], function() { Route::get('/', function() { return view('welcome'); });});
Middleware
Register the necessary middleware in your app/Http/Kernel.php to handle locale session redirection, cookie redirection, and more. Each middleware plays a role in managing how users interact with different locales in your application.
Advanced Features
- Localized URLs: Generate URLs specific to locales and manage how URLs are presented with or without the default locale.
- Translated Routes: Define route translations in language-specific files to support URL translations across different languages.
- Route Caching: Use the custom route caching command provided by the package to ensure that your localized routes are efficiently managed.
Testing
The package supports testing localized applications by allowing you to set the locale context explicitly in your test cases.
Practical Utilities
- Language Selectors: Easily create dropdowns or menus for users to switch languages.
- Locale Information Helpers: Retrieve current locale details like name, script, and direction for use in views and controllers.
Conclusion
Laravel Localization is an essential package for developers looking to create multilingual web applications with Laravel. It extends the framework’s capabilities, providing robust tools for managing language preferences, routes, and content localization seamlessly.