Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 335
giraff Avatar asked Jun 22 '10 13:06

giraff


1 Answers

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.

like image 118
Paul Russell Avatar answered Sep 20 '22 05:09

Paul Russell