Cannot switch language in Laravel 4

前端 未结 2 407
日久生厌
日久生厌 2021-02-05 19:48

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


        
2条回答
  •  误落风尘
    2021-02-05 20:25

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

提交回复
热议问题