我有一个观点:
public function index()
{
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if ($lang == "fr"){
$this->smarty->display('excel.tpl');
}
else{
$this->smarty->display('excel_eng.tpl');
}
}
我的模板:
<div class="fullscreen background" id="landing-page-image">
{include 'components/menu.tpl'}
</div>
我想根据语言
替换组件怎么做?
答案 0 :(得分:1)
你可以这样做:
excel.tpl:
<div class="fullscreen background" id="landing-page-image">
{include 'components/menu.tpl'}
</div>
excel_eng.tpl:
<div class="fullscreen background" id="landing-page-image">
{include 'components/menu_eng.tpl'}
</div>
编辑
PHP代码:
...
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$smarty->assign('lang' , $lang); // passing lang to templates
....
HTML code:
<div class="fullscreen background" id="landing-page-image">
{include 'components/menu_$lang.tpl'}
</div>
答案 1 :(得分:1)
1。此处的第一个选项是加载语言变量并为它们提供通用模板:
$lang = 'en';//your default language
$available_langs = array('en', 'fr'); //your set of languages
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if (in_array($browser_lang, $available_langs)) {
$lang = $browser_lang; //set language according to browser
}
//assumed you keep your language variables in /app/language/lang_en.php
require_once(APPPATH.'language/lang_'.$lang.'.php');
$this->smarty->assign('lang_vars', $lang_vars);
lang_en.php的例子:
<?php
$lang_vars = array(
'some_var_1'=>'Some string',
'some_var_2'=>'Another string',
);
如何在.tpl文件中使用示例:
<div>
{$lang_vars.some_var_1}
</div>
2。此处的另一个选项是在检测到语言时根据相同的方法加载不同的模板:
...
$this->smarty->display('some_template_'.$lang.'.tpl');
但这被认为是不好的做法,因为你需要支持和更改2套模板。