giraff
giraff

Reputation: 4711

How to show all asked-for Translation Strings in Rails?

When Rails functions are asking for a translation (I18n.translate), I don't want to analyze their code in order to get the exact scopes etc.

How can I add a debug output into the console for every string that was asked for?

Examples:

I18n.t 'errors.messages.invalid', :scope => :active_record 
# Translation for 'activerecord.errors.messages.invalid' (not) found

label(:post, :title)
# Translation for 'activerecord.attributes.post.title' not found
# Translation for 'views.labels.post.title' not found

Upvotes: 8

Views: 865

Answers (2)

Paul Russell
Paul Russell

Reputation: 4747

This is not a very elegant solution, but it's worked for me. I've created an initialiser:

require 'i18n'

if (Rails.env.development? || Rails.env.test?) && ENV['DEBUG_TRANSLATION']
  module I18n
    class << self
      def translate_with_debug(*args)
        Rails.logger.debug "Translate : #{args.inspect}"
        translate_without_debug(*args)
      end
      alias_method_chain :translate, :debug
    end
  end
end

You can then run commands like the following:

$ DEBUG_TRANSLATION=true rake cucumber

...and you'll see all the translations being attempted dumped to STDOUT. I don't consider this production code though, so I've kept it in a Gist, and not checked it into my main project source control at this stage.

Noddy, but it does the job.

Upvotes: 10

Andrea D&#39;Amico
Andrea D&#39;Amico

Reputation: 81

Just a small change to put I18n debug messages in the log:

substitute this line:

puts "Translate: #{args.inspect}"

with

Rails.logger.debug "Translate : #{args.inspect}"

Upvotes: 2

Related Questions