Rails Internationalization: i18n look up with nested locales directories
Asked Answered
M

1

7

I am trying to organize my localization files with a nested file structure so that it is easier to look up.

I have followed

Organization of Locale Files in rails app

How do you structure i18n yaml files in Rails?

but I am getting a translation missing: en.view.fruits.apple. I think that Rails is trying to only look up the translation in locales/en.yml file but not the sub-directories although, I have included them.

config/application.rb:

 config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]

My locales directory:

|locales   
|-en.yml
|-views
|--en.yml

locales/views/en.yml:

en:
  fruits:
    apple: "apple"

views/fruit.html.haml:

= I18n.t('views.fruits.apple')
Moline answered 12/11, 2013 at 17:52 Comment(2)
you need to scope the keys in your files!Barcroft
You can use lazy lookup with your own custom scope: github.com/abitdodgy/i18n_lazy_scopeText
M
12

Problem solved

in my views/fruit.html.haml

instead of

= I18n.t('views.fruits.apple')

it should be

= I18n.t('fruits.apple')

since all the sub-folders are preload from

config/application.rb

 config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]

And don't forget you need to restart your server !!

Moline answered 12/11, 2013 at 21:19 Comment(1)
An alternative would have been to nest everything under 'views' in the yml file, so that is basically: {en: views: fruits: apple: "apple"}. Rails doesn't interpret the scopes based on files locations/names, only on the content of the yml.Kochi

© 2022 - 2024 — McMap. All rights reserved.