Skip to main content
Back to packages
3,560 GitHub stars

mcamara/laravel-localization

View on GitHub

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 English
http://url-to-laravel/en
http://url-to-laravel/en/test
 
// Set application language to Spanish
http://url-to-laravel/es
http://url-to-laravel/es/test
 
// Set application language to English or Spanish (depending on browsers default locales)
// if nothing found set to default locale
http://url-to-laravel
http://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(){ //...
});

Related Content on Laravel Daily

Recent Courses on Laravel Daily

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.