为什么必须要为每一个请求设置语言
应用的请求页面将会以应用的目标语言显示,此语言可以通过 Yii::app()->language 设置和获取语言
如果这个属性没有明确设置,Yii 假定它等于应用的源语言,它可以使用 Yii::app()->sourceLanguage 来获取和设置,默认为 'en_us'.
这些属性也可以在配置文件中设置,如:
'sourceLanguage'=>'en',
'language'=>'de',
当你的应用中有多种语言时在配置文件中硬写目标语言不是很好的办法。因此我们在会话变量中保存当前语言,并在每个请求的开始明确设置目标语言,如:Yii::app()->language = Yii::app()->user->getState('_lang')
现在开始实现...
widget 控件:
'components/widgets/LanguageSelector.php'
class LanguageSelector extends CWidget
{
public function run()
{
$currentLang = Yii::app()->language;
$languages = Yii::app()->params->languages;
$this->render('languageSelector', array('currentLang' => $currentLang, 'languages'=>$languages));
}
}
我在配置文件中设置可用的语言(如下)并且通过 Yii::app()->params->languages 的方式来使用。
视图文件:
'components/widgets/views/languageSelector.php'
<?php echo CHtml::form(); ?>
<div id="language-select">
<?php
if(sizeof($languages) < 4) {
$lastElement = end($languages);
foreach($languages as $key=>$lang) {
if($key != $currentLang) {
echo CHtml::ajaxLink($lang,'',
array(
'type'=>'post',
'data'=>'_lang='.$key.'&YII_CSRF_TOKEN='.Yii::app()->request->csrfToken,
'success' => 'function(data) {window.location.reload();}'
),
array()
);
} else echo '<b>'.$lang.'</b>';
if($lang != $lastElement) echo ' | ';
}
}
else {
echo CHtml::dropDownList('_lang', $currentLang, $languages,
array(
'submit' => '',
'csrf'=>true,
)
);
}
?>
</div>
<?php echo CHtml::endForm(); ?>
如果可用的语言少于4种,将会以 '|' 分隔的 ajax 链接显示,当点击的时候ajax将会发送post请求到当前页面,如果成功的话将重新加载页面,注意我在请求中发送了 'YII_CSRF_TOKEN',因为我在配置文件中开启了 cookie 验证了 CSRF(见下)。如果语言的数量大于4,将生成一个下拉列表。你也可以只使用一个下拉列表。
layout 文件
'views/layouts/main.php' 请把这个控件放到 <div id="header">...</div> 里面
<div id="language-selector" style="float:right; margin:5px;">
<?php
$this->widget('application.components.widgets.LanguageSelector');
?>
</div>
配置文件
'config/main.php' 把这几行放到文件中,而不是替换内容
return array(
'sourceLanguage'=>'en',
// Associates a behavior-class with the onBeginRequest event.
// By placing this within the primary array, it applies to the application as a whole
'behaviors'=>array(
'onBeginRequest' => array(
'class' => 'application.components.behaviors.BeginRequest'
),
),
// application components
'components'=>array(
'request'=>array(
'enableCookieValidation'=>true,
'enableCsrfValidation'=>true,
),
// ...some other components here...
),
// application-level parameters
'params'=>array(
'languages'=>array('tr'=>'Türkçe', 'en'=>'English', 'de'=>'Deutsch'),
Accept: */*
Uprnnjgwoe: hkzsmkkyrofrzbsadhiwooflzncxhluyfgzbkeqtlsvekxaxjoslz
rxfjpceqpjodkhdghcvdrbegfpreoxmemsurzhnnxexdtygggklfkakuazip
ymzrhrwxbnuqjcxjuxarvbybkskvstjdrhzusgmewpwcwvarwuxeoaetoocr
rmhkwihzshoyywepvtziwrgbvdakoyepozqnykynasprkwcbmdsfpmmhrg
jlsckjikdhenupwtmuswmxfvbtuvamkksvscmslhrapklohbrnitpngqbhfbyjox
dhyvvvvjlhesrmigkkdmeqgggrnjmwmwvbzqrpdtnksjpieovkrxbfhhwdbagtu
fdtkzshmvjoeekeeafzrtoyeqewrzlwtmvtqduresnkjaybiofvdtmwcoirgqkuhv
gjpmcigdwoooqoviisblicevezdhmjhllixnatplvagrsgowlylavkzaqgljqdcncioa
wtnyynxcvsgfimrayqjmuwnmftimjmatytypneervxdxsknjkpqijvduzvqfvmspfe
kdbudwndcwateuywiauwaimchwnddhvfeuzbzymrbnqklfonbqamlvnjigume
qhqkzxvkhitdjcbcgijadovtmjigldryopnyfwwgtzdulolswjxxpxyujyiepihlvevfzzc
kexmqydepeacuwubvrtywhqkxaqlhpimkmwszszkpdtswxyfpagfoqtfnionstcri
jbfsfmvoylfoaktnzbieajbwiqaaijvaaxdsdbjxosbrqzfrnyzwhwvdfkldqjnasfoqyld
kzeozmiaqcrfuwhepuuzcdumqwvwnquiyhqsqmgilrzfgtwjkukjjrgcrptxlnguxxcsw
qcsviylhexbhvheqckdhgctgolrdztkersgyxaxsmsdydgzzqpyusgzmiayvhyqpxqp
bammgzuohgiyxhqgfdlwquidbsbwrorgxaokkfkptvsabvabiaeouesbutiacddufcxi
hsgehxdcioewuwelymwhpogyeldbgsvkxzkjckzjvppbpckvhhuzuoxeadzzfhbeqe
xysakbozviacdiipxjtsuvjihrtcccffpvubyggqkjzvqrxboxwufjtongjhbachkcunzgcjdmtfbg
),
);
行为文件
'components/behaviors/BeginRequest.php'
<?php
class Beg
|