【问题标题】:How do I internationalize/localize my rails plugin?如何国际化/本地化我的 rails 插件?
【发布时间】:2010-11-19 04:58:41
【问题描述】:

我写了一个validates_word_count plugin。我想将错误消息存储在 YAML 文件中,以便轻松翻译。

我的插件的文件布局如下:

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() 正常工作?

【问题讨论】:

    标签: ruby-on-rails internationalization yaml


    【解决方案1】:

    答案分为两部分。
    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
    

    【讨论】:

    • 你知道如何在 Rails 4 中做到这一点吗?看起来 init.rb 已不再使用。
    • Rails 插件不再受支持。您需要制作宝石。
    • 很高兴知道。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多