简体   繁体   English

如何在运行时在 Codeigniter 中更改语言?

[英]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这是用于发送电子邮件的 API

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.我尝试在 test() 中设置配置,但似乎语言没有改变。

Thanks a lot非常感谢

I don't know what exactly you know but you get one possible solution by ci_i18n_library.zip我不知道您到底知道什么,但是您可以通过ci_i18n_library.zip获得一种可能的解决方案

Visit this site for more details http://jeromejaglale.com/doc/php/codeigniter_i18n访问此网站了解更多详情http://jeromejaglale.com/doc/php/codeigniter_i18n 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM