1 |
- # encoding: utf-8
import os
import os.path
import re
import TranslationProcess
"""
配置字段
:param isReplace: 匹配修改内容开关,false-只是查找出没有翻译的字段,并不作替换处理;
:param dirPath: 需要修改的文件路径,会遍历该文件夹下所有的文件;
:param savePath: 保存结果文件地址
:param projectPath: 项目文件路径
:param zhLocalizablePath: 翻译文件路径,一般配置中文翻译的路径;
"""
isReplace = True
savePath = "./Translate"
projectPath = "/Users/poto/Documents/Rengonsoft"
dirPath = projectPath + "/YunPOS-iOS/YunPOS" #Modules
folders = {"Modules", "BLL"}
zhLocalizablePath = projectPath + "/YunPOS-iOS/YunPOS/language/zh-Hans.lproj/Localizable.strings"
"""
全局变量
fileCount 处理文件计数
translateLog 涉及到需要翻译的文件名称记录
localStringFile 本地翻译文件内容缓存
"""
global fileCount, translateLog
fileCount = 0
translateLog = set()
# 中文翻译文件
global tmpStoreZhTrans
tmpStoreZhTrans = dict()
"""
匹配规则
"""
regularString = re.compile(r"@\"[^\"]*[\u4E00-\u9FA5]+[^\"\\n]*?\"")
regularNotString = re.compile(r"^((?!NSLog|DeLog|NSAssert|Ignore-Localization).)*$")
# 读取项目翻译文件
def readZhLocalizedFile():
global tmpStoreZhTrans
TranslationProcess.readLanguageFilesToTmp()
tmpStoreZhTrans = TranslationProcess.tmpStoreZhTrans
# 搜索开始
def search(path):
for root, dirs, files in os.walk(path, topdown=True):
if len(files) > 0:
# 搜素 文件内容
process(root, files)
# if len(dirs) > 0:
# for subPath in dirs:
# # 递归搜索子文件夹的内容
# realPath = root + "/" + subPath
# search(realPath)
"""
执行翻译 核心方法
:return translateStr: 返回需要翻译的字段
beTranslateStr: 已经有翻译,但是没有翻译的字段
"""
def process(path, files):
global fileCount
global translateLog
for fileName in files:
if os.path.splitext(fileName)[1] == ".m":
realPath = path + "/" + fileName
hasChange = False
try:
with open(realPath, "r", encoding="utf-8") as tFile:
fileContent = ""
for line in tFile:
tmpExtra = regularNotString.findall(line)
if len(tmpExtra) == 0 or line.strip().startswith("//"):
fileContent += line
continue
totalZh = regularString.findall(line)
for zh in totalZh:
zhLocalized = zh + ".localized"
zhKey = removeExtraChar(zh)
if TranslationProcess.tmpNotNeedTransKeys.get(zhKey) == None:
if zhLocalized not in line:
# 没有存在 翻译的,需要加入翻译标识符 .localized
line = line.replace(zh, zhLocalized)
hasChange = True
# 翻译字段
value = tmpStoreZhTrans.get(zhKey)
if value == None:
# 遍历 翻译excel
zhT, enT, hkT = TranslationProcess.searchZhTranslationInExcel(zhKey)
if enT != None:
TranslationProcess.writeTranslateToFile(zhT, enT, hkT)
tmpStoreZhTrans[zhT] = zhT
else:
# 没翻译
print("未翻译---" + zhKey)
translateLog.add(zhKey)
fileContent += line
if hasChange == True and isReplace == True:
with open(realPath, "w", encoding="utf-8") as changeFile:
changeFile.write(fileContent)
except IOError:
print("read error! path = " + realPath)
# 处理进度计数器
fileCount += 1
print("已处理文件:", fileCount)
# 去除多余值
def removeExtraChar(str = ""):
str = str.strip("@")
str = str.strip("\"")
return str
# 保存搜索结果
def saveSetInPath(setObj, path):
# set 转 str
content = ""
for str in setObj:
content += str + "\n"
with open(path, "w", encoding="utf-8") as file:
file.write(content)
if __name__ == "__main__":
# 读取翻译文件
readZhLocalizedFile()
tmpZhCount = len(tmpStoreZhTrans)
# 搜索方法
# 遍历 foler 查询
for str in folders:
path = dirPath + "/" + str
search(path)
# 插入新翻译 字段,保存
if len(tmpStoreZhTrans) > tmpZhCount:
TranslationProcess.writeTmpToTrans()
if len(translateLog) > 0:
saveSetInPath(translateLog, savePath + "/translateLog.txt")
print("处理完成-总文件数:%d 需要翻译的:%d \n 结果路径:%s" %(fileCount, len(translateLog), savePath))
|