I need to implement a way to switch languages in a website developed with Codeigniter. I know that Codeigniter has a library to manage language files and that's what I'm trying to use. But since I also need to be able to modify the translated text I'm storing the translations in the database and I'm generating the Codeigniter language files with a query, this way:
<?php
$LANGCI =& get_instance();
$lang_query = $LANGCI->db->where('lang', 'italian')->get('my_lang');
foreach ($lang_query->result() as $language_data) {
$lang[$language_data->index] = $language_data->translation;
}
This actually works, so when I call:
<?=$this->lang->line('label_about_us')?>
it displays it in the language specified in the config file.
Now I want to change the language clicking on a link. So I created a "languages" controller with this function inside:
public function set_language($lang)
{
$this->session->set_userdata('language', $lang);
$this->config->set_item('language', $lang);
redirect('');
}
it works and changes correctly both the session data and the config file. The problem is that I'm loading the language in a controller, this way:
$this->lang->load('my');
and it just retrieve the language specified in the config file, without considering the language I set with my function. So, after a while I figured it out I can load the language this way:
$this->lang->load('my', $this->session->userdata('language'));
and it works. The problem is that I can't put this line in the autoload file. Actually, I can't put $this->lang->load('my'); either, since it says there's no DB object.
So, following this logic, is there another better approach to change language without messing with the url?