Posted over 7 years ago. Visible to the public. Repeats.

Organize large I18n dictionary files in Rails 3+

If you're suffering from a huge de.yml or similiar file, cry no more. Rails lets you freely organize your dictionary files in config/locales.

My organization works like this:

  • config/locales/ modified Rails boilerplate
  • config/locales/ modified Faker boilerplate
  • config/locales/ model names, attribute names, assignable_value labels
  • config/locales/ text in the GUI
  • config/locales/ if you organize your CSS with BEM you can use this instead of (or together with) views.<locale>.yml for text in the GUI
  • config/locales/de.yml various one liners that don't really fit anywhere else yet. extract lines to separate files as you see clusters of translations emerging.

Note that Rails really doesn't care if the name of the locale (e.g. de or en) appears in the file. Since the root node of each YAML tree is the locale code, it can just merge all files together.

At least in Rails 4+ you need to restart your server if you add files to config/locale.

Using subfolders

You can also use subfolders, but then you need to say in your application.rb:

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

Rails 2 compatibility

I only tested this with Rails 3. Note that the functionality above might not even be delivered by Rails itself, but by the i18n gem. You might need to update that instead.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Henning Koch
Last edit:
over 3 years ago
by Judith Roth
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more