【问题标题】:Translating model with Globalize2 (rails)使用 Globalize2 (rails) 转换模型
【发布时间】:2009-03-02 21:31:04
【问题描述】:

我正在使用最新的 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 都会导致 db 查询:

处理 StoreController#index(2009-03-02 16:05:53 的 10.0.2.2)[GET] 会话 ID:468f54928cbdc0b19c03cfbd01d09fa9 参数:{"action"=>"index", "controller"=>"store"} MenuSection 加载 (0.0ms) SELECT * FROM `menu_sections` 在布局/商店中渲染模板 渲染存储/索引 渲染的应用程序/_js_includes (0.0ms) MenuSection Columns (0.0ms) SHOW FIELDS FROM `menu_sections` MenuSectionTranslation 加载 (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'))) 在 340 毫秒内完成(查看:320,DB:0)| 200 OK [http://www.dev.babooka.com/store]

你怎么看?也许有更好的方法来翻译 Rails 中的 db 数据?

【问题讨论】:

    标签: ruby-on-rails localization


    【解决方案1】:

    你可以通过说这样的话来完成你想做的事情:

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

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

    【讨论】:

      【解决方案2】:

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

      rows = 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 库推出我自己的解决方案。

      【讨论】:

        猜你喜欢
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 2013-03-17
        • 2010-12-29
        • 1970-01-01
        • 2010-12-20
        • 2018-05-27
        • 1970-01-01
        相关资源
        最近更新 更多