在上一个项目中用Python2.0实现了lua脚本中的中文正则匹配,然后自动生成配置脚本,便于翻译和本地化 传送门
而且支持忽略匹配CCLOG中的中文,但是没有支持忽略注释中的中文(双引号内的中文)
在这个项目中,改用Python3.0实现js脚本中中文正则匹配,相对于2.0有部分修改,主要是print函数,bytes和str类型进行区分。中文匹配字符串脚本,并生成ChineseForCehua.js文件给策划,log.txt日志文件,需要策划手动配置,适用于最后验收版本时一次性修改,需要在py当前路径自建文件new_js和old_js,将src文件夹放到old中,执行后将new覆盖到src
还在为代码中的中文需要策划配置好静态数据而沟通等待烦心吗?
快使用findChinese.py脚本(项目根目录)
使用时机:OP2封板
功能:遍历src中所有js文件,正则匹配中文,用CocosUtility.getClientMessage(“文件名N”)规则进行替换
执行方法:安装Python3.0 控制台运行>python findChinese.py
执行前提:在脚本当前目录创建文件夹文件new_js和old_js,将src文件夹拷贝到 old_js中
执行结果:在 new_js中会自动导出替换后的js,在脚本当前目录生成ChineseForCehua.js和log.txt两个文件,其中 ChineseForCehua.js交给策划配置, log.txt是日志文件,便于纠正
不用担心:会自动过滤”HelpTool.log”,”cc.log”,”console.log”,和注释中的中文
快快和我一起在代码里尽情的写中文吧,让策划专心出文档,免去了 找策划配中文 - 执行Jenkins - 更新静态数据git - 替换txt - 修改代码 这种繁琐的操作~
效果图:
完整代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import os
import importlib,sys
importlib.reload(sys)
# sys.setdefaultencoding('utf8')
print (sys.getdefaultencoding())
#list files
def listFiles(dirPath):
fileList=[]
for root,dirs,files in os.walk(dirPath):
for fileObj in files:
fileList.append(os.path.join(root,fileObj))
return fileList
def main():
fileDir = os.getcwd() + "/old_js"
fileTarget = os.getcwd() + "/new_js"
fileList = listFiles(fileDir)
logData = open("./log.txt", 'w+')
open("./ChineseForCehua.js", 'w+')
fData = open("ChineseForCehua.js", "r+", encoding='UTF-8', errors='ignore')
fData.seek(0, 0)
fData.truncate()
fData.write('var Localization = {\n')
#print(fileList)
for fileObj in fileList:
num = 1
strCounter = 1
f = open(fileObj,'r+', encoding='UTF-8', errors='ignore')
print("\nopen file :" + os.path.basename(fileObj), file=logData)
fileName = os.path.basename(fileObj).split('.')[0]
#print fileName
all_the_lines=f.readlines()
f.close()
strTable = []
numStrTable = []
numStrTable.append(fileName+' = {\n')
if ("protobuf" in fileObj) or (os.path.basename(fileObj).split('.')[1] != "js") or (fileName == "ChineseForCehua") or (fileName == "bit") :
print("do nothing ... ... ...", file=logData)
continue
print("analysising ... ... ...", file=logData)
for line in all_the_lines:
regex = re.compile("(['\"])(?:\\\.|.)*?\\1")
regex1 = re.compile(u"[\u4e00-\u9fa5]+")
regex2 = re.compile(u"HelpTool\.log")
regex3 = re.compile(u"cc\.log")
regex4 = re.compile(u"console\.log")
regex5 = re.compile("^(\s)*(\/)+")
line22 = line.encode('utf8') # writable line
line11 = line.encode('utf8') # temp line
line2 = line22.decode('utf8')
line1 = line11.decode('utf8')
# ingore print and cclog
results1 = regex2.findall(line1)
length1 = len(results1)
results2 = regex3.findall(line1)
length2 = len(results2)
results3 = regex4.findall(line1)
length3 = len(results3)
results4 = regex5.findall(line1)
length4 = len(results4)
if length1 + length2 + length3 + length4 == 0 :
# iterate over all found quotes pairs
for match in regex.finditer(line1):
start = match.start()
end = match.end()
temp = line1[start:end]
counter = 0
for match1 in regex1.finditer(temp):
counter += 1
if counter > 0:
nStr = 'CocosUtility.getClientMessage("%s'%fileName+"%s"%strCounter+'")'
print('replace at:%s'%num + " " + temp + " ------> " + nStr, file=logData)
line2 = line2.replace( temp, nStr)
mStr = 'str%s'%strCounter
numStrTable.append(mStr+' = '+temp+',\n')
strCounter += 1
strTable.append(line2)
num = num + 1
numStrTable.append('},\n')
# if fit str exits insert require str
if strCounter > 1 :
dirPath = fileObj.replace( "old_js", "new_js")
targetPath = dirPath.split(os.path.basename(dirPath))[0]
if not os.path.isdir(targetPath):
os.makedirs(targetPath)
# if os.path.exists(targetPath) == 0 :
# os.mkdir(targetPath)
targetF = open(dirPath,'w',encoding='utf-8')
targetF.seek(0, 0)
targetF.truncate()
# targetF.write('''local localization = require("ChineseForCehua").''' + fileName + "\n")
for lines in strTable :
targetF.write(lines)
targetF.close()
# write Localization data
for lines2 in numStrTable :
fData.write(lines2)
fData.write('}\nreturn Localization')
fData.close()
if __name__=='__main__':
main()
# Attention:ChineseForCehua.js 放在findChinese.py脚本同级目录,将LuaScripts文件夹拷贝到(同级目录下)old_js文件夹中,运行脚本,new_js(同级目录下)文件夹中为修改的文件