Debugging I18n lookup in Rails

Here's a nice trick to debug I18n translation strings on Rails.

1. Add this to config/initializers/i18n.rb:
module I18n
  module Backend
    class Simple
      # Monkey-patch-in localization debugging
      # Enable with ENV['I18N_DEBUG']=1 on the command line in server startup, or ./config/environments/*.rb file.
      def lookup(locale, key, scope = [], options = {})
        init_translations unless initialized?
        keys = I18n.normalize_keys(locale, key, scope, options[:separator])

        puts "I18N keys: #{keys}"

        keys.inject(translations) do |result, _key|
          _key = _key.to_sym
          return nil unless result.is_a?(Hash) && result.has_key?(_key)
          result = result[_key]
          result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)

          puts "\t\t => " + result.to_s + "\n" if (result.class == String)

end if ENV['I18N_DEBUG']

2. Call your server/test suite from the console with the environment variable I18N_DEBUG=1, and you'll get this nice log of where Rails is looking for your translation strings:
$ I18N_DEBUG=1 rake test TEST=test/integration/frontend/sign_up_test.rb
Run options: --seed 27950

# Running tests:

I18N keys: [:sv, :frontend, :home, :index, :sign_in, :sign_in]
   => Logga in
I18N keys: [:sv, :frontend, :home, :index, :sign_up, :facebook_connect]
   => Logga in med Facebook
I18N keys: [:sv, :frontend, :home, :index, :sign_in, :or]
   => eller
I18N keys: [:sv, :frontend, :home, :index, :sign_in, :email]
   => Email

Finished tests in 2.280371s, 0.8771 tests/s, 3.5082 assertions/s.

2 tests, 8 assertions, 0 failures, 0 errors, 0 skips
I'm reposting this code (with some modifications) from heresince that link doesn't seem to work most of the time.

There's also another method explained in this blog post for logging all missing translations and fallbacks, which also seems very useful. Maybe I'll try it next time :)

Have a nice day!