'init
This commit is contained in:
163
app.py
Normal file
163
app.py
Normal file
@@ -0,0 +1,163 @@
|
||||
# from importlib.resources import path
|
||||
import os
|
||||
import re
|
||||
import string
|
||||
import time
|
||||
|
||||
from BaiduTrans import transBaidu
|
||||
from trans import getinfo,postData,transI18,transWord
|
||||
from tclient import tenTrans
|
||||
import requests
|
||||
import random
|
||||
|
||||
|
||||
|
||||
# 处理单个文件
|
||||
def transFile(fileName):
|
||||
# 生成新的数据data [lines的形式]
|
||||
newData=[]
|
||||
print('执行文件===>',fileName)
|
||||
# 读取每一行
|
||||
with open(fileName,'r',encoding='utf8') as fLines:
|
||||
# for line in fLines.readlines(): #依次读取每行
|
||||
for index,line in enumerate(fLines.readlines()): #依次读取每行
|
||||
newline = line.strip() #去掉每行头尾空白 -获取字符串
|
||||
# print ("读取的数据为: %s" % (line))
|
||||
# .* 表示任意匹配除换行符(\n、\r)之外的任何单个或多个字符
|
||||
# (.*?) 表示"非贪婪"模式,===>只保存第一个匹配到的子串
|
||||
# ^title: |^label: | :使用模板字符串 =使用{}
|
||||
matchObj = re.match( r'(.*text: |.*title: |.*label: |.*label=|.*placeholder=|.*message: |.*placeholder: |.*title=)["||\'](.*?)["||\']', newline, re.M|re.I) #匹配title/label开始---- '/"结束 | re.M|
|
||||
|
||||
matchObjsingal = re.match( r'(.*text: |.*title: |.*label: |.*message: |.*placeholder: )[\'](.*?)[\']', newline, re.M|re.I) #匹配title/label开始---- '/"结束 | re.M|
|
||||
matchObjmore = re.match( r'(.*text: |.*title: |.*label: |.*message: |.*placeholder: )["](.*?)["]', newline, re.M|re.I) #匹配title/label开始---- '/"结束 | re.M|
|
||||
|
||||
matchquerysl= re.match( r'(.*label=|.*placeholder=|.*title=)[\'](.*?)[\']', newline, re.M|re.I) #匹配title/label开始---- '/"结束 | re.M|
|
||||
matchquerymore= re.match( r'(.*label=|.*placeholder=|.*title=)["](.*?)["]', newline, re.M|re.I) #匹配title/label开始---- '/"结束 | re.M|
|
||||
|
||||
# 识别中文字符
|
||||
znRe=re.match( r'.*["||\']([\u4e00-\u9fa5]+?)["||\']', newline, re.M|re.I) #匹配title/label开始---- '/"结束 | re.M|
|
||||
znResigal=re.match( r'.*["||\']([\u4e00-\u9fa5]+?)["||\']', newline, re.M|re.I) #匹配title/label开始---- '/"结束 | re.M|
|
||||
znRemore=re.match( r'.*["||\']([\u4e00-\u9fa5]+?)["||\']', newline, re.M|re.I) #匹配title/label开始---- '/"结束 | re.M|
|
||||
|
||||
|
||||
|
||||
|
||||
# 匹配
|
||||
if matchObj:
|
||||
# if False:
|
||||
print ("原始语句------------------------ : ", matchObj.group()) #label: "hello'
|
||||
# print ("matchObj.group(1) : ", matchObj.group(1)) #标识符 label:
|
||||
# print ("matchObj.group(2) : ", matchObj.group(2)) #要替换的字符串 hello
|
||||
oldWord=matchObj.group(2)
|
||||
# enWord=transBaidu(oldWord,'zh','en') # 百度-执行中文翻译
|
||||
enWord=tenTrans(oldWord,'zh','en') # 执行中文翻译
|
||||
Idword=tenTrans(oldWord,'zh','id') # 执行印尼翻译 --api不支持
|
||||
key=transI18(oldWord,enWord,Idword)
|
||||
globalKey='`${'+'window.i18n("{key}")'.format(key=key)+'}`'
|
||||
reline=line
|
||||
if matchObjsingal:
|
||||
reline=line.replace("'{}'".format(oldWord),globalKey,1) #仅全局替换1次字符
|
||||
elif matchObjmore:
|
||||
reline=line.replace("\"{}\"".format(oldWord),globalKey,1) #仅全局替换1次字符
|
||||
elif matchquerysl:
|
||||
if '==' not in line or '?' not in line:
|
||||
globalKey='{'+'`${'+'window.i18n("{key}")'.format(key=key)+'}`'+'}'
|
||||
reline=line.replace("'{}'".format(oldWord),globalKey,1) #仅全局替换1次字符
|
||||
elif matchquerymore:
|
||||
# 判断?或者==表达式 [排除翻译]
|
||||
if '==' not in line or '?' not in line:
|
||||
globalKey='{'+'`${'+'window.i18n("{key}")'.format(key=key)+'}`'+'}'
|
||||
reline=line.replace("\"{}\"".format(oldWord),globalKey,1) #仅全局替换1次字符
|
||||
#replace替换-字符串
|
||||
# reline=line.replace("'{}'".format(oldWord),globalKey,1) #仅全局替换1次字符
|
||||
print('globalKey--',globalKey)
|
||||
print('替换语句------------------------ ',reline)
|
||||
#每行写入
|
||||
newData.append(reline)
|
||||
else:
|
||||
# 识别中文字符
|
||||
newznList=re.findall("[\u4e00-\u9fa5]+",newline)
|
||||
newznListsignal=re.findall('[\u4e00-\u9fa5]+',newline)
|
||||
# newznListMore=re.findall('[\u4e00-\u9fa5]+',newline)
|
||||
# newznList=re.findall('["||\'][\u4e00-\u9fa5]+["||\']',newline)
|
||||
# newznListsignal=re.findall('[\'][\u4e00-\u9fa5]+[\']',newline)
|
||||
# newznListMore=re.findall('["][\u4e00-\u9fa5]+["]',newline)
|
||||
print('newznList',newznList)
|
||||
print('newznListsignal',newznListsignal)
|
||||
# print('newznListMore',newznListMore)
|
||||
|
||||
|
||||
|
||||
|
||||
# 没有中文字符
|
||||
if(len(newznList)==0):
|
||||
# print ("No match!!")
|
||||
newData.append(line)
|
||||
else:
|
||||
znline=line
|
||||
# znline=tensWord(line,newznList)
|
||||
for index,znWord in enumerate(newznList):
|
||||
# print('newList',newznList)
|
||||
# 翻译中文单词-英文
|
||||
# enWord=transBaidu(znWord,'zh','en') # 百度-执行中文翻译
|
||||
enWord=tenTrans(znWord,'zh','en') # 执行中文翻译
|
||||
Idword=tenTrans(znWord,'zh','id') # 执行印尼翻译 --api不支持
|
||||
enwordkey=transI18(znWord,enWord,Idword)
|
||||
print('znWord---enWord',znWord,enWord)
|
||||
enwordKey='`${'+'window.i18n("{key}")'.format(key=enwordkey)+'}`'
|
||||
# znline=znline.replace(znWord,enwordKey,1) #仅全局替换1次字符
|
||||
if znWord in newznListsignal:
|
||||
print('单个')
|
||||
znline=znline.replace("'{}'".format(znWord),enwordKey,1) #仅全局替换1次字符
|
||||
znline=znline.replace("\"{}\"".format(znWord),enwordKey,1) #仅全局替换1次字符
|
||||
# znline=znline.replace(znWord,enwordKey,1) #仅全局替换1次字符
|
||||
|
||||
print('znline1--',znline)
|
||||
|
||||
# if znWord in newznListMore:
|
||||
# print('双引号')
|
||||
# print('znline222--',znline)
|
||||
# znline=znline.replace("\"{}\"".format(znWord),enwordKey,1) #仅全局替换1次字符
|
||||
# # znline=znline.replace(znWord,enwordKey,1) #仅全局替换1次字符
|
||||
# print('znline2--',znline)
|
||||
|
||||
# znline=znline.replace("\"{}\"".format(znWord),enwordKey,1) #仅全局替换1次字符
|
||||
# print('znline--',znline)
|
||||
print('znline--',znline)
|
||||
newData.append(znline)
|
||||
|
||||
|
||||
|
||||
|
||||
# 写入文件
|
||||
with open(fileName,'w',encoding='utf8') as newfLines:
|
||||
newfLines.writelines(newData)
|
||||
|
||||
def listFile():
|
||||
newDir = input('请输入文件或者目录:')
|
||||
# 判断类型: 文件还是目录
|
||||
#1. 文件类型
|
||||
if(os.path.isfile(newDir)):
|
||||
transFile(newDir)
|
||||
# pass
|
||||
else:
|
||||
#2. 目录类型
|
||||
print('遍历目录',newDir)
|
||||
for root, dirs, files in os.walk(eval(repr(newDir.replace('"', '')))):
|
||||
# for root, dirs, files in os.walk(eval(repr(".")) , topdown=False):
|
||||
# 遍历子目录
|
||||
for name in dirs:
|
||||
pass
|
||||
# 遍历文件名
|
||||
for name in files:
|
||||
fileName=os.path.join(os.getcwd(), root,name) #windows系统异常
|
||||
print(fileName)
|
||||
transFile(fileName)
|
||||
|
||||
|
||||
# 主程序-打包exe
|
||||
if __name__=='__main__':
|
||||
listFile()
|
||||
input("按任意键回车停止")
|
||||
print("已停止")
|
||||
|
||||
Reference in New Issue
Block a user