Read more

Managing Rails locale files with i18n-tasks

Dominik Schöler
December 08, 2011Software engineer at makandra GmbH

When internationalizing your Rails app, you'll be replacing strings like 'Please enter your name' with t('.name_prompt'). You will be adding keys to your config/locales/*.yml files over and over again. Not to miss any key and place each at the right place is a challenging task.

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

The gem i18n-tasks has you covered. See its README Show archive.org snapshot for a list of things it will do for you.

Note

The i18n-tasks gem does not understand aliases and will duplicate all referenced data when it writes locales. If you are using YAML anchors, aliases and merge keys as suggested in this card, you should disable auto-insertion of missing keys (by having an empty write: key in config/i18n-tasks.yml. Since you need to manually put in the actual translation anyway, this is no drawback.

Test that I18n keys are translated into every locale

This spec will fail if a translaton key is missing:

require 'i18n/tasks'

describe 'I18n' do
  let(:i18n) { I18n::Tasks::BaseTask.new }
  let(:missing_keys) { i18n.missing_keys }
  let(:unused_keys) { i18n.unused_keys }

  it 'does not have missing keys' do
    expect(missing_keys).to be_empty,
      "Missing #{missing_keys.leaves.count} i18n keys, run `i18n-tasks missing' to show them"
  end

  it 'does not have unused keys' do
    expect(unused_keys).to be_empty,
      "Missing #{unused_keys.leaves.count} i18n keys, run `i18n-tasks unused' to show them"
  end
end

Alternatives

Simple

"Whenever I18n::MissingTranslationData exception is caught, i18n_translation_spawner Show archive.org snapshot tries to add a new key into your translation file, generates new translation for it and does it within all locales you have defined in your app."

Extended

The plugin translate Show archive.org snapshot offers a nice web interface for manual translation, presenting you with a easy-to-use list of all your i18n keys.

Caveat: Will reformat your .yml file and use no quotation marks.

Most powerful

DHH's plugin tolk Show archive.org snapshot needs an own database table but is the most powerful of these tools.

Know that this is only a small collection, there are many more tools.

Posted by Dominik Schöler to makandra dev (2011-12-08 10:58)