In the case of a multilingual application, people may think that it is a reasonable way to save the preferred language in the session variables. Then, when every page is requested, check this session variable, the language is displayed on the specified page.
In the case of a multilingual application, people may think that it is a reasonable way to save the preferred language in the session variables. Then, when every page is requested, check this session variable, the language is displayed on the specified page. This tutorial shows the Yii method to solve this problem. We implement an event handler for the onBeginRequest event. this event handler is called at the beginning of each request, so it is a good place to check whether the language is provided (post, session variables or coookie) and set the corresponding language, we also implement a simple language selection component, which can display the language options in an ajax link or drop-down list.
You can also use this to replace.
Why must I set the language for each request?
- The application request page is displayed in the target language of the application. you can set and obtain the language through Yii: app ()-> language.
- If this attribute is not explicitly set, Yii assumes that it is equal to the source language of the application. it can be obtained and set using Yii: app ()-> sourceLanguage. the default value is 'en _ us '.
- These attributes can also be set in the configuration file, for example:
'sourceLanguage'=>'en', 'language'=>'de',
- When your application has multiple languages, it is not a good way to hard write the target language in the configuration file. Therefore, we save the current language in the session variable and specify the target language at the beginning of each request, for example, Yii: app ()-> language = Yii: app () -> user-> getState ('_ lang ')
Implementation now...
Widget control:
'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)); } }
In the configuration file, I set available languages (as follows) and use Yii: app ()-> params-> ages.
View File:
'Components/widgets/views/languageSelector. php'
$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 '
'.$lang.''; if($lang != $lastElement) echo ' | '; } } else { echo CHtml::dropDownList('_lang', $currentLang, $languages, array( 'submit' => '', 'csrf'=>true, ) ); } ?>
If there are fewer than four available languages, the ajax links separated by '|' are displayed. when you click it, ajax will send a post request to the current page, if the request succeeds, the page will be reloaded. Note that I sent the 'yii _ csrf_token' in the request because I enabled cookie verification in the configuration file (see below ). If the number of languages is greater than 4, a drop-down list is generated. You can also use only one drop-down list.
Layout file
'Views/layouts/main. php' put this control in...
widget('application.components.widgets.LanguageSelector'); ?>
Configuration file
'Config/main. php'Put these lines in the file, instead of replacing the content.
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'), ), );
Behavior file
'Components/behaviors/BeginRequest. php'
attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest')); } public function handleBeginRequest($event) { $app = Yii::app(); $user = $app->user; if (isset($_POST['_lang'])) { $app->language = $_POST['_lang']; $app->user->setState('_lang', $_POST['_lang']); $cookie = new CHttpCookie('_lang', $_POST['_lang']); $cookie->expire = time() + (60*60*24*365); // (1 year) Yii::app()->request->cookies['_lang'] = $cookie; } else if ($app->user->hasState('_lang')) $app->language = $app->user->getState('_lang'); else if(isset(Yii::app()->request->cookies['_lang'])) $app->language = Yii::app()->request->cookies['_lang']->value; } }
That's all.
If you do not know, contact me for errors or incomplete information.