大道达人 发表于 2011-8-2 19:51:56

关于CI2.0 i18n的一点建议

本帖最后由 大道达人 于 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;
      }
}

liangpz521 发表于 2012-7-2 17:28:25

这方法可以的 不过不算是最好的 因为你这修改了CI的核心类正确 的应该扩展核心类的方法或重写核心类的方法 这样方便以后升级
页: [1]
查看完整版本: 关于CI2.0 i18n的一点建议