0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Janaka
  • 398
  • 5
  • 16
  • I wanted to show validation error messages according to selected language, Here user can switch between English and Japanese.. any help is appriciated – Janaka Sep 14 '16 at 10:31
  • http://stackoverflow.com/questions/7563390/codeigniter-change-loaded-language – James Lalor Sep 14 '16 at 11:03

1 Answers1

3

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)

Janaka
  • 398
  • 5
  • 16