用户
 找回密码
 入住 CI 中国社区
搜索
12
返回列表 发新帖
楼主: lony
收起左侧

[程序 App] [1.5.4]CI 多语言解决方案【原创】

  [复制链接]
发表于 2011-10-12 14:02:17 | 显示全部楼层
首先我使用的是CI 2.0.3  
上面朋友的方法有问题是每次都要把很多class重复load,浪费了资源。
我的方法是在程序早期(index.php)就使用系统定义的变量$assign_to_config来设定语言,来源是cookie数据(当然您也可以使用其他的方式存放)。提供一个方法修改cookie即可。看框架注释来说,使用$assign_to_config也是CI的设计者所推崇的

PHP复制代码
 
/*
 * -------------------------------------------------------------------
 *  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
PHP复制代码
 
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来记录当前的语言种类,不好,不利于搜索引擎优化。

本版积分规则