2013-07-10 57 views
3

我有一個Rails應用程序依賴於單獨的引擎(存儲在vendor/engine_name)。發動機有一個ActiveRecord對象BarRails引擎ActiveRecord模型I18n

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格式文件也是如此,沒有運氣。

我錯過了什麼讓這些模型名稱/屬性正確翻譯?

回答

9

它花了一點搜索,但我在另一個問題Inheriting Rails i18n validation error messages in the subclass找到了一個答案,因爲我也試圖讓Rails引擎中的i18n在ActiveRecord中工作。

基於上面的例子中,答案很可能是類似的東西,

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

它的工作原理!你真了不起。再一次,StackOverflow比官方文檔更好。 – JacobEvelyn