Read more

Organize large I18n dictionary files in Ruby on Rails

Henning Koch
June 18, 2012Software engineer at makandra GmbH

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.

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

My organization works like this:

  • config/locales/rails.de.yml modified Rails boilerplate Show archive.org snapshot
  • config/locales/faker.de.yml modified Faker boilerplate Show archive.org snapshot
  • 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.

You may 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}')]
Posted by Henning Koch to makandra dev (2012-06-18 10:05)