简体   繁体   English

使用Globalize2转换模型(导轨)

[英]Translating model with Globalize2 (rails)

I'm using latest globalize2 and rails 2.2. 我正在使用最新的globalize2和rails 2.2。 I wonder if the following is bug or feature: there seems to be a separate db query for each item in a dataset to get the translation. 我想知道以下是错误还是功能:似乎对于数据集中的每个项目都有单独的数据库查询来获取翻译。 That doesn't sound right since it may easily result in hundreds of queries. 这听起来不对,因为它很容易导致数百个查询。

Illustration. 插图。 Simple controller: 简单控制器:

def index
    @menu_sections = MenuSection.find(:all)
end

Then @menu_sections is looped thru in a view, where localized attribute (name) is called: 然后@menu_sections在视图中循环,其中调用了本地化的属性(名称):

  <% @menu_sections.each do |menu_section| %>
    <p><%= link_to menu_section.name, :controller => 'store', :action => 'list_menu_items_for_section', :section_id => menu_section.id %></p>
  <% end %>

Looks like every menu_section.name results in db query: 看起来每个menu_section.name都会导致数据库查询:

Processing StoreController#index (for 10.0.2.2 at 2009-03-02 16:05:53) [GET]
  Session ID: 468f54928cbdc0b19c03cfbd01d09fa9
  Parameters: {"action"=>"index", "controller"=>"store"}
  MenuSection Load (0.0ms)   SELECT * FROM `menu_sections`
Rendering template within layouts/store
Rendering store/index
Rendered application/_js_includes (0.0ms)
  MenuSection Columns (0.0ms)   SHOW FIELDS FROM `menu_sections`
  MenuSectionTranslation Load (0.0ms)   SELECT * FROM `menu_section_translations` WHERE (`menu_section_translations`.menu_section_id = 1 AND (`menu_section_translations`.`locale` IN ('en','root')))
  MenuSectionTranslation Columns (0.0ms)   SHOW FIELDS FROM `menu_section_translations`
  MenuSectionTranslation Load (0.0ms)   SELECT * FROM `menu_section_translations` WHERE (`menu_section_translations`.menu_section_id = 2 AND (`menu_section_translations`.`locale` IN ('en','root')))
  MenuSectionTranslation Load (0.0ms)   SELECT * FROM `menu_section_translations` WHERE (`menu_section_translations`.menu_section_id = 3 AND (`menu_section_translations`.`locale` IN ('en','root')))
Completed in 340ms (View: 320, DB: 0) | 200 OK [http://www.dev.babooka.com/store]

What do you think? 你怎么看? Perhaps there is a better way for translating db data in rails? 也许有一种更好的方法可以在rails中转换db数据?

You can accomplish what you're trying to do by saying something like: 您可以说出类似以下内容来完成您想做的事情:

def index
    @menu_sections = MenuSection.find(:all,:include=>:globalize_translations)
end

That will eager load the translations, so, there will only be 2 queries. 这将渴望加载翻译,因此,将只有2个查询。

I don'k know if you reslove this issue. 我不知道您是否喜欢这个问题。 But I am ran into the same problem. 但是我遇到了同样的问题。 I an translating just one column of a table (say title). 我仅翻译表的一列(例如标题)。 If I have 100 rows in a table and if I do a query like this for a given locale: 如果我在一个表中有100行,并且针对给定的语言环境执行如下查询:

rows = Category.find(:all, .....) 行= Category.find(:all,.....)

It will result in 101 queries on the database !! 它将导致对数据库的101个查询! I am not sure if this is expected or intended by the designer of Globalize. 我不确定这是否是Globalize的设计师所期望或想要的。

OR I am missing something (Like an optional configuration parameter on the query). 或者我丢失了一些内容(例如查询中的可选配置参数)。

However I did find an alternative solution by Saimon Moore who has implemented Alternative implementation of Globalize Model Translations. 但是,我确实找到了Saimon Moore的替代解决方案,他已经实现了Globalize Model Translations的替代实现。 You can read about it at: 您可以在以下位置阅读有关它的信息:

http://saimonmoore.net/2006/12/1/alternative-implementation-of-globalize-model-translations http://saimonmoore.net/2006/12/1/alternative-implementation-of-globalize-model-translations

However I can't find any references if this works with Globalize2. 但是,如果与Globalize2一起使用,我找不到任何引用。

I am at a point of throwing out Globalize2 plugin and roll my own solution using Ruby I18N library. 我现在正抛弃Globalize2插件,并使用Ruby I18N库推出自己的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM