How to show all requested translation strings in Rails?

When Rails functions request translation ( I18n.translate ), I don’t want to parse their code to get exact areas, etc.

How can I add debug output to the console for every line that was requested?

<strong> 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 
+6
debugging ruby-on-rails internationalization
source share
2 answers

This is not a very elegant solution, but it worked for me. I created an initializer:

 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 

Then you can run the commands as shown below:

 $ DEBUG_TRANSLATION=true rake cucumber 

... and you will see all translation attempts reset to STDOUT. However, I do not consider this production code, so I saved it in Gist and did not check it in my main project source control at this stage.

Noddy, but he does the job.

+8
source share

Just a small change to send I18n debugging messages in the log:

replace this line:

 puts "Translate: #{args.inspect}" 

from

 Rails.logger.debug "Translate : #{args.inspect}" 
+2
source share

All Articles