class Po::PoTranslation

Public Class Methods

standarize(msgid, translated_str) click to toggle source
# File lib/po_translation.rb, line 12
def standarize(msgid, translated_str)
  return translated_str if translated_str.blank?
  translated_str = HTMLEntities.new.encode(translated_str.gsub("% s", "%s").gsub("\" ", "\"").gsub(/(\w) ([:"\.\?])"$\n/, "\1\2\"").strip)
  if msgid[0] == msgid[0].upcase
    translated_str = translated_str.capitalize
  end
end
translate(input_file, output_path=nil) click to toggle source
# File lib/po_translation.rb, line 20
def translate(input_file, output_path=nil)
  puts "--- #{input_file}"
  translations = GetPomo::PoFile.parse(File.read(input_file))
  translations.each_with_index do |translation, index|
    next if translation.msgid.blank? || translation.msgid.match(/["']/)

    msgstr = standarize(translation.msgid, GoogleTranslator.translate(translation.msgid))
    next if msgstr.blank?

    translation.msgstr = msgstr
    puts translation.msgstr
    if index % 20 == 0
      puts index
      sleep 2
    end
  end

  unless output_path
    output_path = File.dirname(input_file)
  end
  File.write(File.join(output_path, "#{File.basename(input_file, ".*")}-trans.po"), GetPomo::PoFile.to_text(translations))
end