ConversionToChineseKey.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # encoding: utf-8
  2. """
  3. 把翻译文件 全部转换为新的类型文件 "中文" = "Chinese"
  4. 1、转换源码翻译 字段为"NSLocalizedString(@"key", @"Chinese")"
  5. 2、转换翻译文档
  6. """
  7. import os
  8. import os.path
  9. import re
  10. import time
  11. import TranslationProcess
  12. import CoverProcess
  13. global tmpStoreZhTrans, tmpStoreEnTrans, tmpStoreHKTrans, tmpNoTransKeys, tmpNotNeedTransKeys, fileCount
  14. tmpStoreZhTrans = dict() # 中文翻译 对应key保存字典
  15. tmpStoreEnTrans = dict() # 英文翻译
  16. tmpStoreHKTrans = dict() # 繁体
  17. fileCount = 1
  18. tmpNotNeedTransKeys = dict() # 不需要翻译的字段
  19. tmpNoTransKeys = dict() # 没有翻译的字段
  20. # 读取翻译文件
  21. def readTransformFile():
  22. global tmpStoreZhTrans, tmpNotNeedTransKeys
  23. TranslationProcess.readLanguageFilesToTmp()
  24. for zh in TranslationProcess.zhHansFile:
  25. key, value = removeTheTranslateLine(zh)
  26. if len(key) > 0:
  27. tmpStoreZhTrans[key] = value
  28. with open(CoverProcess.savePath + "/NotNeedTrans.txt", "r", encoding="utf-8") as tFile:
  29. datas = tFile.read().splitlines()
  30. for str in datas:
  31. tmpNotNeedTransKeys[str] = "1"
  32. # 转换翻译文件
  33. def switchTransformFile():
  34. global tmpStoreEnTrans, tmpStoreHKTrans, tmpStoreZhTrans
  35. readTransformFile()
  36. for en in TranslationProcess.enFile:
  37. key, value = removeTheTranslateLine(en)
  38. if len(key) > 0:
  39. zhKey = tmpStoreZhTrans.get(key)
  40. if zhKey == None:
  41. zhKey = key
  42. tmpStoreEnTrans[zhKey] = value
  43. for hk in TranslationProcess.zhHKFile:
  44. key, value = removeTheTranslateLine(hk)
  45. if len(key) > 0:
  46. zhKey = tmpStoreZhTrans.get(key)
  47. if zhKey == None:
  48. zhKey = key
  49. tmpStoreHKTrans[zhKey] = value
  50. print("fileOk")
  51. zhTrans = dict()
  52. for zh in TranslationProcess.zhHansFile:
  53. key, value = removeTheTranslateLine(zh)
  54. if len(key) > 0:
  55. zhKey = tmpStoreZhTrans.get(key)
  56. if zhKey == None:
  57. zhKey = key
  58. zhTrans[zhKey] = value
  59. #writeToPath(zhTrans, TranslationProcess.zhHansPath)
  60. #writeToPath(tmpStoreHKTrans, TranslationProcess.zhHantPath)
  61. #writeToPath(tmpStoreHKTrans, TranslationProcess.zhHKPath)
  62. #writeToPath(tmpStoreEnTrans, TranslationProcess.enPath)
  63. # 写入文件
  64. def writeToPath(file = {}, path = ""):
  65. content = ""
  66. header = "/*\nLocalizable.strings\nYunPOS\n\nCreated by Vyron_Lee on 2018/11/22.\nCopyright © 2018 莫艳. All rights reserved.\n*/\n"
  67. content += header
  68. for key in file.keys():
  69. content += ("\"" + key + "\" = \"" + file.get(key) + "\";\n")
  70. with open(path, "w", encoding="utf-8") as tFile:
  71. tFile.write(content)
  72. # 去除 引号,分号,空格,返回key,value 值
  73. def removeTheTranslateLine(line = ""):
  74. return TranslationProcess.removeTheTranslateLine(line)
  75. # 查找文件中 翻译
  76. def findTheLocalizedString(filePath):
  77. global tmpStoreZhTrans, tmpNoTransKeys, fileCount
  78. print("\r处理第 " + str(fileCount) + " 文件")
  79. fileCount += 1
  80. tmpFile = ""
  81. hasChange = False
  82. with open(filePath, 'r', encoding="utf-8") as mFile:
  83. datas = mFile.read().splitlines()
  84. for line in datas:
  85. hasLocalized = CoverProcess.regularLocalizedKey.findall(line)
  86. for localizedString in hasLocalized:
  87. key = getLocalizedKey(localizedString)
  88. if len(key) > 0:
  89. value = tmpStoreZhTrans.get(key)
  90. if value != None:
  91. #replace = "@\"" + value + "\".localized"
  92. #line = line.replace(localizedString, replace)
  93. #hasChange = True
  94. print("**翻译了")
  95. elif tmpNotNeedTransKeys.get(key) != None:
  96. print("***不需要翻译")
  97. else:
  98. tmpNoTransKeys[key] = ""
  99. print("*****未找到翻译的:" + key)
  100. tmpFile += (line + "\n")
  101. if hasChange == True and 0:
  102. with open(filePath, "w", encoding="utf-8") as changeFile:
  103. changeFile.write(tmpFile)
  104. # 提取代码中翻译的key值 @"".localized
  105. def getLocalizedKey(str = ""):
  106. str = str.strip(".localized")
  107. str = str.strip("@")
  108. str = str.strip("\"")
  109. return str
  110. # 提取代码中翻译的key值 NSLocalizedString(@"", @"")
  111. def getNSLocalizedStringKey(str = ""):
  112. str = str.strip("NSLocalizedString")
  113. str = str.strip("(")
  114. str = str.strip(")")
  115. result = str.split(",")
  116. key = result[0]
  117. key = key.strip()
  118. key = key.strip("@")
  119. key = key.strip("\"")
  120. return key
  121. # 遍历 path 文件夹下所有 .m 文件
  122. def beginSearch(path):
  123. for root, dirs, files in os.walk(path, topdown=True):
  124. if len(files) > 0:
  125. for fileName in files:
  126. if os.path.splitext(fileName)[1] == ".m":
  127. realPath = root + "/" + fileName
  128. findTheLocalizedString(realPath)
  129. # if len(dirs) > 0:
  130. # for subPath in dirs:
  131. # realPath = root + "/" + subPath
  132. # beginSearch(realPath)
  133. if __name__ == "__main__":
  134. project = CoverProcess.dirPath
  135. print("begin!")
  136. # 翻译转换
  137. #switchTransformFile()
  138. # 提取翻译
  139. readTransformFile()
  140. beginSearch(project)
  141. if len(tmpNoTransKeys) > 0:
  142. fileName = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  143. CoverProcess.saveSetInPath(tmpNoTransKeys.keys(), CoverProcess.savePath + "/" + fileName + ".txt")
  144. print("end!")
  145. # 保存未翻译的字段