codeigniter源代码分析 - 语言包类 Lang.php
2014-04-29 09:43
274 查看
语言包主要是加载语言文件
注释源码如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Lang {
var $language = array();
var $is_loaded = array();
function __construct()
{
log_message('debug', "Language Class Initialized");
}
function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
{
// 语言文件
$langfile = str_replace('.php', '', $langfile);
// 是否需要后缀
if ($add_suffix == TRUE)
{
$langfile = str_replace('_lang.', '', $langfile).'_lang';
}
$langfile .= '.php';
// 已经加载
if (in_array($langfile, $this->is_loaded, TRUE))
{
return;
}
$config =& get_config();
// 配置文件中指定的语言文件 如果没有默认为english
if ($idiom == '')
{
$deft_lang = ( ! isset($config['language'])) ? 'english' : $config['language'];
$idiom = ($deft_lang == '') ? 'english' : $deft_lang;
}
// Determine where the language file is and load it
if ($alt_path != '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
{
include($alt_path.'language/'.$idiom.'/'.$langfile);
}
else
{
$found = FALSE;
// 这个get_instance函数是在CodeIgniter.php文件中定义的
// 在指定路径找不到的情况下 看看路径包里面是否有文件
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
{
if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
{
include($package_path.'language/'.$idiom.'/'.$langfile);
$found = TRUE;
break;
}
}
if ($found !== TRUE)
{
show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
}
}
if ( ! isset($lang))
{
log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
return;
}
if ($return == TRUE)
{
return $lang;
}
// 记录文件已经加载
$this->is_loaded[] = $langfile;
$this->language = array_merge($this->language, $lang);
unset($lang);
log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
return TRUE;
}
function line($line = '')
{
$value = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
// Because killer robots like unicorns!
if ($value === FALSE)
{
log_message('error', 'Could not find the language line "'.$line.'"');
}
return $value;
}
}
Code Tips:
文件单次加载、后缀处理、配置文件、路径包文件检查、内容合并
注释源码如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Lang {
var $language = array();
var $is_loaded = array();
function __construct()
{
log_message('debug', "Language Class Initialized");
}
function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
{
// 语言文件
$langfile = str_replace('.php', '', $langfile);
// 是否需要后缀
if ($add_suffix == TRUE)
{
$langfile = str_replace('_lang.', '', $langfile).'_lang';
}
$langfile .= '.php';
// 已经加载
if (in_array($langfile, $this->is_loaded, TRUE))
{
return;
}
$config =& get_config();
// 配置文件中指定的语言文件 如果没有默认为english
if ($idiom == '')
{
$deft_lang = ( ! isset($config['language'])) ? 'english' : $config['language'];
$idiom = ($deft_lang == '') ? 'english' : $deft_lang;
}
// Determine where the language file is and load it
if ($alt_path != '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
{
include($alt_path.'language/'.$idiom.'/'.$langfile);
}
else
{
$found = FALSE;
// 这个get_instance函数是在CodeIgniter.php文件中定义的
// 在指定路径找不到的情况下 看看路径包里面是否有文件
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
{
if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
{
include($package_path.'language/'.$idiom.'/'.$langfile);
$found = TRUE;
break;
}
}
if ($found !== TRUE)
{
show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
}
}
if ( ! isset($lang))
{
log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
return;
}
if ($return == TRUE)
{
return $lang;
}
// 记录文件已经加载
$this->is_loaded[] = $langfile;
$this->language = array_merge($this->language, $lang);
unset($lang);
log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
return TRUE;
}
function line($line = '')
{
$value = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
// Because killer robots like unicorns!
if ($value === FALSE)
{
log_message('error', 'Could not find the language line "'.$line.'"');
}
return $value;
}
}
Code Tips:
文件单次加载、后缀处理、配置文件、路径包文件检查、内容合并
相关文章推荐
- codeigniter源代码分析之CI系统运行流程 CodeIgniter.php
- codeigniter源代码分析之输入类 Input.php
- codeigniter源代码分析之文件加载类 Loader.php
- codeigniter源代码分析 - 钩子类 Hooks.php
- codeigniter源代码分析 -安全类 Security.php
- codeigniter源代码分析 - 性能分析类 Benchmark.php
- codeigniter源代码分析 - 编码类 Utf8.php
- codeigniter源代码分析之路由处理类 Router.php
- codeigniter源代码分析 - 控制器类 Controller.php
- codeigniter源代码分析之入口文件 index.php
- codeigniter源代码分析 - 模型类 Model.php
- Twitter的分布式自增ID算法Snowflake实现分析及其Java、Php和Python版
- PHP程序漏洞产生的原因分析与防范方法说明
- PHP Yii Framework --组件[CComponent]分析
- Oracle9i 字符集与NLS_LANG搭配测试兼乱码问题分析
- PHP无法访问远程mysql的问题分析及解决
- php数组源码分析
- PHP中new static()与new self()的区别异同分析
- 【discuzX2】/source/class/class_core.php文件中核心基础类库中discuz_core类分析
- php性能分析工具工具xhprof的安装与使用