zxhralf 发表于 2011-10-12 14:02:17

首先我使用的是CI 2.0.3
上面朋友的方法有问题是每次都要把很多class重复load,浪费了资源。
我的方法是在程序早期(index.php)就使用系统定义的变量$assign_to_config来设定语言,来源是cookie数据(当然您也可以使用其他的方式存放)。提供一个方法修改cookie即可。看框架注释来说,使用$assign_to_config也是CI的设计者所推崇的


/*
* -------------------------------------------------------------------
*CUSTOM CONFIG VALUES
* -------------------------------------------------------------------
*
* The $assign_to_config array below will be passed dynamically to the
* config class when initialized. This allows you to set custom config
* items or override any default config values found in the config.php file.
* This can be handy as it permits you to share one application between
* multiple front controller files, with each file containing different
* config values.
*
* Un-comment the $assign_to_config array below to use this feature
*
*/
// $assign_to_config['name_of_config_item'] = 'value of config item';
//add by xxx
if (isset($_COOKIE["test_user_lang"])) {
        switch ($_COOKIE["test_user_lang"]) {
                case "chinese_simplified":
                case "japanese":
                case "english":
                        $assign_to_config['language'] = $_COOKIE["test_user_lang"];
                        break;
                default:
                        $assign_to_config['language'] = "chinese_simplified";                       
        }
} else {
        $assign_to_config['language'] = "chinese_simplified";
}


这是普通的controller和function
要改变设定。要改变时候直接访问诸如ht-tp;和谐/xxx.com/index.php/login/switchlang/japanese

class Login extends MY_Controller {
public function switchlang(¥$ang) {
                //$lang = $this->input->post("lang");
                $cookie = array('name'   => 'test_user_lang',
                   'value'=> $lang,
                   'expire' => '86400',
                   'path'   => '/'
                );
                //setcookie("test_user_lang",$lang,time()+88888,"/");
                $this->input->set_cookie($cookie);
               
                $this->load->helper('url');
                redirect("/login/index");
        }
}

嘟嘟返利 发表于 2011-10-13 21:04:25

最好的多语言方案是XML,PHP数组无法处理复杂的多语言

小蜗牛 发表于 2011-10-14 14:51:39

用session来记录当前的语言种类,不好,不利于搜索引擎优化。
页: 1 [2]
查看完整版本: [1.5.4]CI 多语言解决方案【原创】