laravel-localizer
laravel-localizer copied to clipboard
Manage multiple language app and detect languages automatically.
laravel-localizer
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::setLocalefor changing locales - add Carbon store to get localized timestamps
- add Facade to use
allowedLocales()method in views
Install
composer require naoray/laravel-localizer
Publish config with php artisan vendor:publish --provider="CodeZero\Localizer\LocalizerServiceProvider" --tag="config"
Usage
Add Supported Locales
Edit supported-locales array of the localizer config to include all allowed locales.
'supported-locales' => ['en', 'de']
Add Carbon Store
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,
],
Use Localizer in middleware
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 Change Locale routes in view
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
Extend Functionalities
You can add new stores and detectors by implementing the corresponding interface. For more info visit codezero/laravel-localizer
Testing
Run the tests with:
vendor/bin/phpunit
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email DummyAuthorEmail instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.