""" 整理翻译文件,通过匹配翻译的excel表格,写入未翻译的字段 """ import CoverProcess as func import langconv # 读取 excel import xlrd from xlrd.book import Book from xlrd.sheet import Sheet from xlrd.sheet import Cell """ 基础配置文件 :param excellPath: 翻译excel表格路径 :param translationSheetName: 翻译表单名称 """ isSave = True excelPath = func.savePath + "/fanyi.xlsx" translationSheetName = "翻译登记表" # 历史翻译【所有】/ 翻译登记表 baseLanguagePath = func.projectPath + "/YunPOS-iOS/YunPOS/language" # 翻译文件路径 zhTransFilePath = func.savePath + "/aa.txt" # 翻译文件路径 zhHansPath = baseLanguagePath + "/zh-Hans.lproj/Localizable.strings" zhHantPath = baseLanguagePath + "/zh-Hant.lproj/Localizable.strings" zhHKPath = baseLanguagePath + "/zh-HK.lproj/Localizable.strings" enPath = baseLanguagePath + "/en.lproj/Localizable.strings" # excel 暂存 global tmpExcelFile tmpExcelFile = None tmpExcelIndex = dict() # excel 索引 # 需要翻译的字段列表 global zhNeedList zhNeedList = [] # 文件存储 global zhHansFile, zhHantFile, zhHKFile, enFile global tmpStoreZhTrans zhHansFile = [] # 简体中文 tmpStoreZhTrans = dict() # 简体中文 zhHantFile = [] # 繁体中文 zhHKFile = [] # 繁体中文 enFile = [] # 英文 global tmpNotNeedTransKeys tmpNotNeedTransKeys = dict() # 不需要翻译的字段 # 判读 是否已经翻译了 # return: True 已经存在翻译;False 没有存在翻译; def matchExcelTranslationToStr(aRow = []): zhValue = aRow[0] zhResult = func.findWordInFile(zhValue) if zhResult: for zh in zhResult: if "=" in zh: return True return False # excel 相关 # 读取excel表格/获取翻译sheet表 def readExcelFile(excelPath, sheetName): excel = xlrd.open_workbook(excelPath) translation = excel.sheet_by_name(sheetName) return translation; # 读入翻译文件至 缓存中 def readLanguageFilesToTmp(): global zhHansFile, zhHantFile, zhHKFile, enFile global tmpStoreZhTrans, tmpNotNeedTransKeys # 中文简体 zhHansFile = readFileNoReturn(zhHansPath) # 中文繁体 zhHantFile = readFileNoReturn(zhHantPath) # 中文繁体 zhHKFile = readFileNoReturn(zhHKPath) # 英文 enFile = readFileNoReturn(enPath) # 中文简体暂存为 Dict,方便查询是否已经翻译了 for zh in zhHansFile: key, value = removeTheTranslateLine(zh) if len(key) > 0: tmpStoreZhTrans[key] = value try: with open(func.savePath + "/NotNeedTrans.txt", "r", encoding="utf-8") as tFile: datas = tFile.read().splitlines() for str in datas: tmpNotNeedTransKeys[str] = "1" except IOError: print("File is not accessible.") # 去除 引号,分号,空格,返回key,value 值 def removeTheTranslateLine(line = ""): if "=" not in line: return "", "" line = line.split(";")[0] result = line.split("=") tmpKey = "" tmpValue = "" if len(result) == 4: # 包含 = 的翻译 tmpKey = result[0] + "=" + result[1] tmpValue = result[2] + "=" + result[3] elif len(result) == 3: tmpKey = result[0] tmpValue = result[1] + "=" + result[2] elif len(result) == 2: tmpKey = result[0] tmpValue = result[1] else: print("error") key = tmpKey.strip() value = tmpValue.strip() key = key.strip("\"") value = value.strip("\"") return key, value # 读取文件,并去除换行符号 def readFileNoReturn(path): with open(path) as file: datas = file.read().splitlines() return datas return None # 读取需要翻译的文件 def readNeedTranslationFile(): global zhNeedList if True: tmpFile = open(zhTransFilePath, 'r', encoding="utf-8") zhNeedList = list(tmpFile) tmpFile.close() else: # 读取中文翻译文件 func.readZhLocalizedFile() func.isReplace = True # 筛选需要翻译的数据 subTranslateStr, subBeTranslateStr = func.search(func.dirPath) zhNeedList = subTranslateStr # 去除首尾可能存在的空格 def dealStartEndSpace(str = ""): str = str.strip() return str # 去除翻译文字\n 和 " def dealStartEndChar(str = ""): str = str.strip("\n") str = str.strip("\"") return str # 转换简体到繁体 def chsToCht(line): line = langconv.Converter('zh-hant').convert(line) line.encode('utf-8') return line # 生成翻译文件 line def createTranslateLine(zh, translate): return "\""+zh+"\""+" = "+"\""+translate+"\";"; """ # 查看字段是否在excel中是否已经翻译 # return: 成功 返回 中文,英文,繁体 # 失败 Null """ def searchZhTranslationInExcel(zh = "", excel = excelPath, sheet = translationSheetName): global tmpExcelFile, tmpExcelIndex # 读入excel文件 if tmpExcelFile == None: tmpExcelFile = readExcelFile(excel, sheet) tmpExcelIndex = createExcelIndexDict(tmpExcelFile) indexStr = tmpExcelIndex.get(zh) if indexStr != None: index = int(indexStr) row = tmpExcelFile.row_values(index) """ if (index > 1 and index < 2644) or (index > 2953): print(str(index) + "--匹配成功") enStr = dealStartEndSpace(row[1]) if len(enStr) > 0: return zh, enStr, chsToCht(zh) else: print("--录入,但是未翻译:" + zh) """ print(str(index) + "--匹配成功") enStr = dealStartEndSpace(row[2]) if len(enStr) > 0: return zh, enStr, chsToCht(zh) else: print("--录入,但是未翻译:" + zh) return zh, None, None # 生成excel索引 def createExcelIndexDict(tmpExcelFile = []): tmpExcelIndex = dict() rowCount = 0 # 行数,<2644 行文件格式 "中文" "英文" "繁体" >2644 格式:"None" "中文" "英文" "繁体" >2953 情况1 spaceCount = 0 # 中间空行记录,最多10行,当>10就认为结束了 for row in tmpExcelFile.get_rows(): if (len(row[0].value) == 0 and len(row[1].value) == 0) or row[0].value == " ": spaceCount += 1 if spaceCount > 10: break else: rowCount += 1 continue """ if (rowCount > 1 and rowCount < 2644) or (rowCount > 2953): zhStr = dealStartEndSpace(row[0].value) tmpExcelIndex[zhStr] = str(rowCount) """ zhStr = dealStartEndSpace(row[1].value) tmpExcelIndex[zhStr] = str(rowCount) if len(row[1].value) > 0: spaceCount = 0 rowCount += 1 return tmpExcelIndex """ 写入翻译到 对应的翻译文件中去 总共有4个翻译文件 -中文 -英文 -繁体 -繁体 """ def writeTranslateToFile(zh, en, hk): zhStr = createTranslateLine(zh, zh) hkStr = createTranslateLine(zh, hk) enStr = createTranslateLine(zh, en) zhHansFile.append(zhStr) zhHantFile.append(hkStr) zhHKFile.append(hkStr) enFile.append(enStr) zh print("插入翻译: ", zhStr, enStr, hkStr) # 写入翻译文件 def writeTmpToTrans(): func.saveSetInPath(zhHansFile, zhHansPath) func.saveSetInPath(zhHantFile, zhHantPath) func.saveSetInPath(zhHKFile, zhHKPath) func.saveSetInPath(enFile, enPath) print("写入文件完成") # 主运行 if __name__ == "__main__": # 读取文件缓存 readNeedTranslationFile() readLanguageFilesToTmp() # 循环遍历需要翻译的字段 zhNoTrans = set() for zhStr in zhNeedList: zhStr = dealStartEndChar(zhStr) zhNoTrans.add(zhStr) print("begin\n匹配:" + zhStr) zh, en, hk = searchZhTranslationInExcel(zhStr, excelPath, translationSheetName) if zh != None: if tmpStoreZhTrans.get(zh) == None: if en != None: writeTranslateToFile(zh, en, hk) zhNoTrans.remove(zhStr) print("---end\n") # 保存处理的文件 func.saveSetInPath(zhNoTrans, zhTransFilePath) if isSave == True: writeTmpToTrans() # 结果 #print("\n已经翻译:" + str(succCount) + "\n没有翻译:" + str(falureCount) + "\n总处理:" + str(rowCount))