Yii 框架的 I18n 基础知识

发布于 2024-11-30 06:43:15 字数 748 浏览 5 评论 0原文

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

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

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

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

这是我的视图文件的内容:

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

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

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

一切正常,它正在返回 Example 。但正如您所看到的,我正在我的索引操作上手动设置语言。我怎样才能自动完成呢?我必须将 Yii::app()->language='en'; 添加到所有操作中吗?您如何在项目中使用本地化?

注意:我是 Yii 和 l18n 菜鸟,所以请逐步描述。

谢谢。

Yii's I18n topic isn't enough for me.

My source lang is Turkish , target lang is English (for example)

My test controller's index action :

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

This is my view file's content :

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

And lastly, this is my protected/messages/en/test.php file's content:

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

Everything OK, it's returning Example . But as you can see, i'm setting language manually on my index action. How can i do it automatically ? Must i add Yii::app()->language='en'; to all actions? How you are using l18n on your projects ?

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷遇一生 2024-12-07 06:43:15

CWebApplication:beginRequest()中设置目标语言

您应该在protected/config/main.php中的

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

,添加:在protected/components中,创建一个文件MyApp.php php,并添加这个类:

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

记住将beginRequest()声明为static,否则你会遇到这样的错误:
https://github.com/yiisoft/yii/issues/794

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

in protected/config/main.php, add:

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

In protected/components, create a file MyApp.php, 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 errors like this:
https://github.com/yiisoft/yii/issues/794

孤独难免 2024-12-07 06:43:15

这相当简单。您按照您所说的进行所有语言翻译。然后,在父控制器的 init 方法中,您可以检查所需的语言并设置当前语言。这样,您不必在每个操作中都这样做,只需一次。

it's fairly simple. You do all 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. That way, you don't have to do that in every action, just once.

缘字诀 2024-12-07 06:43:15

Yii的教程中有一篇文章解释得很好。
这样你就有了 3 个文件:一个是你的语言选择器,一个是语言选择器的小部件,一个是用于处理语言选择器文件的行为。
阅读此处并使用它...
管理多语言应用程序中的(目标)语言 + 语言选择器小部件 (i18n)

in the Yii's tutorials there is an article that has explained it very good.
in this way you have 3 file: one, your language selector, one, language selector's widget and one is an behavior for handling your language selector file.
read here and use it...
Manage (Target) Language in Multilingual Applications + A Language Selector Widget (i18n)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文