关键字:php smarty 3 多语言 版本 标签 静态化 tag multilanguage nginx
--------------------------------------------------------------------------------------------------------------------------------
网络上可以查到在smarty 2中实现自动替换多语言标签的方法,但是smarty3中有了很多变化。
所谓的静态化,就是在编译期将标签替换进编译后的php程序,这样避免了很多echo $L['xxx']的东西。
总的思路是:重载Smarty类,在编译之前,根据当前选择的语言,将里面的标签全部替换掉。
因为Smarty编译后是缓存的,这个过程不是每次都做,所以这个工作很值得,
编译后的php不需要访问标签文件,也不用在html中间夹杂很多echo语句,可以部分的提高性能。
下面的方法既是模板多语言化的一种方法,也可以静态化标签。
好处就不多说了,看方法:
//-ExSmarty.php--------------------------------------------------------------------------------------------
require_once ("Smarty.class.php");
function _static_multi_language($tpl_source, &$smarty) {
$tags = include($smarty->root.'lang/'.$smarty->lang.'/tags.php');
if(is_array($tags)) {
$GLOBALS['tags'] = $tags;
//用正则替换,将标签内容全部替换掉
//假设你的标签格式是这样的<div>[#your_tag_name]</div>
return preg_replace_callback('/\[#(.+)\]/i', '_compile_lang_tag', $tpl_source);
}
return ''; //失败则返回空,可以根据需要返回其他提示信息
}
function _compile_lang_tag($key) {
//注意这里是$key[1],不是$key或$key[0]
return $GLOBALS['tags'][$key[1]];
}
class ExSmarty extends Smarty{
public $lang = 'zh';
public $home = '/'; //在测试时home可能不是web的根目录
public static $root = $_SERVER['DOCUMENT_ROOT'];
function __construct () {
parent::__construct();
//通过你的方法决定当前的语言是什么,比如cookie、参数、HTTP_ACCEPT_LANGUAGE 等
$this->lang = 'xx';
//其他smarty的初始化工作,比如一些路径的设置等
$this->template_dir = 'xxxx';
$this->compile_dir = self::$root.'inc/smarty/templates_c/';
$this->config_dir = 'yyy';
$this->cache_dir = 'zzz';
/*如果设为true,会将整个页面静态化,输出时将不使用编译后的php动态生成,而是上次php输出的html
模板中不希望cache的地方需要加{nocache}...{/nocache}
设置$this->cache_lifetime(秒)控制缓存时间 */
$this->caching = false;
//$smarty->debugging = true;
......
$this->registerFilter(Smarty::FILTER_PRE, '_static_multi_language');
}
//重载fetch函数,根据语言不通,分配不通的compile id,保证将相同模板、不同语言的编译缓存分开
function fetch($file, $_smarty_cache_id = null, $_smarty_compile_id = null, $_smarty_parent = null, $_smarty_display = false) {
$id = $this->lang.'_'.$file;
//注意最后一个参数是true,表示要预处理
return parent::fetch($file, $id, $id, $_smarty_parent, true);
}
//与fetch类似,不同语言的编译缓存分开判断
function is_cached($file, $cache_id = null, $compile_id = null) {
if (!$this->caching) {
return false;
}
if (!isset($compile_id)) {
$id = $this->lang.'_'.$file;
return parent::is_cached($file, $id, $id);
}
}
......其他的方法
}
//-DOCUMENT_ROOT/lang/zh/tags.php--------------------------------------------------------------------
return array(
'your_tag_name1' => '你好',
'your_tag_name2' => 'flyinmind'
);
//-DOCUMENT_ROOT/lang/en/tags.php----------------------------------------------------------------------
return array(
'your_tag_name1' => 'Hello',
'your_tag_name2' => 'flyinmind'
);
//---模板文件.tpl---------------------------------------------------------------------------------------------------------
<html><body>
<div>[#your_tag_name1] [#your_tag_name2]<div>
</body></html>
//----------------------------------------------------------------------------------------------------------------------------
按照上面的方法,应该可以实现静态化多语言标签了。
如果还想更快更好,可以使用eaccelerator,很优秀,lampp中自带了,在php.ini增加相应配置即可。
如果对并发要求很高,建议使用nginx+php(fastcgi),具体怎样配置,可以参考这里
http://blog.s135.com/nginx_php_v6/
我在自己的环境用webbench简单测试了一下,比apache的处理能力高出2-3倍