如何在多语言数据库模型中使用 Yii? [英] How to use Yii with a multilingual database model?

查看:20
本文介绍了如何在多语言数据库模型中使用 Yii?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从我创建的完全多语言的数据库中获取数据时遇到问题,我希望这里有人可以帮助我.

我把我所有的桌子分成两部分;通用"表(不包含任何需要翻译的文本)和包含所有需要翻译的字段及其翻译的表.

示例表格:

base_materialID图片base_material_i18nbase_material_idlocalization_id名称描述评论状态评论笔记本土化ID语言名称

查询以获取翻译(如果没有可用的翻译,则使用英语 (en) 作为后备语言):

SELECT o.id, o.type, o.code, o.position, ifnull(t.name,d.name) 名称, ifnull(t.description,d.description) 描述FROM base_material o内部连接 ​​base_material_i18n dON (o.id=d.base_material_id)左外连接 base_material_i18n tON ( d.base_material_id=t.base_material_id AND t.localization_id='nl' )WHERE d.localization_id='en'

我的问题是,当我搜索 base_material 对象时,如何在 Yii 中自动将这些翻译(使用此查询中的后备语言)附加到我的模型?(这只是 1 个示例表,但我几乎所有的表(20+)都是以这种方式构建的,所以如果可能的话,我需要一些灵活的东西)

使用我需要的现有系统的一个例子是 Propel:http://propel.posterous.com/propel-gets-i18n-behavior-and-why-it-matters

任何想法如何去做?我已经检查了有关多语言站点的现有 Yii 扩展(例如 Multilingual Active Record),但它们都使用不同的数据库设计(主表中的一般信息+后备语言,i18n 表中的翻译),我不确定如何更改这些扩展以使用我的数据库模型.

如果有人知道更改现有扩展的方法,以便它可以使用我的数据库方案,那么这绝对是绝妙的,而且可能是最好的方法.

我添加了一个赏金,因为我仍然找不到关于如何让 Propel 与 Yii 一起工作的任何信息(确实存在 Doctrine 的扩展,但 Doctrine 也不支持这种带翻译的 DB 模型),也没有关于如何使用现有的 Yii 扩展或作用域来处理此问题的更多信息.

编辑:查看了 98 次,但只有 3 条赞和 1 条评论.我不禁觉得我在这里做错了什么,无论是我的问题还是应用程序/数据库设计;要么是那个,要么是我的问题非常独特(这会让我感到惊讶,因为我不认为我的多语言数据库设计是那么荒谬;-).因此,如果有人知道使用 Yii 和/或 Propel 的多语言站点的更好的全方位解决方案(除了由于文本字段重复而我真的不喜欢的当前扩展)或类似的东西,请告诉我

提前致谢!

解决方案

我也在寻找将 i18n 实现到 Yii 模型的通用解决方案.最近我为像你这样的项目选择了一个非常相似的数据库模式.唯一的区别是,我没有使用单独的语言表,而是将语言信息存储在 i18n 表中.

以下解决方案没有自定义 SQL 语句,但我认为这可以使用关系参数来实现,无论如何,如果您在数据库中使用外键(例如 MySQL InnoDB),gii 将在您的 base_material 之间创建关系和 base_material_i18n 表,如

class BaseMaterial 扩展 CActiveRecord公共函数关系(){返回数组('baseMaterialI18ns' =>数组(自我:: HAS_MANY,'base_material_i18n','id'),);}类 BaseMaterialI18n 扩展了 CActiveRecord公共函数关系(){返回数组('基础材料' =>数组(自我:: BELONGS_TO,'base_material','id'),);}

现在您可以使用关系的对象表示法访问您的翻译.

