简体   繁体   中英

Codeigniter 3, issue in changing default language dynamically

I have defined the default language in application/config.php file as $config['language'] = 'english' ;

But in some cases I want to switch to other language, I tried using

$this->config->set_item('language', 'japanese');

and in my system/language folder I have both english and japanese folders and relevent files inside them, but when switch in controller using

$this->config->set_item('language', 'japanese');

didn't work. So how can I switch between these languages

Here $this->config->set_item('language', $user_lang ); should call in the controller's constructor or in controller's relevant functions. Then it works.

function __construct() {
    parent::__construct();

    //wanted to make this controller's language  dynamic
    $user_lang = $this->session->userdata('user_lang');
    $this->config->set_item('language', $user_lang );

}

in my function I used validation in this way

public function insert(){ 
  $user_lang = $this->session->userdata('user_lang');
  $this->lang->load('uitext',$user_lang);

    // validate data  
    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', $this->lang->line('uitext_username'), 'required');
    $this->form_validation->set_rules('email', $this->lang->line('uitext_email'), 'required|valid_email');
}

Now the validation messages show dynamically, according to user selected language (english /japanese)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM