原创作者: vb2005xu   阅读:841次   评论:0条   更新时间:2011-05-26    
<?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);
	}
	
}

 

评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics