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/rails.de.yml modified Rails boilerplate
  • config/locales/faker.de.yml modified Faker boilerplate
  • config/locales/models.de.yml model names, attribute names, assignable_value labels
  • config/locales/views.de.yml text in the GUI
  • config/locales/blocks.de.yml 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:

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

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
almost 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