Managing Rails locale files with i18n-tasks

Updated . Posted . Visible to the public.

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.

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.

Dominik Schöler
Last edit
Michael Leimstädtner
Keywords
i18n_tasks
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2011-12-08 09:58)