<?php
/**
* 国际化多语言组件
*/
class xser_languages {
private $_dict ,$_dict_files ;
public function __construct(){
$this->_dict = array();
$this->_dict_files = array();
}
public function load($dict,$language = ''){
$language = $this->get_language($language);
$abs_fpath = "{$language}/{$dict}" ;
$fdict = xser::ini('application_dir') . "/language/{$abs_fpath}";
// 预防重复加载
if (in_array($abs_fpath,$this->_dict_files)) return ;
if (file_exists($fdict) && is_readable($fdict)){
$dict = include($fdict) ;
if (!is_array($dict)) $dict = array();
if (isset($this->_dict[$language]))
$this->_dict[$language] = array_merge($this->_dict[$language],$dict);
else
$this->_dict[$language] = $dict;
// 将已经加载的字典文件储存
$this->_dict_files[] = $abs_fpath ;
return ;
}
throw new Exception("cannot read language dict file:{$abs_fpath}");
}
private function get_language($language = ''){
if (!xser::varify_provider()->not_null_string($language))
$language = xser::ini('i18n_default_language') ;
return strtolower($language) ;
}
public function get($key, $language=''){
$language = $this->get_language($language);
if (isset($this->_dict[$language]))
if (isset($this->_dict[$language][$key]))
return $this->_dict[$language][$key] ;
return null ;
}
}
if (xser::ini('i18n_multi_languages')) // 启用多语言支持
{
// 加载多语言引擎类
if (!xser::is_register('i18n_multi_languages')){
xser::loadlibrary('languages','xser');
xser::register(new xser_languages(),'i18n_multi_languages');
}
// 获取翻译
function xser_translate($key ,$language=''){
return xser::singleton_alias('i18n_multi_languages')->get($key, $language);
}
// 载入语言字典文件
function xser_load_language($dict ,$language='' ,$extname='.php'){
return xser::singleton_alias('i18n_multi_languages')->load("{$dict}{$extname}", $language);
}
}
新版本:
<?php
/**
* 国际化多语言组件
* // 获取翻译 xser::translate('ui/admin/title')
*/
class xser_languages {
const g_conf_key = '__xser#i18n_multi_languages' ;
protected $_dict_files ;
public function __construct(){
xser::replaceIni(g_conf_key,array());
$this->_dict_files = array();
}
public function load($dict_name,$language='',$fdict_extname='.php'){
$language = $this->get_language($language);
$abs_fpath = "{$language}/{$dict_name}{$fdict_extname}" ;
$fdict = xser::ini('application_dir') . "/language/{$abs_fpath}";
// 预防重复加载
if (in_array($abs_fpath,$this->_dict_files)) return ;
if (file_exists($fdict) && is_readable($fdict)){
$dict = include($fdict) ;
if (!is_array($dict)) $dict = array();
// 定义数据结构
xser::changeIni(g_conf_key,array(
$language => array(
$dict_name => $dict
)
));
// 将已经加载的字典文件储存
$this->_dict_files[] = $abs_fpath ;
return ;
}
throw new Exception("cannot read language dict file:{$abs_fpath}");
}
protected function get_language($language = ''){
if (!xser::varify_provider()->not_null_string($language))
$language = xser::ini('i18n_default_language') ;
return strtolower($language) ;
}
// 只接受字符串格式的key,对于层次化的key,可以在 $key 中使用/符号来指定
public function get($key, $language=''){
if (!xser::varify_provider()->not_null_string($key))
return null ;
$language = $this->get_language($language);
$_key = g_conf_key . "/{$language}/{$key}" ;
$value = xser::ini($_key,'__xser#null');
if ($value === '__xser#null')
return htmlspecialchars("{{$key}}");
return $value ;
}
}
language/utf8-zh/ui.php
<?php
/**
* 中文版 国际化资源文件
*/
// xser::translate('ui/admin/title');
return array(
'admin' => array(
'title' => 'Time In China 后台管理系统' ,
'welmsg' => ' 您好,感谢登陆使用!',
'logout' => '您已经成功退出了登录。',
'invalid' => array(
'imgcode' => '您输入的验证码无效,或者验证码已经过期。请重新登录。',
'username' => '您输入的用户名不存在,请检查后重新登录。',
'password' => '您输入的密码不正确,请检查后重新登录。',
)
) ,
'site' => 'http://www.timeinchina.com' ,
'version' => 'Copyright © 2009-2011 www.timeinchina.com' ,
'username' => '管理员:',
'password' => '密码:',
'imgcode' => '验证码:',
'submit' => ' 登 陆 ',
'cancel' => ' 取 消 ',
'languages' => '界面语言:',
);