Description
Adds the missing localization features to Laravel: language selection in the URL and from headers, translatable routes and many other small features that are commonly needed in multilanguage sites.
Easy i18n localization for Laravel, an useful tool to combine with Laravel localization classes.
The package offers the following:
- Detect language from browser
- Smart redirects (Save locale in session/cookie)
- Smart routing (Define your routes only once, no matter how many languages you use)
- Translatable Routes
- Supports caching & testing
- Option to hide default locale in url
- Many snippets and helpers (like language selector)
Usage
Add the following to your routes file:
// routes/web.php Route::group(['prefix' => LaravelLocalization::setLocale()], function(){ /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/ Route::get('/', function() { return View::make('hello'); }); Route::get('test',function(){ return View::make('test'); });}); /** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
Once this route group is added to the routes file, a user can access all locales added into supportedLocales (en and es by default).
For example, the above route file creates the following addresses:
// Set application language to Englishhttp://url-to-laravel/enhttp://url-to-laravel/en/test // Set application language to Spanishhttp://url-to-laravel/eshttp://url-to-laravel/es/test // Set application language to English or Spanish (depending on browsers default locales)// if nothing found set to default localehttp://url-to-laravelhttp://url-to-laravel/test
The package sets your application locale App::getLocale() according to your url. The locale may then be used for Laravel's localization features.
You may add middleware to your group like this:
Route::group([ 'prefix' => LaravelLocalization::setLocale(), 'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]], function(){ //...});