update_translate.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # -*- coding:utf-8 -*-
  2. import os
  3. from optparse import OptionParser
  4. from xml.dom import minidom
  5. import xlrd
  6. import sys
  7. import re
  8. APP_MOUDULE = 'app'
  9. FIND_DIDS = [APP_MOUDULE, 'frame', 'module']
  10. def option_parser():
  11. parser = OptionParser()
  12. parser.add_option("-l", "--langCode",
  13. help="语言码",
  14. metavar="langCode")
  15. parser.add_option("-i", "--inputDir",
  16. help="翻译文件路径",
  17. metavar="inputDir")
  18. (options, _) = parser.parse_args()
  19. return options
  20. def read_xml_2_dom(path):
  21. f = open(path, 'r')
  22. try:
  23. dom = minidom.parse(f)
  24. f.close()
  25. return dom
  26. except:
  27. print('xml_2_dom error, path:{}'.format(path))
  28. f.close()
  29. return
  30. def read_strings_2_dict(strings_path):
  31. string_dict = {}
  32. if not os.path.exists(strings_path):
  33. return string_dict
  34. dom = read_xml_2_dom(strings_path)
  35. if dom == None:
  36. return
  37. root = dom.documentElement
  38. strings = root.getElementsByTagName('string')
  39. for string in strings:
  40. translatable = string.getAttribute('translatable')
  41. if translatable == 'false':
  42. continue
  43. key = string.getAttribute('name')
  44. # print("read_strings_2_dict, key:{0}".format(key))
  45. value = string.firstChild.data
  46. string_dict[key] = value
  47. return string_dict
  48. def module_update_translate(module_name, module_dir, lang_code, input_dir):
  49. print("module_update_translate, module_name:{0}, module_dir:{1}".format(
  50. module_name, module_dir))
  51. # 从xls中读取更新字符串
  52. update_strings_xls_file_path = os.path.join(
  53. input_dir, '{}.xls'.format(module_name))
  54. table = xlrd.open_workbook(
  55. update_strings_xls_file_path).sheet_by_name(module_name)
  56. firstRow = table.row_values(0)
  57. target_lang_col = 0
  58. for index in range(len(firstRow)):
  59. if index <= 0:
  60. continue
  61. if firstRow[index] == lang_code:
  62. target_lang_col = index
  63. keys = table.col_values(0)
  64. del keys[0]
  65. values = table.col_values(target_lang_col)
  66. del values[0]
  67. update_strings_dic = {}
  68. for x in range(len(keys)):
  69. key = keys[x].strip()
  70. value = re.sub(r'(%\d\$)(@)', r'\1s', values[x])
  71. update_strings_dic[key] = value
  72. module_strings = os.path.join(
  73. module_dir, 'src/main/res/values-{}/strings.xml'.format(lang_code))
  74. if not os.path.exists(module_strings):
  75. module_strings_dir = os.path.join(
  76. module_dir, 'src/main/res/values-{}/'.format(lang_code))
  77. if not os.path.exists(module_strings_dir):
  78. os.makedirs(module_strings_dir)
  79. fo = open(module_strings, "w")
  80. strings = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n"
  81. for k, v in update_strings_dic.items():
  82. strings = strings + " <string name=\"" + k + "\">" + v + "</string>\n"
  83. strings = strings + "</resources>"
  84. fo.write(strings)
  85. fo.close()
  86. else:
  87. module_strings_dict = read_strings_2_dict(module_strings)
  88. module_strings_fr = open(module_strings, "r")
  89. # 更新已有字符串
  90. module_strings_text = module_strings_fr.read()
  91. module_strings_fr.close()
  92. insert_strings_dict = {}
  93. for k, v in update_strings_dic.items():
  94. if k in module_strings_dict:
  95. module_strings_text.replace(module_strings_dict[k], v)
  96. else:
  97. insert_strings_dict[k] = v
  98. module_strings_fw = open(module_strings, "w")
  99. module_strings_fw.write(module_strings_text)
  100. module_strings_fw.close()
  101. # 插入新字符串
  102. module_strings_fr = open(module_strings, "r")
  103. module_strings_lines = module_strings_fr.readlines()
  104. module_strings_fr.close()
  105. start_line_num = -1
  106. for line_num in range(len(module_strings_lines)):
  107. if module_strings_lines[line_num].find('</resources>') > -1:
  108. start_line_num = line_num
  109. break
  110. for k, v in insert_strings_dict.items():
  111. module_strings_lines.insert(
  112. start_line_num, " <string name=\"" + k + "\">" + v + "</string>\n")
  113. start_line_num = start_line_num + 1
  114. module_strings_fw = open(module_strings, "w")
  115. module_strings_fw.write(''.join(module_strings_lines))
  116. module_strings_fw.close()
  117. def update_translate(options):
  118. lang_code = options.langCode
  119. input_dir = options.inputDir
  120. cwd = os.getcwd()
  121. print("update_translate, cwd:{0}, lang_code:{1}, input_dir:${2}".format(
  122. cwd, lang_code, input_dir))
  123. module_include_dir_dict = {}
  124. for dir in FIND_DIDS:
  125. if dir != APP_MOUDULE:
  126. full_dir = os.path.join(cwd, dir)
  127. for module in os.listdir(full_dir):
  128. module_include_dir_dict[module] = dir
  129. for file_name in os.listdir(input_dir):
  130. if not file_name.endswith(".xls"):
  131. continue
  132. module = file_name.split('.')[0]
  133. if module == "":
  134. continue
  135. if module == APP_MOUDULE:
  136. module_update_translate(APP_MOUDULE, os.path.join(
  137. cwd, APP_MOUDULE), lang_code, input_dir)
  138. else:
  139. dir = module_include_dir_dict[module]
  140. module_include_dir = os.path.join(cwd, dir)
  141. module_update_translate(module, os.path.join(
  142. module_include_dir, module), lang_code, input_dir)
  143. options = option_parser()
  144. update_translate(options)