A simple scenario when we want to setup the Locale Language settings (App::setLocale()) depending on a $_GET parameter as in following URL:
http://aboutlaravel.com/?language_code=en
Possible Solution: Create a middleware and register it in app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
...
\App\Http\Middleware\Localization::class
],
The Localization middleware will look like this:
public function handle($request, Closure $next)
{
if(isset($_GET['language_code'])) {
if ($_GET['language_code'] == "en") {
App::setLocale("en");
} else if ($_GET['language_code'] == "de") {
App::setLocale("de");
}
}
return $next($request);
}
With PHP >= 7.0 you can use a very nice one liner.
Just use this:
App::setLocale(request('langauge_code', 'en'));
// en is the default if request has no language_code
VoilĂ :)