main.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. require 'csv'
  2. require 'nokogiri'
  3. module MultiLangTrans
  4. class << self
  5. INPUT_FOLDER = 'input/*.csv'
  6. TEMPLATE_FOLDER = 'template/*.resw'
  7. TEMPLATE_CSV_PATH = 'template/template.csv'
  8. def run
  9. template_file_paths = Dir.glob(TEMPLATE_FOLDER)
  10. print "Using #{template_file_paths.first}, "
  11. make_template(template_file_paths.first)
  12. input_file_paths = Dir.glob(INPUT_FOLDER)
  13. puts "#{input_file_paths.count} file(s) translating..."
  14. input_file_paths.each do |input_file_path|
  15. input_content = read_csv(input_file_path)
  16. output_file_paths = get_output_paths(input_file_path)
  17. print " * #{input_file_path} --> #{output_file_paths[:matched]}, #{output_file_paths[:dismatched]}: "
  18. puts 'ok!' if write_xml(output_file_paths, input_content)
  19. end
  20. puts 'Done!'
  21. end
  22. private
  23. def make_template(template_file_path)
  24. xml_doc = File.open(template_file_path) { |f| Nokogiri::XML(f) }
  25. File.open(TEMPLATE_CSV_PATH, 'w') do |f|
  26. f.puts("name,trans_value,trans_comment")
  27. xml_doc.xpath("//data").each do |data|
  28. value = data.at_xpath('value').content unless data.at_xpath('value').nil?
  29. comment = data.at_xpath('comment').content unless data.at_xpath('comment').nil?
  30. f.puts("#{data['name']},#{value},#{comment}")
  31. end
  32. end
  33. @template_content = read_csv(TEMPLATE_CSV_PATH)
  34. end
  35. def read_csv(csv_file_path)
  36. input_content = []
  37. CSV.foreach(csv_file_path, headers: true) do |row|
  38. input_content << row.to_hash
  39. end
  40. input_content
  41. end
  42. def get_output_paths(input_path)
  43. output_paths = {}
  44. output_path = input_path.sub('input/', "output/")
  45. output_paths[:matched] = output_path.sub('.csv', ".txt")
  46. output_paths[:dismatched] = output_path.sub('.csv', "-not-found.txt")
  47. output_paths
  48. end
  49. def write_xml(xml_file_paths, content_rows)
  50. not_found_rows = []
  51. File.open(xml_file_paths[:matched], 'w') do |f|
  52. content_rows.each_with_index do |row, index|
  53. template_row = @template_content.find{|t| t['trans_value'] == row['template_value']}
  54. if template_row.nil?
  55. not_found_rows << "line:#{index + 2}.\t#{row['template_value']}"
  56. else
  57. f.puts("<trans-unit id=\"#{template_row['name']}\" translate=\"yes\" xml:space=\"preserve\">")
  58. f.puts(" <source>#{template_row['trans_value']}</source>") unless template_row['trans_value'].nil?
  59. f.puts(" <target state=\"translated\">#{row['trans_value']}</target>") unless row['trans_value'].nil?
  60. f.puts(" <note from=\"MultilingualBuild\" annotates=\"source\" priority=\"2\">#{template_row['trans_comment']}</note>") unless template_row['trans_comment'].nil?
  61. f.puts("</trans-unit>")
  62. end
  63. end
  64. end
  65. File.open(xml_file_paths[:dismatched], 'w') do |f|
  66. not_found_rows.each { |row| f.puts(row) }
  67. end
  68. true
  69. rescue => e
  70. puts e
  71. false
  72. end
  73. end
  74. end
  75. MultiLangTrans.run