用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3496|回复: 1
收起左侧

[优化] 关于CI2.0 i18n的一点建议

[复制链接]
发表于 2011-8-2 19:51:56 | 显示全部楼层 |阅读模式
本帖最后由 大道达人 于 2011-8-2 23:52 编辑

假设语言配置如下:
$lang['c_welcome']['c_hello']    = '欢迎您';//echo $this->lang->line('c_welcome');得到 Array
对CI_LANG修改了下,可以支持数组读取
Lang.php
        function line($line = '' ,$index = NULL)
        {
        
                $line = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];

                // Because killer robots like unicorns!
                if ($line === FALSE)
                {
                        log_message('error', 'Could not find the language line "'.$line.'"');
                }
                else
                {
                        // Keys Value Check
                        if ($index !== NULL && is_array($line)  && isset($line[$index]))
                        {
                                 $line = $line[$index];                        
                        }
                        //TODO debug Code                  
                }
                return $line;
        }
//调用直接 $this->lang->line('c_welcome','c_hello');
同时需要修改
language_helper.php
if ( ! function_exists('lang'))
{
        function lang($line, $id = '', $index = NULL)
        {

                $CI =& get_instance();
                if($index === NULL)
                        $line = $CI->lang->line($line);
                else
                        $line = $CI->lang->line($line,$index);//exist key
                if ($id != '')
                {
        
                        $line = '<label for="'.$id.'">'.$line."</label>";
                }

                return $line;
        }
}
发表于 2012-7-2 17:28:25 | 显示全部楼层
这方法可以的 不过不算是最好的 因为你这修改了CI的核心类  正确 的应该扩展核心类的方法或重写核心类的方法 这样方便以后升级

本版积分规则