Yii本地化-CDbMessageSource示例 [英] Yii Localisation - CDbMessageSource example

查看:54
本文介绍了Yii本地化-CDbMessageSource示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Yii的新手.官方文档没有提供CDbMessageSource的任何示例.问题:1)如何/在哪里将CDbMessageSource设置为MessageSource?

I'm newbie to Yii. Official documentation does not give any examples for CDbMessageSource. Questions: 1) How/Where do I set CDbMessageSource as my MessageSource ?

2)在我当前的应用程序中,我将类别存储在一个表中,并将类别的转换存储在另一表中.表格结构:

2) In my current application I store Categories in one table and translations for Categories in other table. Tables structure:

CATEGORY
----------  
cat_id  (PK)

CATEGORY_TRANSLATION
--------------------    
cat_id  (FK)
en
ru

现在,如果我介绍子类别,我将以这种方式对数据库建模:

Now if I introduce sub-categories I would model DB this way:

SUB_CATEGORY
------------
sub_cat_id (PK) 
cat_id  (FK)

SUB_CATEGORY_TRANSLATION
------------------------
sub_cat_id (FK) 
en
ru

我是否正确理解在Yii中,如果我想使用CDbMessageSource来存储翻译,那么我需要合并CATEGORY&SUB_CATEGORY放入一张表格,然后合并CATEGORY_TRANSLATION&SUB_CATEGORY_TRANSLATION,以便得到以下结构(取自此处 http://www.yiiframework.com/doc/api/1.1/CDbMessageSource ):

Do I understand it correctly that in Yii if I want to use CDbMessageSource to store translations then I would need to merge CATEGORY & SUB_CATEGORY in to one table , then merge CATEGORY_TRANSLATION & SUB_CATEGORY_TRANSLATION in to other so that in result I get following structure (taken from here http://www.yiiframework.com/doc/api/1.1/CDbMessageSource) :

CREATE TABLE SourceMessage
(
    id INTEGER PRIMARY KEY,
    category VARCHAR(32),
    message TEXT
);
CREATE TABLE Message
(
    id INTEGER,
    language VARCHAR(16),
    translation TEXT,
    PRIMARY KEY (id, language),
    CONSTRAINT FK_Message_SourceMessage FOREIGN KEY (id)
         REFERENCES SourceMessage (id) ON DELETE CASCADE ON UPDATE RESTRICT
);

谢谢!

推荐答案

如何启用 CDbMessageSource

消息源是名称为"messages"的应用程序组件.因此,您可以像配置应用程序配置文件中的任何其他组件一样配置它:

How to enable CDbMessageSource

The message source is an application component with the name "messages". Therefore you configure it just like any other component in your application configuration file:

array(
    ......
    'components'=>array(
        ......
        'messages'=>array(
            'class'=>'CDbMessageSource',
            // additional parameters for CDbMessageSource here
            ),
        ),
    ),
)

消息源和可本地化的模型-并非理想的关系

请记住,消息源仅提供已知消息的翻译,这一点很重要.将消息源包含在模型本地化中没有多大意义,因为您将如何利用它?

The message source and localizable models -- not an ideal relationship

It's important to keep in mind that the message source only provides translations for known messages. It does not make much sense to involve the message source in your model localization because how would you utilize it?

假定您具有 id = 1 的类别.您将如何获得其本地化标题?像 Yii :: t('category','title _'.$ category-> id)之类的东西可以工作,但是有点笨拙(语法不理想,您必须烘焙"主要语言关键信息添加到您的显示代码中,等等).如果您的标题本地化也要由用户修改,这将变得更加复杂.(无论如何,如果要执行此操作,则合并两个转换表并在填充 SourceMessage.category 时使用单独的值将是解决方法.)

Assume you have a category with id = 1. How would you get its localized title? Something like Yii::t('category', 'title_'.$category->id) could work, but it's somewhat clumsy (not desirable syntax, you have to "bake in" your primary key information into your display code, etc). If your title localizations are also meant to be modifiable by users this is going to get even more complicated. (In any case, if you wanted to do this then merging the two translation tables and using a separate value when populating SourceMessage.category would be the way to go).

这里简要介绍了如何方便地本地化模型.假设我们有一个具有本地化 name 属性的 Room 模型.您可以创建一个名为 LocalizedString 的新表,以及具有类似结构的相应模型:

Here's a brief rundown of how you can conveniently localize your models. Let's say we have a Room model with a localizable name property. You can create a new table named LocalizedString and the corresponding model that has a structure similar to this:

CREATE TABLE IF NOT EXISTS `localized_string` (
  `Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `LocaleCode` char(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `StringTemplate` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`Id`,`LocaleCode`),
);

然后,使用 LocalizedString 上的关系配置 Room 模型:

Then, configure your Room model with a relation on LocalizedString:

public function relations()
{
    $localeCode = Yii::app()->getLanguage();

    return array(
        'nameStringTemplate' => array(
            self::BELONGS_TO, 'LocalizedString', 'NameStringTemplateId',
            'condition' => 'nameStringTemplate.LocaleCode = \''.$localeCode.'\''),
    );
}

并添加一个只读属性:

public function getName() {
    return $this->nameStringTemplate->StringTemplate;
}

这样做,您现在可以在任何地方编写 $ room-> name ,并且可以自动获取应用程序当前语言的本地化翻译.

By doing this, you can now write $room->name anywhere and you will automagically get back the localized translation for the application's current language.

有很多细节需要处理,我在这里已经介绍过,但是这个主意应该很明显.

There are many details that need to be taken care of and that I have glossed over here, but the idea should be apparent.

这篇关于Yii本地化-CDbMessageSource示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