1

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?

Carlo
  • 4,016
  • 7
  • 44
  • 65
  • why don't you load db and session like this in autoload libraries section $autoload['libraries'] = array('database','session'); – shikhar Oct 10 '11 at 11:37
  • @shikhar: I already do that. I think that it actually loads them after anyway – Carlo Oct 10 '11 at 11:51
  • okay .. how about putting it in a hook !! – shikhar Oct 10 '11 at 11:55
  • possible duplicate of [the best way to make codeigniter website multi-language. calling from lang arrays depends on lang session?](http://stackoverflow.com/questions/1328420/the-best-way-to-make-codeigniter-website-multi-language-calling-from-lang-arrays) – Johan Oct 10 '11 at 15:04
  • what do you suggest shall I put in a hook? – Carlo Oct 10 '11 at 15:13
  • @Johan: yes, I saw that question, but it doesn't seem that the answer is complete. It just suggests to use the session data, but doesn't explain how – Carlo Oct 10 '11 at 15:16

1 Answers1

1

You could try extending the controller class.

class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->lang->load('my', $this->session->userdata('language'));
    }
}

Then instead of extending the CI_Controller class from your controllers extend from your custom class. In this way you are ensuring that each time a controller is instantiated it is loading your language.

See the User Guide Creating Core Classes for more information.

JD Guzman
  • 368
  • 2
  • 7