Set locale on the fly in laravel4

后端 未结 2 2061
一个人的身影
一个人的身影 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:

    <html><body>
    
        Please select a Language:
    
        {{link_to_route('language.select', 'English', array('en'))}}
    
        {{link_to_route('language.select', 'Portuguese', array('pt'))}}
    
    </body></html>
    

    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'));
    
    0 讨论(0)
  • 2021-01-05 22:58

    There is great library for Laravel that allows you to handle locales flexible - mcamara laravel localization. In readme of the project you can find example how to implement such a switcher.

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