Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Change loaded language

Currently i have a language loaded inside MY_Controller which extends CI_Controller. But inside a special page which controller (let's call it ABC controller) extends MY_Controller, I need to override the loaded language with another language. I tried loading another language inside this ABC controller, but unsuccessful. Is there a way to unload the loaded language and load another language?

like image 319
coder Avatar asked Sep 27 '11 01:09

coder


3 Answers

an easier way is to reset the language data and is_loaded

$this->lang->is_loaded = array();
$this->lang->language = array();
like image 60
Damian Dennis Avatar answered Oct 07 '22 11:10

Damian Dennis


I know it's a bit late to answer this but I think you can change the config item 'language' dynamically based on page requirement.

$this->config->set_item('language', 'chinese');
$this->config->set_item('language', 'english'); // based on the language folder of course holding language files

I had a requirement to send newsletters in users base lang, and this helped me change the language on the fly, hope this might help..

like image 38
Anupam Avatar answered Oct 07 '22 13:10

Anupam


Have you tried just loading the language file you need?

$this->lang->load('filename', 'language');

It should be then accessible just like your default language. I haven't tested this tho, but from my understanding this should be the way to go about it.

Reference: http://codeigniter.com/user_guide/libraries/language.html


REVISED

I ended up digging a bit more for you, and found that you CANNOT load a default language (define it as default in your controller) and then later try to change it to something else.

Follow these steps:

  • If you need a language OTHER than english (default), set that in your config.
  • If you want to load ANOTHER language on a controller basis, you need to define that (most commonly in your constructor using something like session array / user selection.
  • You cannot load 2 languages (1 in the constructor, then another in a different class.. won't work!)

Reference here per forum posts: http://codeigniter.com/forums/viewthread/176223/

like image 27
Jakub Avatar answered Oct 07 '22 11:10

Jakub