Cannot switch language in Laravel 4

前端 未结 2 402
日久生厌
日久生厌 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')));
    
    0 讨论(0)
  • 2021-02-05 20:25

    I solved the problem by putting

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

    in app/start/global.php

    0 讨论(0)
提交回复
热议问题