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?

6
  • why don't you load db and session like this in autoload libraries section $autoload['libraries'] = array('database','session'); Commented Oct 10, 2011 at 11:37
  • @shikhar: I already do that. I think that it actually loads them after anyway Commented Oct 10, 2011 at 11:51
  • okay .. how about putting it in a hook !! Commented Oct 10, 2011 at 11:55
  • possible duplicate of the best way to make codeigniter website multi-language. calling from lang arrays depends on lang session? Commented Oct 10, 2011 at 15:04
  • what do you suggest shall I put in a hook? Commented Oct 10, 2011 at 15:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.