export_untranslate.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding:utf-8 -*-
  2. import os
  3. from optparse import OptionParser
  4. from xml.dom import minidom
  5. import pyExcelerator
  6. APP_MOUDULE = 'app'
  7. FIND_DIDS = [APP_MOUDULE, 'frame', 'module']
  8. def option_parser():
  9. parser = OptionParser()
  10. parser.add_option("-l", "--langCode",
  11. help="语言码",
  12. metavar="langCode")
  13. parser.add_option("-o", "--outputDir",
  14. help="输出路径",
  15. metavar="outputDir")
  16. (options, _) = parser.parse_args()
  17. return options
  18. def read_xml_2_dom(path):
  19. f = open(path, 'r')
  20. try:
  21. dom = minidom.parse(f)
  22. f.close()
  23. return dom
  24. except:
  25. print('xml_2_dom error, path:{}'.format(path))
  26. f.close()
  27. return
  28. def read_strings_2_dict(strings_path):
  29. string_dict = {}
  30. if not os.path.exists(strings_path):
  31. return string_dict
  32. dom = read_xml_2_dom(strings_path)
  33. if dom == None:
  34. return
  35. root = dom.documentElement
  36. strings = root.getElementsByTagName('string')
  37. for string in strings:
  38. translatable = string.getAttribute('translatable')
  39. if translatable == 'false':
  40. continue
  41. key = string.getAttribute('name')
  42. # print("read_strings_2_dict, key:{0}".format(key))
  43. value = string.firstChild.data
  44. string_dict[key] = value
  45. return string_dict
  46. def module_export_untranslate(module_name, module_dir, lang_code, output_dir):
  47. print("module_export_untranslate, module_name:{0}, module_dir:{1}".format(
  48. module_name, module_dir))
  49. all_en_strings = os.path.join(
  50. module_dir, 'src/main/res/values/strings.xml')
  51. all_zh_strings = os.path.join(
  52. module_dir, 'src/main/res/values-zh/strings.xml')
  53. to_handle_strings = os.path.join(
  54. module_dir, 'src/main/res/values-{}/strings.xml'.format(lang_code))
  55. en_strings_dict = read_strings_2_dict(all_en_strings)
  56. zh_strings_dict = read_strings_2_dict(all_zh_strings)
  57. to_handle_strings_dict = read_strings_2_dict(to_handle_strings)
  58. module_untranslate_list = [['keyName', 'en', 'zh']]
  59. for k, v in en_strings_dict.items():
  60. if not to_handle_strings_dict.has_key(k):
  61. zh_value = zh_strings_dict.get(k)
  62. module_untranslate_list.append([k, v, zh_value])
  63. if len(module_untranslate_list) <= 1:
  64. return
  65. workbook = pyExcelerator.Workbook()
  66. ws = workbook.add_sheet(module_name)
  67. row_index = 0
  68. for row in module_untranslate_list:
  69. column_index = 0
  70. for column in row:
  71. # print("module_export_untranslate, row_index:{0}, column_index:{1}, column:{2}".format(
  72. # row_index, column_index, column.encode('utf-8')))
  73. ws.write(row_index, column_index, column)
  74. column_index += 1
  75. row_index += 1
  76. output_path = os.path.join(output_dir, '{}.xls'.format(module_name))
  77. workbook.save(output_path)
  78. def export_untranslate(options):
  79. lang_code = options.langCode
  80. output_dir = options.outputDir
  81. cwd = os.getcwd()
  82. print("export_untranslate, cwd:{0}, lang_code:{1}, output_dir:${2}".format(
  83. cwd, lang_code, output_dir))
  84. for dir in FIND_DIDS:
  85. full_dir = os.path.join(cwd, dir)
  86. if not os.path.exists(full_dir):
  87. continue
  88. if dir == APP_MOUDULE:
  89. module_export_untranslate(
  90. APP_MOUDULE, full_dir, lang_code, output_dir)
  91. else:
  92. for module in os.listdir(full_dir):
  93. module_export_untranslate(
  94. module, os.path.join(full_dir, module), lang_code, output_dir)
  95. options = option_parser()
  96. export_untranslate(options)