📚 Blog Archive

Debugging I18n lookup in Rails

· Miguel Parramón · blogger

Tags: i18n, Ruby on 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)

      result
    end
  end
end

end end if ENV[‘I18N_DEBUG’]

Read more »

View original post →