我有一个依赖于单独引擎的 Rails 应用程序(存储在 vendor/engine_name
中)。该引擎有一个 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
格式,但没有成功。
要正确翻译这些模型名称/属性,我缺少什么?
最佳答案
经过一番搜索,我在另一个问题 Inheriting Rails i18n validation error messages in the subclass 中找到了答案因为我还试图让 i18n 在 Rails 引擎中为 ActiveRecord 工作。
根据上面的例子,答案可能是这样的,
en:
activerecord:
attributes:
foo/bar:
bar_attr: "TEST"
关于ruby-on-rails - Rails 引擎 ActiveRecord 模型 I18n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17579325/