Package Data | |
---|---|
Maintainer Username: | Lykegenes |
Maintainer Contact: | patricksamson236@gmail.com (Patrick Samson) |
Package Create Date: | 2015-07-11 |
Package Last Update: | 2022-10-26 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:25:45 |
Package Statistics | |
---|---|
Total Downloads: | 1,202 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 6 |
Total Watchers: | 2 |
Total Forks: | 2 |
Total Open Issues: | 0 |
A Simple Laravel middleware to easily load and switch the user's locale.
Via Composer
composer require lykegenes/laravel-locale-switcher
Then, add this to your Service Providers :
Lykegenes\LocaleSwitcher\ServiceProvider::class,
and add this Alias [Optional]
'LocaleSwitcher' => Lykegenes\LocaleSwitcher\Facades\LocaleSwitcher::class,
Finally, you will need to register this Middleware either in your Kernel.php
or directly in your routes.
This way, you can control which routes will have automatic locale detection (Web pages) or not (your API).
Make sure this middleware is placed after any Session or Cookie related middlewares from the framework.
\Lykegenes\LocaleSwitcher\Middleware\SwitchLocaleMiddleware::class,
Optionally, you can publish and edit the configuration file :
php artisan vendor:publish --provider="Lykegenes\LocaleSwitcher\ServiceProvider" --tag=config
To change the locale, simply add "locale" to the route parameters. It works for all your routes. Some examples :
http://my-app.com/?locale=en
http://my-app.com/some/sub/route?locale=fr
http://my-app.com/some/sub/route?locale=es&otherParam=value
This will store the locale in the user's session, and set it as the current locale everytime the user requests a page.
You can build the routes with the included helpers. The URLs will be generated using the current locale.
$url = action_localized('SomeController@someFunction', ['your' => 'parameters']);
$url = route_localized('someNamedRoute', ['your' => 'parameters']);
To build a URL to the same page, but with a different locale, use the switch_locale()
helper.
$url = switch_locale('fr'); // URL of the French version of the current page.
You can easily generate a dropdown using the laravelcollective/html package :
HTML::ul(LocaleSwitcher::getEnabledLocales());
The MIT License (MIT). Please see License File for more information.