$model = BaseMaterial::model()->with('baseMaterialI18ns')->findByPk(1);foreach($model->baseMaterialI18ns AS $translation) {if ($translation->language != "我需要的语言") 继续://对翻译数据做一些事情...}

我想为这些模型创建一个行为或基类,作为管理翻译的助手 - 伪代码:

I18nActiveRecord 扩展了 CActiveRecord受保护的 $_attributesI18n;//在查询时填充 _attributesI18n ...公共函数 __get($name) {if(isset($this->_attributesI18n['language_I_need'][$name]))返回 $this->_attributesI18n[$name];else if(isset($this->_attributesI18n['fallback_language'][$name]))返回 $this->_attributesI18n[$name];别的父::__get();}

CActiveRecord __get() 源码

要找到所需的 i18n 记录还有更多工作要做,您还可以进一步限制 with() 选项以提高性能并减少 PHP 端的解析.

但是可能有不同的用例如何确定值,例如所有翻译,翻译或回退,没有回退(空值).场景在这里可能会有所帮助.

PS:我会参与一个 github 项目

I’m having a problem getting the data from my database which I created to be completely multilingual, and I hope someone here can help me.

I’ve split up all my tables in 2 parts; the "universal" table (does not contain any text that needs to be translated) and the table which contains all the fields that need to be translated with their translations.

Example tables:

base_material
    id
    picture
base_material_i18n
    base_material_id
    localization_id
    name
    description
    review_status
    review_notes
localization
    id
    language_name

Query to get the translations (using English (en) as a fall-back language if there is no translation available):

SELECT o.id
     , o.type
     , o.code
     , o.position
     , ifnull(t.name,d.name) name
     , ifnull(t.description,d.description) description
  FROM base_material o
       INNER JOIN base_material_i18n d
               ON ( o.id=d.base_material_id)
       LEFT OUTER JOIN base_material_i18n t
                    ON ( d.base_material_id=t.base_material_id AND t.localization_id='nl' )
 WHERE d.localization_id='en'

My question is how I can automatically get those translations (with the fall-back language as in this query) attached to my model in Yii when I’m searching for the base_material objects? (This is only 1 example table, but almost all my tables (20+) are built in this way, so if possible I would be needing something flexible)

An example of an existing system using what I would need is Propel: http://propel.posterous.com/propel-gets-i18n-behavior-and-why-it-matters

Any ideas how to go about doing that? I’ve checked the existing Yii extensions regarding multilingual sites (like Multilingual Active Record), but they all use a different database design (general information+fall-back language in the main table, translations in the i18n table), and I’m not sure how to change those extensions to use my kind of DB model.

If someone knows of a way to change that existing extension so it can use my kind of DB scheme, then that would be absolutely brilliant and probably the best way to do this.

Edit: I've added a bounty because I still can't find anything on how to let Propel work with Yii (there does exist an extension for Doctrine, but Doctrine doesn't support this kind of DB model with translations either), nor any more information as to how to deal with this using an existing Yii extension or with scopes.

Edit: 98 times viewed but only 3 upvotes and 1 comment. I can't help feeling like I'm doing something wrong here, be it in my question or application/database design; either that or my problem is just very unique (which would surprise me, as I don't think my multilingual database design is that absurd ;-). So, if anyone knows of a better all-round solution for multilingual sites with Yii and/or Propel (apart from the current extensions which I really don't like due to the duplication of text fields) or something similar, please let me know as well.

Thanks in advance!

解决方案

I am also looking for a generic solution to implement i18n into Yii models. Recently I chose a very similar database schema for a project like you. The only difference is, that I am not using a separate language table, I store the language information in the i18n table.

The following solution is without a custom SQL statement, but I think this could be implemented with relation params, anyway, if you're working with foreign key in your database (eg. MySQL InnoDB) gii will create relations between your base_material and base_material_i18n table, like

class BaseMaterial extends CActiveRecord

public function relations()
{
    return array(
        'baseMaterialI18ns' => array(self::HAS_MANY, 'base_material_i18n', 'id'),
    );
}



class BaseMaterialI18n extends CActiveRecord

public function relations()
{
    return array(
        'baseMaterial' => array(self::BELONGS_TO, 'base_material', 'id'),
    );
}

Now you'd be able to access your translations by using the object notation for relations.

$model = BaseMaterial::model()->with('baseMaterialI18ns')->findByPk(1);
foreach($model->baseMaterialI18ns AS $translation) {
  if ($translation->language != "the language I need") continue:
  // do something with translation data ...
}

I thought about creating a behavior or base class for those models which would act a as helper for managing the translations - pseudo code:

I18nActiveRecord extends CActiveRecord

protected $_attributesI18n;

// populate _attributesI18n on query ...

public function __get($name) {
  if(isset($this->_attributesI18n['language_I_need'][$name]))
    return $this->_attributesI18n[$name];
  else if(isset($this->_attributesI18n['fallback_language'][$name]))
    return $this->_attributesI18n[$name];
  else 
    parent::__get();
}

CActiveRecord __get() source

There is more work to be done to find the needed i18n record, also you could further limit the with() option to improve performance and reduce parsing on the PHP side.

But there may be different use cases how to determine the value, e.g. all translations, translation or fallback, no fallback (empty value). Scenarios could be helpful here.

PS: I would be up for a github project!

这篇关于如何在多语言数据库模型中使用 Yii?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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