Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Translation in Initializer

I have several translations tucked away into an initializer like so:

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]
]

In my en.yml file I have:

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

Unfortunately, when I try to render these translations in a view, I get the following errors:

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

I've also tried removing the scope, but the translations still come up as missing. Is there another way to approach this? Perhaps there's a default scope for initializers that I don't know about?

like image 755
nullnullnull Avatar asked Mar 10 '13 20:03

nullnullnull


2 Answers

AFAIK, translations get loaded after the initializers. Try putting it in environment.rb

like image 154
Hesham Avatar answered Oct 11 '22 22:10

Hesham


If you want to be able to use your translation files in other initializer files you can add an I18n initializer.

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]
like image 40
Mugur 'Bud' Chirica Avatar answered Oct 11 '22 20:10

Mugur 'Bud' Chirica