I18n Basics for Yii Framework

Yii I18n topic is not enough for me.

My source language is Turkish, the target language is English (for example)

Action of my test controller:

public function actionIndex() { Yii::app()->language='en'; $this->render("index"); } 

This is my view file contents:

 echo Yii::t('test', 'Deneme'); 

And finally, this is my protected / message file / en / test.php:

 return array( 'Deneme' => 'Example', ); 

Everything is in order, it returns an Example . But, as you can see, I set the language manually in my indexed action . How can I do this automatically? Should I add Yii :: app () → language = 'en'; to all actions? How do you use l18n in your projects?

Note: I am Yii and l18n noob, so please describe step by step.

Thanks.

+6
source share
3 answers

You must set the target language in CWebApplication:beginRequest()

in protected/config/main.php , add:

 'onBeginRequest' => array('MyApp', 'beginRequest') 

In the protected / components, create the MyApp.php file and add this class:

 class MyApp { public static function beginRequest(CEvent $event) { //set your language, theme, etc here } } 

Remember to declare beginRequest() as static , or you will encounter such errors:
https://github.com/yiisoft/yii/issues/794

+5
source

it's pretty simple. You do all the language translations, as you said. Then, in the parent controller, in the init method, yo can check the desired language and set the current language. Thus, you do not need to do this in every action just once.

+3
source

there is an article in Yii's textbooks that explained this very well. so you have 3 files: one, your language selector, one, a language selection widget, and the other is the behavior for processing your language selection file. read here and use it ... Control (target) language in multilingual applications + Language selection widget (i18n)

+1
source

Source: https://habr.com/ru/post/895284/


All Articles