Record Gettext at Test Runtime

Recently our team got frustrated with all the phrases gettext would not find.

  • words in if blocks
  • word produced by helpers
  • words used in arrays that get translated at runtime

Here is a simple solution: collect all phrases that were used during testing. Since the testsuite often has  100% C0 coverage and any phrase that is not found will signals missing tests.

Install

#spec/spec_helper.rb or test/test_helper.rb
if ENV['LOG_GETTEXT']
  def _(word)
    File.open(ENV['LOG_GETTEXT'], File::WRONLY|File::APPEND|File::CREAT) do |f|
      f.puts word
    end
    gettext(word)
  end
end


#lib/taskts/gettext_test_log.rb
task :gettext_test_log do
  tmpfile = 'locale/tmp_gettext_test_log.txt'
  outfile = "app/testlog_phrases.rb"
  
#  system "rake test LOG_GETTEXT=#{tmpfile}"
  system "rake spec LOG_GETTEXT=#{tmpfile}"
  process_log(tmpfile,outfile)
end

def process_log(tmpfile,outfile)
  found = {}
  File.readlines(tmpfile).each do |line|
    line.strip!
    next if line.empty?
    next if line =~ /%s of /
    next if %w[nil Sun Mon Tue Wed Thu Fri Sat Sunday Monday Tuesday Wednesday Thursday  Friday  Saturday  Jan Feb Mar  Apr Jun  Jul Aug Sep Oct Nov Dec January February March April May June July August September October November December].include? line
    found[line]=true
  end
  
  File.open(outfile,'w') do |f|
    found.each do |k,v|
      f.puts "_('#{k}')"
    end
  end
end

Usage
The generated testlog_phrases.rb should be placed in a folder that is search by your updatepo task(it is not meant to be executed).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s