Codeigniter如何在函数__construct()中使用此代码为所有函数添加多语言

时间:2016-11-24 22:15:34

标签: php codeigniter multilingual

我需要以某种方式添加此代码,以便在函数__construct()中使用它来不重复每个函数的代码。

我在索引

中完成了
public function index($lang = ""){
    $ip = $_SERVER['REMOTE_ADDR'];
    $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
    if($query && $query['status'] == 'success') {
        $country = $query['country'];
        if($country == 'Brazil'){
            $this->lang->load('front',$lang==''?'pt_BR': $lang);
            $this->session->set_userdata('site_lang', $lang);
            $data['home'] = 'home';
            $data['which_country'] = $country;
            $this->load->view('front_end/site/home',$data);
        }elseif($country == 'Egypt' || $country == 'Algeria' || $country == 'Bahrain' || $country == 'Iraq' || $country == 'Jordan' || $country == 'Kuwait' || $country == 'Lebanon' || $country == 'Libya' || $country == 'Morocco' || $country == 'Oman' || $country == 'Palestine' || $country == 'Qatar' || $country == 'Saudi Arabia' || $country == 'Sudan' || $country == 'Syria' || $country == 'Tunisia' || $country == 'United Arab Emirates' || $country == 'Yemen'){
            $lang = "ar_EG";
            $this->lang->load('front',$lang==''?'ar_EG': $lang);
            $this->session->set_userdata('site_lang', $lang);
            $data['home'] = 'home';
            $this->load->view('front_end/site/home_arabic',$data);
        }elseif($country != 'Brazil' || $country != 'Egypt' || $country != 'Algeria' || $country != 'Bahrain' || $country != 'Iraq' || $country != 'Jordan' || $country != 'Kuwait' || $country != 'Lebanon' || $country != 'Libya' || $country != 'Morocco' || $country != 'Oman' || $country != 'Palestine' || $country != 'Qatar' || $country != 'Saudi Arabia' || $country != 'Sudan' || $country != 'Syria' || $country != 'Tunisia' || $country != 'United Arab Emirates' || $country != 'Yemen'){
            $this->lang->load('front',$lang==''?'en_US': $lang);
            $this->session->set_userdata('site_lang', $lang);
            $data['home'] = 'home';
            $data['which_country'] = $country;
            $this->load->view('front_end/site/home',$data);
        }else{
            //$data['remove']="<script>$('#cookie-law').remove();</script>";
        }
    } else {
        //echo 'Unable to get location';
    }
}

1 个答案:

答案 0 :(得分:1)

控制器的内部构造()?您可以使用帮助程序并自动加载它。在帮助函数内部调用$CI = &get_instance();以访问会话。