Yii框架的I18n基础知识

Era*_*ray 6 php internationalization yii

Yii的I18n主题对我来说还不够.

我的来源是土耳其语,目标语言是英语(例如)

我的测试控制器的索引操作:

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

这是我的查看文件的内容:

echo Yii::t('test', 'Deneme');
Run Code Online (Sandbox Code Playgroud)

最后,这是我的protected/messages/en/test.php文件的内容:

return array(
    'Deneme' => 'Example',
);
Run Code Online (Sandbox Code Playgroud)

一切都好,它正在返回示例.但正如您所看到的,我正在我的索引操作上手动设置语言.我怎么能自动完成?我必须添加Yii :: app() - > language ='en'; 一切行动?你如何在你的项目中使用l18n?

注意:我是Yii和l18n noob,所以请逐步描述.

谢谢.

Nei*_*gan 5

你应该设置目标语言 CWebApplication:beginRequest()

protected/config/main.php,添加:

'onBeginRequest' => array('MyApp', 'beginRequest')
Run Code Online (Sandbox Code Playgroud)

在protected/components中,创建一个文件MyApp.php,然后添加以下类:

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

记得声明beginRequest()static,或者你会遇到这样的错误:https:
//github.com/yiisoft/yii/issues/794