【问题标题】:Rails engine ActiveRecord models I18nRails 引擎 ActiveRecord 模型 I18n
【发布时间】:2013-07-10 19:29:15
【问题描述】:

我有一个依赖于单独引擎(存储在vendor/engine_name)的 Rails 应用程序。该引擎有一个 ActiveRecord 对象Bar

module Foo
  class Bar < ActiveRecord::Base
    # has an attribute bar_attr
  end
end

在该引擎的 config/locales/en.yml 文件中,我尝试过:

en:
  activerecord:
    attributes:
      bar_attr: "TEST"

我也试过了:

en:
  activerecord:
    attributes:
      bar:
        bar_attr: "TEST"

和:

en:
  activerecord:
    attributes:
      foo:
        bar:
          bar_attr: "TEST"

但无论如何,当我从父应用程序调用 Foo::Bar.human_attribute_name("bar_attr") 时,我会得到“Bar attr”(例如默认的人类属性名称)。请注意,当我尝试使用以下方法进行翻译时,Foo::Bar.model_name.human 也会出现同样的问题:

en:
  activerecord:
    models:
      ...

我不确定应用程序/引擎结构是否相关,因为我也在父应用程序的翻译文件中尝试了上述三种en.yml 格式,但没有成功。

要正确翻译这些模型名称/属性,我缺少什么?

【问题讨论】:

    标签: ruby-on-rails rails-activerecord rails-i18n


    【解决方案1】:

    我进行了一些搜索,但我在另一个问题 Inheriting Rails i18n validation error messages in the subclass 中找到了答案,因为我还试图让 i18n 在 Rails 引擎中为 ActiveRecord 工作。

    根据上面的例子,答案可能是这样的,

    en:
      activerecord:
        attributes:
          foo/bar:
            bar_attr: "TEST"
    

    【讨论】:

    • 有效!你太棒了。再一次,StackOverflow 比官方文档更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多