19

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

j0k
  • 22,600
  • 28
  • 79
  • 90
Jay
  • 3,012
  • 14
  • 48
  • 99
  • Please show us the specific error you are getting. Do you have a stack trace you can share? – Kevin Bedell May 24 '12 at 10:44
  • In the view I am receiving the string: translation missing: en.new_tenant.header, I an not seeing any error messages – Jay May 24 '12 at 11:08
  • Could the problem be that the application.rb file resides in a spec folder for testing purposes and that my config folder is not being loaded at the root level and I am only seeing the translations as the config/locales are added automatically but not any subfolders? – Jay May 24 '12 at 11:28
  • Dir[Rails.root.to_s] results in the string C:/Sites/MyApp/spec/dummy which is the dummy app for testing MyApp. The config folder with the translations resides at C:/Sites/MyApp/Config/Locales/ – Jay May 24 '12 at 11:35
  • I tried the tried this: my_root = "C:/Sites/MyApp" config.i18n.load_path += Dir[my_root.join('config', 'locales', '**', '*.{rb,yml}').to_s] config.i18n.default_locale = :de – Jay May 24 '12 at 11:44
  • But the join method is unrecognised for this string – Jay May 24 '12 at 11:44

5 Answers5

14

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
Dorian
  • 22,759
  • 8
  • 120
  • 116
8

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}"]
ABrowne
  • 1,574
  • 1
  • 11
  • 21
3

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

Matthias
  • 1,884
  • 2
  • 18
  • 35
2

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"]
airled
  • 177
  • 3
  • 15
0

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

Dorian
  • 22,759
  • 8
  • 120
  • 116