Search code examples
phphtmlcodeigniterlocalizationlang

How to change the language in run time in Codeigniter?


I would like to send newsletter based on the user selected language preference.

And the preference is stored at database and can get correctly.

This is the extended controller:

class Front_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();

        if ($this->session->userdata('site_lang') == false) {
            $this->session->set_userdata('site_lang', "english");

            if ($this->agent->is_referral()) {
                redirect($this->agent->referrer());
            }
        }

        //set language
        $lang = $this->session->userdata('site_lang');
        $this->config->set_item('language', $lang);
        $this->lang->load("site", $lang);
}

This is the API for sending email

class Purchase extends Front_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function test() {
        $user_id = $this->input->post('user_id');
        $user = $this->User_model->get_user(array("id" => $user_id));
        //correctly get the perference

        //need the code to change language

        echo lang("hello"); //the "hello" should be based on the language preference
    }

});

I have try set the config inside test() but it seems the language is not changed.

Thanks a lot


Solution

  • I don't know what exactly you know but you get one possible solution by ci_i18n_library.zip

    Visit this site for more details http://jeromejaglale.com/doc/php/codeigniter_i18n enter image description here