Configure Rails 3’s I18N load path for subdirectories

If you’re like me you want to use nested directories to organize all of your files, especially when it comes to internationalization (I18N). I just found out about the nifty views trick that uses your current controller and action as the first part of your translation key. For example, let’s say you are looking at a list of all uploads you might be viewing the action:

http://www.yoursite.com/uploads/

Inside the template you probably have:

<h1><%= t('.title') %></h1>

Because of the leading dot Rails will be looking for the key defined as (assuming :en locale):

en:
  uploads:
    index:
      title: Your title

Since you’re going to have a lot of locale files, you might think of placing the English locale file in:

config/locales/views/uploads/index/en.yml

Which is all well and good until you realize the I18N load path doesn’t include subdirectories. A quick fix to application.rb is all it takes though. Add:

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**/*.{rb,yml}').to_s]

The ** is the magic there.