Rails:初始化程序中的翻译

时间:2013-03-10 20:12:22

标签: ruby-on-rails translation initializer

我有几个翻译隐藏在这样的初始化程序中:

GROUP_GOVERNANCE_STYLES = [
  [I18n.t("constants.group_governance_styles.collective"), 1],
  [I18n.t("constants.group_governance_styles.electoral_democracy"), 2],
  [I18n.t("constants.group_governance_styles.dictatorship"), 3]
]

在我的en.yml文件中,我有:

en:
  constants:
    group_governance_styles:
      collective: "collective"
      electoral_democracy: "electoral democracy"
      dictatorship: "dictatorship"

不幸的是,当我尝试在视图中呈现这些翻译时,我收到以下错误:

translation missing: en.constants.group_governance_styles.collective
translation missing: en.constants.group_governance_styles.electoral_democracy
translation missing: en.constants.group_governance_styles.dictatorship

我也尝试删除范围,但翻译仍然缺失。还有另一种方法来解决这个问题吗?也许我不知道初始化器的默认范围?

2 个答案:

答案 0 :(得分:3)

AFAIK,翻译在初始化程序之后加载。尝试将其放入environment.rb

答案 1 :(得分:1)

如果您希望能够在其他初始化程序文件中使用翻译文件,则可以添加I18n初始化程序。

config / initializers / i18n.rb

# Load application custom translations in order to use them in other initializers
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]