123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- # encoding: utf-8
- """
- 把翻译文件 全部转换为新的类型文件 "中文" = "Chinese"
- 1、转换源码翻译 字段为"NSLocalizedString(@"key", @"Chinese")"
- 2、转换翻译文档
- """
- import os
- import os.path
- import re
- import time
- import TranslationProcess
- import CoverProcess
- global tmpStoreZhTrans, tmpStoreEnTrans, tmpStoreHKTrans, tmpNoTransKeys, tmpNotNeedTransKeys, fileCount
- tmpStoreZhTrans = dict() # 中文翻译 对应key保存字典
- tmpStoreEnTrans = dict() # 英文翻译
- tmpStoreHKTrans = dict() # 繁体
- fileCount = 1
- tmpNotNeedTransKeys = dict() # 不需要翻译的字段
- tmpNoTransKeys = dict() # 没有翻译的字段
- # 读取翻译文件
- def readTransformFile():
- global tmpStoreZhTrans, tmpNotNeedTransKeys
- TranslationProcess.readLanguageFilesToTmp()
- for zh in TranslationProcess.zhHansFile:
- key, value = removeTheTranslateLine(zh)
- if len(key) > 0:
- tmpStoreZhTrans[key] = value
- with open(CoverProcess.savePath + "/NotNeedTrans.txt", "r", encoding="utf-8") as tFile:
- datas = tFile.read().splitlines()
- for str in datas:
- tmpNotNeedTransKeys[str] = "1"
- # 转换翻译文件
- def switchTransformFile():
- global tmpStoreEnTrans, tmpStoreHKTrans, tmpStoreZhTrans
- readTransformFile()
- for en in TranslationProcess.enFile:
- key, value = removeTheTranslateLine(en)
- if len(key) > 0:
- zhKey = tmpStoreZhTrans.get(key)
- if zhKey == None:
- zhKey = key
- tmpStoreEnTrans[zhKey] = value
- for hk in TranslationProcess.zhHKFile:
- key, value = removeTheTranslateLine(hk)
- if len(key) > 0:
- zhKey = tmpStoreZhTrans.get(key)
- if zhKey == None:
- zhKey = key
- tmpStoreHKTrans[zhKey] = value
- print("fileOk")
- zhTrans = dict()
- for zh in TranslationProcess.zhHansFile:
- key, value = removeTheTranslateLine(zh)
- if len(key) > 0:
- zhKey = tmpStoreZhTrans.get(key)
- if zhKey == None:
- zhKey = key
- zhTrans[zhKey] = value
- #writeToPath(zhTrans, TranslationProcess.zhHansPath)
- #writeToPath(tmpStoreHKTrans, TranslationProcess.zhHantPath)
- #writeToPath(tmpStoreHKTrans, TranslationProcess.zhHKPath)
- #writeToPath(tmpStoreEnTrans, TranslationProcess.enPath)
- # 写入文件
- def writeToPath(file = {}, path = ""):
- content = ""
- header = "/*\nLocalizable.strings\nYunPOS\n\nCreated by Vyron_Lee on 2018/11/22.\nCopyright © 2018 莫艳. All rights reserved.\n*/\n"
- content += header
- for key in file.keys():
- content += ("\"" + key + "\" = \"" + file.get(key) + "\";\n")
- with open(path, "w", encoding="utf-8") as tFile:
- tFile.write(content)
- # 去除 引号,分号,空格,返回key,value 值
- def removeTheTranslateLine(line = ""):
- return TranslationProcess.removeTheTranslateLine(line)
- # 查找文件中 翻译
- def findTheLocalizedString(filePath):
- global tmpStoreZhTrans, tmpNoTransKeys, fileCount
- print("\r处理第 " + str(fileCount) + " 文件")
- fileCount += 1
- tmpFile = ""
- hasChange = False
- with open(filePath, 'r', encoding="utf-8") as mFile:
- datas = mFile.read().splitlines()
- for line in datas:
- hasLocalized = CoverProcess.regularLocalizedKey.findall(line)
- for localizedString in hasLocalized:
- key = getLocalizedKey(localizedString)
- if len(key) > 0:
- value = tmpStoreZhTrans.get(key)
- if value != None:
- #replace = "@\"" + value + "\".localized"
- #line = line.replace(localizedString, replace)
- #hasChange = True
- print("**翻译了")
- elif tmpNotNeedTransKeys.get(key) != None:
- print("***不需要翻译")
- else:
- tmpNoTransKeys[key] = ""
- print("*****未找到翻译的:" + key)
- tmpFile += (line + "\n")
- if hasChange == True and 0:
- with open(filePath, "w", encoding="utf-8") as changeFile:
- changeFile.write(tmpFile)
- # 提取代码中翻译的key值 @"".localized
- def getLocalizedKey(str = ""):
- str = str.strip(".localized")
- str = str.strip("@")
- str = str.strip("\"")
- return str
- # 提取代码中翻译的key值 NSLocalizedString(@"", @"")
- def getNSLocalizedStringKey(str = ""):
- str = str.strip("NSLocalizedString")
- str = str.strip("(")
- str = str.strip(")")
- result = str.split(",")
- key = result[0]
- key = key.strip()
- key = key.strip("@")
- key = key.strip("\"")
- return key
- # 遍历 path 文件夹下所有 .m 文件
- def beginSearch(path):
- for root, dirs, files in os.walk(path, topdown=True):
- if len(files) > 0:
- for fileName in files:
- if os.path.splitext(fileName)[1] == ".m":
- realPath = root + "/" + fileName
- findTheLocalizedString(realPath)
- # if len(dirs) > 0:
- # for subPath in dirs:
- # realPath = root + "/" + subPath
- # beginSearch(realPath)
- if __name__ == "__main__":
- project = CoverProcess.dirPath
- print("begin!")
- # 翻译转换
- #switchTransformFile()
- # 提取翻译
- readTransformFile()
- beginSearch(project)
- if len(tmpNoTransKeys) > 0:
- fileName = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- CoverProcess.saveSetInPath(tmpNoTransKeys.keys(), CoverProcess.savePath + "/" + fileName + ".txt")
- print("end!")
- # 保存未翻译的字段
|