Set locale on the fly in laravel4

后端 未结 2 2064
一个人的身影
一个人的身影 2021-01-05 22:06

After searching through the documentation from laravel 4 I see that the way to set a language is to do

App::setLocale(\'en\');

But how do

2条回答
  •  广开言路
    2021-01-05 22:51

    This is a way:

    Create a route for your language selector:

    Route::get('language/{lang}', 
               array(
                      'as' => 'language.select', 
                      'uses' => 'LanguageController@select'
                     )
              );
    

    Create your language selectors links in Laravel Blade's view:

    
    
        Please select a Language:
    
        {{link_to_route('language.select', 'English', array('en'))}}
    
        {{link_to_route('language.select', 'Portuguese', array('pt'))}}
    
    
    

    A Controller:

    Class LanguageController extends BaseController {
    
        public function select($lang)
        {
            Session::put('lang', $lang);
    
            return Redirect::route('home');
        }
    
    }
    

    Then in your app/start/global.php you can:

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

提交回复
热议问题