I tried routing to switch language but there's no change. Could you help me, pls?
Route::get('lang/{lang}', function($lang)
{
App::setLocale($lang);
return Redirect::to('/');
});
Laravel's gives you the option to change the locale for a single Http Request by executing the setLocale method on App Facade App::setLocale($locale); , But what if once the language is changed you don't want to worry about setting the Locale and this should be taken care by an automatic code logic.
To fetch current locale you should use App::getLocale() or App::isLocale('...') . Localization. You can also use app()->getLocale() which in Blade would be {{ app()->getLocale() }} .
App::setLocale()
is not persistent - that is to say that it will not remember between requests what you have stored. Instead you could use the session to remember the chosen locale, and read from the session the locale on each request. We can also read the default locale (from config) in case there isn't one set in the session.
// app/routes.php
Route::get('lang/{lang}', function($lang)
{
Session::put('my.locale', $lang);
return Redirect::to('/');
});
// app/start/global.php
App::setLocale(Session::get('my.locale', Config::get('app.locale')));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With