export_no_translate_complete.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding:utf-8 -*-
  2. # 导出哪些翻译未翻译(找出个别的几个没有翻译, 或者多余的词条)
  3. import os
  4. from optparse import OptionParser
  5. from xml.dom import minidom
  6. import pyExcelerator
  7. APP_MOUDULE = 'app'
  8. FIND_DIDS = [APP_MOUDULE, 'frame', 'module']
  9. LANGUAGE_CODE = ['', 'zh', 'ar', 'hi', 'tr', 'th']
  10. def option_parser():
  11. parser = OptionParser()
  12. parser.add_option("-o", "--outputDir",
  13. help="输出路径",
  14. metavar="outputDir")
  15. (options, _) = parser.parse_args()
  16. return options
  17. def read_xml_2_dom(path):
  18. f = open(path, 'r')
  19. try:
  20. dom = minidom.parse(f)
  21. f.close()
  22. return dom
  23. except:
  24. print('xml_2_dom error, path:{}'.format(path))
  25. f.close()
  26. return
  27. def read_strings_2_dict(strings_path):
  28. string_dict = {}
  29. if not os.path.exists(strings_path):
  30. return string_dict
  31. dom = read_xml_2_dom(strings_path)
  32. if dom == None:
  33. return
  34. root = dom.documentElement
  35. strings = root.getElementsByTagName('string')
  36. for string in strings:
  37. translatable = string.getAttribute('translatable')
  38. if translatable == 'false':
  39. continue
  40. key = string.getAttribute('name')
  41. # print("read_strings_2_dict, key:{0}".format(key))
  42. value = string.firstChild.data
  43. string_dict[key] = value
  44. return string_dict
  45. def module_export_untranslate(module_name, module_dir, output_dir):
  46. print("module_export_untranslate, module_name:{0}, module_dir:{1}".format(
  47. module_name, module_dir))
  48. all_en_strings = os.path.join(
  49. module_dir, 'src/main/res/values/strings.xml')
  50. all_zh_strings = os.path.join(
  51. module_dir, 'src/main/res/values-zh/strings.xml')
  52. all_ar_strings = os.path.join(
  53. module_dir, 'src/main/res/values-ar/strings.xml')
  54. all_hi_strings = os.path.join(
  55. module_dir, 'src/main/res/values-hi/strings.xml')
  56. all_tr_strings = os.path.join(
  57. module_dir, 'src/main/res/values-tr/strings.xml')
  58. all_th_strings = os.path.join(
  59. module_dir, 'src/main/res/values-th/strings.xml')
  60. en_strings_dict = read_strings_2_dict(all_en_strings)
  61. zh_strings_dict = read_strings_2_dict(all_zh_strings)
  62. ar_strings_dict = read_strings_2_dict(all_ar_strings)
  63. hi_strings_dict = read_strings_2_dict(all_hi_strings)
  64. tr_strings_dict = read_strings_2_dict(all_tr_strings)
  65. th_strings_dict = read_strings_2_dict(all_th_strings)
  66. all_keys = set()
  67. all_keys.update(en_strings_dict.keys())
  68. all_keys.update(zh_strings_dict.keys())
  69. all_keys.update(ar_strings_dict.keys())
  70. all_keys.update(hi_strings_dict.keys())
  71. all_keys.update(tr_strings_dict.keys())
  72. all_keys.update(th_strings_dict.keys())
  73. module_untranslate_list = [['keyName', 'en', 'zh', 'ar', 'hi', 'tr', 'th']]
  74. for key in all_keys:
  75. if not en_strings_dict.has_key(key) or not zh_strings_dict.has_key(key) or not ar_strings_dict.has_key(
  76. key) or not hi_strings_dict.has_key(key) or not tr_strings_dict.has_key(key) or not th_strings_dict.has_key(key):
  77. en_value = en_strings_dict.get(key)
  78. zh_value = zh_strings_dict.get(key)
  79. ar_value = ar_strings_dict.get(key)
  80. hi_value = hi_strings_dict.get(key)
  81. tr_value = tr_strings_dict.get(key)
  82. th_value = th_strings_dict.get(key)
  83. module_untranslate_list.append([key, en_value, zh_value, ar_value, hi_value, tr_value, th_value])
  84. if len(module_untranslate_list) <= 1:
  85. return
  86. workbook = pyExcelerator.Workbook()
  87. ws = workbook.add_sheet(module_name)
  88. row_index = 0
  89. for row in module_untranslate_list:
  90. column_index = 0
  91. print(row)
  92. for column in row:
  93. # print("module_export_untranslate, row_index:{0}, column_index:{1}, column:{2}".format(
  94. # row_index, column_index, column.encode('utf-8')))
  95. columnStr = column
  96. if column is None:
  97. columnStr = ""
  98. ws.write(row_index, column_index, columnStr)
  99. column_index += 1
  100. row_index += 1
  101. output_path = os.path.join(output_dir, '{}.xls'.format(module_name))
  102. workbook.save(output_path)
  103. def export_untranslate(options):
  104. output_dir = options.outputDir
  105. cwd = os.getcwd()
  106. print("export_untranslate, cwd:{0}, output_dir:${1}".format(cwd, output_dir))
  107. for dir in FIND_DIDS:
  108. full_dir = os.path.join(cwd, dir)
  109. if not os.path.exists(full_dir):
  110. continue
  111. if dir == APP_MOUDULE:
  112. module_export_untranslate(APP_MOUDULE, full_dir, output_dir)
  113. else:
  114. for module in os.listdir(full_dir):
  115. module_export_untranslate(
  116. module, os.path.join(full_dir, module), output_dir)
  117. options = option_parser()
  118. export_untranslate(options)