繁体   English   中英

使用Globalize2转换模型(导轨)

[英]Translating model with Globalize2 (rails)

我正在使用最新的globalize2和rails 2.2。 我想知道以下是错误还是功能:似乎对于数据集中的每个项目都有单独的数据库查询来获取翻译。 这听起来不对,因为它很容易导致数百个查询。

插图。 简单控制器:

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

然后@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 %>

看起来每个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]

你怎么看? 也许有一种更好的方法可以在rails中转换db数据?

您可以说出类似以下内容来完成您想做的事情:

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

这将渴望加载翻译,因此,将只有2个查询。

我不知道您是否喜欢这个问题。 但是我遇到了同样的问题。 我仅翻译表的一列(例如标题)。 如果我在一个表中有100行,并且针对给定的语言环境执行如下查询:

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

它将导致对数据库的101个查询! 我不确定这是否是Globalize的设计师所期望或想要的。

或者我丢失了一些内容(例如查询中的可选配置参数)。

但是,我确实找到了Saimon Moore的替代解决方案,他已经实现了Globalize Model Translations的替代实现。 您可以在以下位置阅读有关它的信息:

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

但是,如果与Globalize2一起使用,我找不到任何引用。

我现在正抛弃Globalize2插件,并使用Ruby I18N库推出自己的解决方案。

暂无
暂无

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

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