2010-11-19 72 views
3

我寫了validates_word_count plugin。我想將錯誤消息存儲在YAML文件中,以便輕鬆翻譯。我如何國際化/本地化我的rails插件?

我的插件的文件格式是這樣的:

validates_word_count/ 
    init.rb 
    lib/ 
    validates_word_count.rb 
    locale/ 
     en.yml 

我YAML文件看起來像這樣:

en: 
    validates_word_count: 
    errors: 
     messages: 
     too_few_words: "has too few words (minimum is %d words)" 
     too_many_words: "has too many words (maximum is %d words)" 

但是,如果我叫I18n.translate('validates_word_count.errors.messages.too_few_words'),我得到這個錯誤:

translation missing: en, validates_word_count, errors, messages, too_few_words 

如何設置我的插件/語言環境,使I18n.translate()的作品?

回答

3

答案有兩部分。
1.使用標準的目錄結構:

validates_word_count/ 
init.rb 
    lib/ 
    validates_word_count.rb 
    config/ 
    locales/ 
     en.yml 


2.在init.rb,加上下面幾行:

Dir[File.join("#{File.dirname(__FILE__)}/config/locales/*.yml")].each do |locale| 
I18n.load_path.unshift(locale) 
end 
+0

你知道如何在軌道4,5做到這一點?它看起來像init.rb不再被使用。 – nullnullnull 2014-01-27 03:34:28

+2

不再支持Rails插件。你需要創造一個寶石。 – 2014-01-27 14:44:02

+0

很高興知道。謝謝! – nullnullnull 2014-01-27 15:53:57