Automatic Translations with Autolang for Gettext

Problem

  1. Translation = boring
  2. Translation time = €
  3. Cannot be automated

Solution

  1. Translate everything automatically
  2. Correct any errors

Magic

  1. Use Google Translate API
  2. Remove Namespaces (Movie|Name = Nombre )
  3. Preserve String replacements (hello %{name} = Olla %{name} )

Install

http://github.com/grosser/autolang
Copy autolang.rake OR checkout into lib/tasks(if you are using rails, otherwise place it where rake can find it)

(Non-ruby?: install ruby + rubygems + rake-gem)

Usage

rake autolang:translate L=es POT_FILE=x.pot

Example Output

(commandline output, translations are written to "es.po")
Translating...
register
registro
--------------------------------------------------------------------------------
login
inicio de sesión
--------------------------------------------------------------------------------
Your DVD is being built and will be finished soon.
Su DVD se está construyendo y será terminado pronto.
--------------------------------------------------------------------------------
Invitation sent to %{email}
Invitación enviada a %{email}
....

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).