TranslationProcess.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. """
  2. 整理翻译文件,通过匹配翻译的excel表格,写入未翻译的字段
  3. """
  4. import CoverProcess as func
  5. import langconv
  6. # 读取 excel
  7. import xlrd
  8. from xlrd.book import Book
  9. from xlrd.sheet import Sheet
  10. from xlrd.sheet import Cell
  11. """
  12. 基础配置文件
  13. :param excellPath: 翻译excel表格路径
  14. :param translationSheetName: 翻译表单名称
  15. """
  16. isSave = True
  17. excelPath = func.savePath + "/fanyi.xlsx"
  18. translationSheetName = "翻译登记表" # 历史翻译【所有】/ 翻译登记表
  19. baseLanguagePath = func.projectPath + "/YunPOS-iOS/YunPOS/language"
  20. # 翻译文件路径
  21. zhTransFilePath = func.savePath + "/aa.txt"
  22. # 翻译文件路径
  23. zhHansPath = baseLanguagePath + "/zh-Hans.lproj/Localizable.strings"
  24. zhHantPath = baseLanguagePath + "/zh-Hant.lproj/Localizable.strings"
  25. zhHKPath = baseLanguagePath + "/zh-HK.lproj/Localizable.strings"
  26. enPath = baseLanguagePath + "/en.lproj/Localizable.strings"
  27. # excel 暂存
  28. global tmpExcelFile
  29. tmpExcelFile = None
  30. tmpExcelIndex = dict() # excel 索引
  31. # 需要翻译的字段列表
  32. global zhNeedList
  33. zhNeedList = []
  34. # 文件存储
  35. global zhHansFile, zhHantFile, zhHKFile, enFile
  36. global tmpStoreZhTrans
  37. zhHansFile = [] # 简体中文
  38. tmpStoreZhTrans = dict() # 简体中文
  39. zhHantFile = [] # 繁体中文
  40. zhHKFile = [] # 繁体中文
  41. enFile = [] # 英文
  42. global tmpNotNeedTransKeys
  43. tmpNotNeedTransKeys = dict() # 不需要翻译的字段
  44. # 判读 是否已经翻译了
  45. # return: True 已经存在翻译;False 没有存在翻译;
  46. def matchExcelTranslationToStr(aRow = []):
  47. zhValue = aRow[0]
  48. zhResult = func.findWordInFile(zhValue)
  49. if zhResult:
  50. for zh in zhResult:
  51. if "=" in zh:
  52. return True
  53. return False
  54. # excel 相关
  55. # 读取excel表格/获取翻译sheet表
  56. def readExcelFile(excelPath, sheetName):
  57. excel = xlrd.open_workbook(excelPath)
  58. translation = excel.sheet_by_name(sheetName)
  59. return translation;
  60. # 读入翻译文件至 缓存中
  61. def readLanguageFilesToTmp():
  62. global zhHansFile, zhHantFile, zhHKFile, enFile
  63. global tmpStoreZhTrans, tmpNotNeedTransKeys
  64. # 中文简体
  65. zhHansFile = readFileNoReturn(zhHansPath)
  66. # 中文繁体
  67. zhHantFile = readFileNoReturn(zhHantPath)
  68. # 中文繁体
  69. zhHKFile = readFileNoReturn(zhHKPath)
  70. # 英文
  71. enFile = readFileNoReturn(enPath)
  72. # 中文简体暂存为 Dict,方便查询是否已经翻译了
  73. for zh in zhHansFile:
  74. key, value = removeTheTranslateLine(zh)
  75. if len(key) > 0:
  76. tmpStoreZhTrans[key] = value
  77. try:
  78. with open(func.savePath + "/NotNeedTrans.txt", "r", encoding="utf-8") as tFile:
  79. datas = tFile.read().splitlines()
  80. for str in datas:
  81. tmpNotNeedTransKeys[str] = "1"
  82. except IOError:
  83. print("File is not accessible.")
  84. # 去除 引号,分号,空格,返回key,value 值
  85. def removeTheTranslateLine(line = ""):
  86. if "=" not in line:
  87. return "", ""
  88. line = line.split(";")[0]
  89. result = line.split("=")
  90. tmpKey = ""
  91. tmpValue = ""
  92. if len(result) == 4:
  93. # 包含 = 的翻译
  94. tmpKey = result[0] + "=" + result[1]
  95. tmpValue = result[2] + "=" + result[3]
  96. elif len(result) == 3:
  97. tmpKey = result[0]
  98. tmpValue = result[1] + "=" + result[2]
  99. elif len(result) == 2:
  100. tmpKey = result[0]
  101. tmpValue = result[1]
  102. else:
  103. print("error")
  104. key = tmpKey.strip()
  105. value = tmpValue.strip()
  106. key = key.strip("\"")
  107. value = value.strip("\"")
  108. return key, value
  109. # 读取文件,并去除换行符号
  110. def readFileNoReturn(path):
  111. with open(path) as file:
  112. datas = file.read().splitlines()
  113. return datas
  114. return None
  115. # 读取需要翻译的文件
  116. def readNeedTranslationFile():
  117. global zhNeedList
  118. if True:
  119. tmpFile = open(zhTransFilePath, 'r', encoding="utf-8")
  120. zhNeedList = list(tmpFile)
  121. tmpFile.close()
  122. else:
  123. # 读取中文翻译文件
  124. func.readZhLocalizedFile()
  125. func.isReplace = True
  126. # 筛选需要翻译的数据
  127. subTranslateStr, subBeTranslateStr = func.search(func.dirPath)
  128. zhNeedList = subTranslateStr
  129. # 去除首尾可能存在的空格
  130. def dealStartEndSpace(str = ""):
  131. str = str.strip()
  132. return str
  133. # 去除翻译文字\n 和 "
  134. def dealStartEndChar(str = ""):
  135. str = str.strip("\n")
  136. str = str.strip("\"")
  137. return str
  138. # 转换简体到繁体
  139. def chsToCht(line):
  140. line = langconv.Converter('zh-hant').convert(line)
  141. line.encode('utf-8')
  142. return line
  143. # 生成翻译文件 line
  144. def createTranslateLine(zh, translate):
  145. return "\""+zh+"\""+" = "+"\""+translate+"\";";
  146. """
  147. # 查看字段是否在excel中是否已经翻译
  148. # return: 成功 返回 中文,英文,繁体
  149. # 失败 Null
  150. """
  151. def searchZhTranslationInExcel(zh = "", excel = excelPath, sheet = translationSheetName):
  152. global tmpExcelFile, tmpExcelIndex
  153. # 读入excel文件
  154. if tmpExcelFile == None:
  155. tmpExcelFile = readExcelFile(excel, sheet)
  156. tmpExcelIndex = createExcelIndexDict(tmpExcelFile)
  157. indexStr = tmpExcelIndex.get(zh)
  158. if indexStr != None:
  159. index = int(indexStr)
  160. row = tmpExcelFile.row_values(index)
  161. """
  162. if (index > 1 and index < 2644) or (index > 2953):
  163. print(str(index) + "--匹配成功")
  164. enStr = dealStartEndSpace(row[1])
  165. if len(enStr) > 0:
  166. return zh, enStr, chsToCht(zh)
  167. else:
  168. print("--录入,但是未翻译:" + zh)
  169. """
  170. print(str(index) + "--匹配成功")
  171. enStr = dealStartEndSpace(row[2])
  172. if len(enStr) > 0:
  173. return zh, enStr, chsToCht(zh)
  174. else:
  175. print("--录入,但是未翻译:" + zh)
  176. return zh, None, None
  177. # 生成excel索引
  178. def createExcelIndexDict(tmpExcelFile = []):
  179. tmpExcelIndex = dict()
  180. rowCount = 0 # 行数,<2644 行文件格式 "中文" "英文" "繁体" >2644 格式:"None" "中文" "英文" "繁体" >2953 情况1
  181. spaceCount = 0 # 中间空行记录,最多10行,当>10就认为结束了
  182. for row in tmpExcelFile.get_rows():
  183. if (len(row[0].value) == 0 and len(row[1].value) == 0) or row[0].value == " ":
  184. spaceCount += 1
  185. if spaceCount > 10:
  186. break
  187. else:
  188. rowCount += 1
  189. continue
  190. """
  191. if (rowCount > 1 and rowCount < 2644) or (rowCount > 2953):
  192. zhStr = dealStartEndSpace(row[0].value)
  193. tmpExcelIndex[zhStr] = str(rowCount)
  194. """
  195. zhStr = dealStartEndSpace(row[1].value)
  196. tmpExcelIndex[zhStr] = str(rowCount)
  197. if len(row[1].value) > 0:
  198. spaceCount = 0
  199. rowCount += 1
  200. return tmpExcelIndex
  201. """
  202. 写入翻译到 对应的翻译文件中去
  203. 总共有4个翻译文件 -中文 -英文 -繁体 -繁体
  204. """
  205. def writeTranslateToFile(zh, en, hk):
  206. zhStr = createTranslateLine(zh, zh)
  207. hkStr = createTranslateLine(zh, hk)
  208. enStr = createTranslateLine(zh, en)
  209. zhHansFile.append(zhStr)
  210. zhHantFile.append(hkStr)
  211. zhHKFile.append(hkStr)
  212. enFile.append(enStr)
  213. zh
  214. print("插入翻译: ", zhStr, enStr, hkStr)
  215. # 写入翻译文件
  216. def writeTmpToTrans():
  217. func.saveSetInPath(zhHansFile, zhHansPath)
  218. func.saveSetInPath(zhHantFile, zhHantPath)
  219. func.saveSetInPath(zhHKFile, zhHKPath)
  220. func.saveSetInPath(enFile, enPath)
  221. print("写入文件完成")
  222. # 主运行
  223. if __name__ == "__main__":
  224. # 读取文件缓存
  225. readNeedTranslationFile()
  226. readLanguageFilesToTmp()
  227. # 循环遍历需要翻译的字段
  228. zhNoTrans = set()
  229. for zhStr in zhNeedList:
  230. zhStr = dealStartEndChar(zhStr)
  231. zhNoTrans.add(zhStr)
  232. print("begin\n匹配:" + zhStr)
  233. zh, en, hk = searchZhTranslationInExcel(zhStr, excelPath, translationSheetName)
  234. if zh != None:
  235. if tmpStoreZhTrans.get(zh) == None:
  236. if en != None:
  237. writeTranslateToFile(zh, en, hk)
  238. zhNoTrans.remove(zhStr)
  239. print("---end\n")
  240. # 保存处理的文件
  241. func.saveSetInPath(zhNoTrans, zhTransFilePath)
  242. if isSave == True:
  243. writeTmpToTrans()
  244. # 结果
  245. #print("\n已经翻译:" + str(succCount) + "\n没有翻译:" + str(falureCount) + "\n总处理:" + str(rowCount))