Reputation: 3082
I currently have the following 4 files in my config/locales of my root application:
-en.yml
-de.yml
-simple_form.en.yml
-simple_form.de.yml
In my application.rb which resides in a spec/dummy folder for testing the application gem I have the following line of code which seems to be retrieving the translations as expected:
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :de
I now wish to introduce to structure to the file structure of my locales folder but when I add the additional folders and change the load path in the application.rb I am getting translation not found errors. Here is my attempt:
Attempt at including structure in config/locales of my root application:
-views
-en.yml
-de.yml
-models
-en.yml
-de.yml
-forms
-simple_form.en.yml
-simple_form.de.yml
And my load path in the application.rb changed to:
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
According to the following rails guide: http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-domain-name
Upvotes: 19
Views: 10215
Reputation: 381
Want to mention. All solutions above also include files in config/locales
directory again (first time rails add it by itself). It's not a problem becase values will be rewritten with the same keys. But if you want to include ONLY subdirectory files inside config/locales
it is better to use
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*', '**', '*.{rb,yml}')]
Example. My structure:
config/
locales/
en.yml
breadcrumbs/
breadcrumbs.en.yml
If you do config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
you add en.yml several times:
irb(main):001:0> Rails.application.config.i18n.load_path
=> ["/home/air/projects/qq2/config/locales/en.yml"]
irb(main):002:0> Rails.application.config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
irb(main):003:0> Rails.application.config.i18n.load_path
=> ["/home/air/projects/qq2/config/locales/en.yml", "/home/air/projects/qq2/config/locales/en.yml", "/home/air/projects/qq2/config/locales/breadcrumbs/breadcrumbs.en.yml"]
with Dir[Rails.root.join('config', 'locales', '*', '**', '*.{rb,yml}')]
:
irb(main):001:0> Rails.application.config.i18n.load_path
=> ["/home/air/projects/qq2/config/locales/en.yml"]
irb(main):002:0> Rails.application.config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*', '**', '*.{rb,yml}')]
irb(main):003:0> Rails.application.config.i18n.load_path
=> ["/home/air/projects/qq2/config/locales/en.yml", "/home/air/projects/qq2/config/locales/breadcrumbs/breadcrumbs.en.yml"]
Upvotes: 2
Reputation: 24009
In config/application.rb
:
module PointsProject
class Application < Rails::Application
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
end
end
From Rails's guide on Internationalization: http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-domain-name
Upvotes: 0
Reputation: 294
To test the host application you need to change the i18n.load_path to the config folder of your main app and not the dummy spec for testing purposes. Code as follows:
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.default_locale = :en
Upvotes: 14
Reputation: 1953
The following options all worked for me
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.yml"]
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**' '*.{rb,yml}').to_s]
After restarting the webserver of course...
Upvotes: 3
Reputation: 1604
I had a similar issue, I solved it by adding this line to my application.rb config:
# load the subfolders in the locales
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]
Upvotes: 8