Home » Php » Cannot switch language in Laravel 4

Cannot switch language in Laravel 4

Posted by: admin October 29, 2017 Leave a comment

Questions:

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('/');
});
Answers:

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')));

Questions:
Answers:

I solved the problem by putting

App::setLocale(Session::get('lang', 'en'));

in app/start/global.php