Easily detect the language of your user from session
, cookie
, browser locale
or app setting
. Change locale with a get request to localizer::setLocale
and control which language version the user can see via localizer
middleware.
This package is using codezero/laravel-localizer behind the scenes and extends its functionality to include:
- add Route
localizer::setLocale
for changing locales - add Carbon store to get localized timestamps
- add Facade to use
allowedLocales()
method in views
composer require naoray/laravel-localizer
Publish config with php artisan vendor:publish --provider="CodeZero\Localizer\LocalizerServiceProvider" --tag="config"
Edit supported-locales
array of the localizer
config to include all allowed locales.
'supported-locales' => ['en', 'de']
Add \Naoray\LaravelLocalizer\Stores\CarbonStore::class
to localizerstores
to enable the carbon store and sync current locale with carbon locale.
'stores' => [
CodeZero\Localizer\Stores\SessionStore::class,
CodeZero\Localizer\Stores\CookieStore::class,
CodeZero\Localizer\Stores\AppStore::class,
Naoray\LaravelLocalizer\Stores\CarbonStore::class,
],
Add localize
middleware to your web route in the RouteServiceProvider
//...
protected function mapWebRoutes()
{
Route::middleware(['web', 'localize'])
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
Or simply add it as a middleware to your route groups.
Add Localizer
Facade to app
config.
//...
'Localizer' => Naoray\LaravelLocalizer\Facades\LocalizerFacade::class,
In the view you can use allowedLocales()
to get all allowed locales in the view.
@foreach (Localizer::allowedLocales() as $locale)
<a href="{{ route('localizer::setLocale', ['locale' => $locale]) }}">{{ strtoupper($locale)}}</a>
@endforeach
You can add new stores
and detectors
by implementing the corresponding interface. For more info visit codezero/laravel-localizer
Run the tests with:
vendor/bin/phpunit
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email DummyAuthorEmail instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.