5

我有几个翻译隐藏在初始化器中,如下所示:

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

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

4

2 回答 2

4

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

于 2013-03-10T20:57:35.200 回答
3

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

配置/初始化程序/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]
于 2018-12-25T00:26:17.347 回答