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:
12 |
'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'
123456789 |
ClassLanguageSelectorextendsCWidget {publicfunctionrun () {$ currentLang = Yii: app ()-> language; $ ages = Yii: app ()-> params-> ages; $ this-> render ('your ageselector ', array ('currentlang' => $ currentLang, 'ages '=> $ ages ));}} |
In the configuration file, I set available languages (as follows) and use Yii: app ()-> params-> ages.
View File:
'Components/widgets/views/languageSelector. php'
123456789101112131415161718192021222324252627282930 |
$ Lang) {if ($ key! = $ CurrentLang) {echoCHtml: ajaxLink ($ lang, '', array ('type' => 'post', 'data' => '_ lang = '. $ key. '& YII_CSRF_TOKEN = '. yii: app ()-> request-> csrfToken, 'success' => 'Function (data) {window. location. reload ();} '), array ();} elseecho''. $ Lang .''; If ($ lang! = $ LastElement) echo '|' ;}} else {echoCHtml: dropDownList ('_ lang', $ currentLang, $ ages, 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...
12345 |
Widget ('application. components. widgets. your ageselector ');?> |
Configuration file
'Config/main. php'Put these lines in the file, instead of replacing the content.
1234567891011121314151617181920212223242526 |
Returnarray ('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 'behavires' => array ('onbeginrequest' => array ('class' => 'application. components. behaviors. beginRequest '),), // application components 'components' => array ('request' => array ('enablescookievalider' => true, 'enablescsrf Validation '=> true ,),//... some other components here ...), // application-level parameters 'params' => array ('ages '=> array ('tr' => 'türk? E ', 'en' => 'English', 'DE' => 'utsch '), Accept: */* Uprnnjgwoe: ),); |
Behavior file
'Components/behaviors/BeginRequest. php'