2016-11-24 1 views
0

J'ai besoin d'ajouter ce code pour l'utiliser dans la fonction __construct() pour ne pas répéter le code pour chaque fonction.Codeigniter comment ajouter plusieurs langues pour toutes les fonctions en utilisant ce code dans la fonction __construct()

je l'ai fait dans l'index

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'; 
    } 
} 

Répondre

1

intérieur construct() d'un contrôleur? Vous pouvez utiliser un assistant et le charger automatiquement. Dans votre fonction d'assistance, appelez le $CI = &get_instance(); pour accéder à la session.

+0

merci pour la suggestion je goint pour essayer .. c'est beaucoup d'étapes que je dois obtenir un guide! Si vous avez un temps et pouvez me donner un exemple je serai reconnaissant! Merci quand même! –

+0

merci .. je viens d'essayer quelque chose de plus facile pour moi d'ajouter une variable protégée avant __construct et d'appeler $ this-> variable ... plus tard, je vais apprendre d'autres façons de le faire. Merci encore! –