What's the best logic for switching language in Laravel?

后端 未结 8 1641
遥遥无期
遥遥无期 2020-12-23 02:29

I\'m using Laravel localization to provide two different languages. I\'ve got all the path stuff set up, and mydomain.com/en/bla delivers English and stores the \'en\' sessi

8条回答
  •  甜味超标
    2020-12-23 02:57

    Try use Session's. Somthing like this:

    Controller:

     class Language_Controller extends Base_Controller {
    
            function __construct(){
                $this->action_set();
                parent::__construct();
            }
    
           private function checkLang($lang = null){
             if(isset($lang)){
               foreach($this->_Langs as $k => $v){
                 if(strcmp($lang, $k) == 0) $Check = true;
               }
           }
            return isset($Check) ? $Check : false;
           }
    
           public function action_set($lang = null){
            if(isset($lang) && $this->checkLang($lang)){
                Session::put('lang', $lang);
                $this->_Langs['current'] = $lang;
                Config::set('application.language', $lang);
            } else {
                if(Session::has('lang')){
                    Config::set('application.language', Session::get('lang'));
                    $this->_Langs['current'] = Session::get('lang');
                } else {
                    $this->_Langs['current'] = $this->_Default;
                }
            }
            return Redirect::to('/');
        }
    }
    

    In Route.php:

    Route::get('lang/(:any)', 'language@set');
    

提交回复
热议问题