Miguel Parramón Teixidó

Debugging I18n lookup in Rails

· Miguel Parramón Teixidó · Blogger

Etiquetes: 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 »

Veure entrada original →