I18n::MissingTranslations
I18n::MissingTranslations is a simple tool that helps with finding missing translations in your application.
It consists of three parts:
- a module that plugs into the I18n::ExceptionHandler class and logs I18n::MissingTranslationData exceptions
- an in-memory logger that simply holds missing translations during a request or test run
- a middleware that can be used to dump the contents of the logger to a file after each request
Installation
I18n::MissingTranslations requires I18n 0.5.0 which has not been released yet (2010-11-06). So you need to make sure you require the 0.5.0 branch from the I18n repository:
gem 'i18n', :git => 'git://github.com/svenfuchs/i18n.git', :ref => '0.5.0'
Usage in test environment
This is what you might want to put into your test_helper:
require 'i18n/missing_translations' at_exit { I18n.missing_translations.dump }
If there are any missing translations then it will print out a YAML snippet for them that you can copy and paste to your locale file.
Usage in development environment
The following hooks up the I18n::MissingTranslations middleware in development mode. You might want to add these lines as an initializer:
require 'i18n/missing_translations' config.app_middleware.use(I18n::MissingTranslations) if Rails.env.development?
The middleware will then log missing tranlations to a file missing_translations.yml
in your locales dir (which is config/locales
if present or the current directory otherwise). You can also pass the filename as an argument:
config.app_middleware.use(I18n::MissingTranslations, 'path/to/locales/missing.yml')
The middleware reads and writes per request. That means that on subsequent requests missing translations are added to the missing_translations.yml
file. So if you go ahead and copy translations from the missing_translations.yml
to your actual locale files you will also want to clear or delete missing_translations.yml
.
NOTE Rails (3.0.1) does not pick up new locale files between requests. That effectively means that manual changes to the missing_translations.yml
file might be overwritten unless you restart the server.
Thus your workflow for finding and moving missing translation keys might look something like this:
- start the server
- click around/work on stuff
- check
config/locales/missing_translations.yml
- copy any missing translation keys to your actual locale files and correct the translations
- delete or clear
config/locales/missing_translations.yml
- restart the server