Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit 7a9ecbd

Browse files
committed
· 增加了对.lang文件的支持
1 parent fa1cac7 commit 7a9ecbd

File tree

6 files changed

+56
-24
lines changed

6 files changed

+56
-24
lines changed

dist/ui/translation.png

18.9 KB
Loading

run/lang.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def loadLang(langFile):
2+
readLang = langFile.readlines()
3+
4+
output = {}
5+
for item in readLang:
6+
if item[0] != '#' and '=' in item:
7+
items = item.split('=')
8+
if items[1][-1] == '\n':
9+
items[1] = items[1][: -1]
10+
output[items[0]] = items[1]
11+
return output

run/progress.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def __init__(self):
2323
def setConfigStatus(self, status):
2424
self.progressBar.setValue(1)
2525
if not status:
26-
QMessageBox.critical(self, '错误', 'config文件不存在,已重新创建,请填写后重新打开应用', QMessageBox.StandardButton.Ok)
26+
QMessageBox.critical(self, '错误', 'config文件不存在,已重新创建,请填写后重新打开应用',
27+
QMessageBox.StandardButton.Ok)
28+
self.close()
2729
return
2830
self.progressBar.setValue(2)
2931
self.progressLabel.setText('API鉴权中...')

run/request.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,27 @@ def translation(jar, word, authToken):
1414
'q': word
1515
}
1616
transHost = 'https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=' + authToken
17-
while True:
17+
for i in range(3):
1818
transResponse = post(transHost, headers=header, params=params)
1919
if transResponse.status_code != 200:
20-
jar.addStatus('请求失败,请检查网络情况,5秒后重试')
20+
jar.addStatus('请求失败,请检查网络情况,5秒后重试...({}/3)'.format(i + 1))
2121
QApplication.processEvents()
2222
sleep(5)
2323
continue
2424
if 'error_code' in transResponse.json().keys():
25-
jar.addStatus('翻译错误: {}: {}'.format(transResponse.json()['error_code'], transResponse.json()['error_msg']))
26-
return False
25+
jar.addStatus('翻译错误: {}: {}, 5秒后重试...({}/3)'.format(transResponse.json()['error_code'],
26+
transResponse.json()['error_msg'], i + 1))
27+
continue
2728
return transResponse.json()['result']['trans_result'][0]
2829

2930

3031
def transJson(jar, js, authToken):
3132
outputJson = {}
32-
jar.addStatus('正在翻译,请稍等')
33-
QApplication.processEvents()
33+
processNow = 0
3434
for key in js:
35+
processNow += 1
36+
jar.addStatus('正在翻译...({}/{})'.format(processNow, len(js)))
37+
QApplication.processEvents()
3538
outputJson[key] = translation(jar, js[key], authToken)
3639
jar.addStatus('翻译完成')
3740
QApplication.processEvents()

run/translator.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from PyQt6.QtWidgets import QMainWindow, QFileDialog, QApplication
88

9+
from run.lang import loadLang
910
from run.request import transJson
1011
from ui.MainWindow import Ui_MainWindow
1112

@@ -21,8 +22,9 @@ def __init__(self, authBack):
2122
self.fileName = None
2223
self.workDir = None
2324
self.unzipDir = None
24-
self.originWork = 'en_us.json'
25-
self.oldWork = 'zh_cn.json'
25+
self.workType = ['.json', '.lang']
26+
self.originWork = 'en_us'
27+
self.oldWork = 'zh_cn'
2628
self.hasOldWork = False
2729
self.exportWork = join(getcwd(), 'export.json')
2830
self.authToken = authBack
@@ -53,18 +55,26 @@ def isTranslatable(self):
5355
for root, _, files in walk(self.unzipDir):
5456
for filesIndex in range(len(files)):
5557
files[filesIndex] = files[filesIndex].lower()
56-
if self.originWork in files:
57-
self.originWork = join(root, self.originWork)
58-
if not self.isRestarted:
59-
if self.oldWork in files:
60-
self.oldWork = join(root, self.oldWork)
61-
self.hasOldWork = True
62-
self.addStatus('已找到\'zh_cn.json\'文件')
63-
QApplication.processEvents()
64-
self.addStatus('已找到\'en_us.json\'文件')
65-
QApplication.processEvents()
66-
return True
58+
for fileType in self.workType:
59+
thisOriginWork = self.originWork + fileType
60+
thisOldWork = self.oldWork + fileType
61+
if thisOriginWork in files:
62+
self.originWork = thisOriginWork
63+
self.workType = fileType
64+
self.addStatus('已找到\'{}\'文件'.format(self.originWork))
65+
QApplication.processEvents()
66+
self.originWork = join(root, self.originWork)
67+
if not self.isRestarted:
68+
if thisOldWork in files:
69+
self.oldWork = thisOldWork
70+
self.addStatus('已找到\'{}\'文件'.format(self.oldWork))
71+
QApplication.processEvents()
72+
self.oldWork = join(root, self.oldWork)
73+
self.hasOldWork = True
74+
return True
6775
self.addStatus('未找到可翻译文件')
76+
QApplication.processEvents()
77+
self.ImportJar.setEnabled(True)
6878
return False
6979

7080
def nextItem(self):
@@ -98,6 +108,12 @@ def getTranslated(self):
98108
def setUnchecked(self, number):
99109
self.Unchecked.setText('还剩 {} 个未校对'.format(number))
100110

111+
def autoLoad(self, file):
112+
if self.workType == '.json':
113+
return load(file)
114+
else:
115+
return loadLang(file)
116+
101117
def onClickNext(self):
102118
if self.getTag():
103119
self.receiveChecked([self.getTag(), self.getTranslated()])
@@ -116,7 +132,7 @@ def onClickFinish(self):
116132
self.Finish.setDisabled(True)
117133
if self.hasOldWork:
118134
oldFile = open(self.oldWork, 'r', encoding='UTF-8')
119-
oldJson = load(oldFile)
135+
oldJson = self.autoLoad(oldFile)
120136
oldFile.close()
121137
for key in self.checkedJson:
122138
oldJson[key] = self.checkedJson[key]
@@ -151,11 +167,11 @@ def selectFile(self):
151167
return
152168
if self.isTranslatable():
153169
originFile = open(self.originWork, 'r', encoding='UTF-8')
154-
originJson = load(originFile)
170+
originJson = self.autoLoad(originFile)
155171
originFile.close()
156172
if self.hasOldWork:
157173
oldFile = open(self.oldWork, 'r', encoding='UTF-8')
158-
oldJson = load(oldFile)
174+
oldJson = self.autoLoad(oldFile)
159175
oldFile.close()
160176
preparedJson = {}
161177
for i in originJson:
@@ -178,7 +194,7 @@ def selectFile(self):
178194
self.transJson = transJson(self, originJson, self.authToken)
179195
dump(self.transJson, exportFile, indent=4)
180196
exportFile.close()
181-
self.addStatus('共 {} 条需要翻译'.format(self.items))
197+
self.addStatus('共 {} 条需要校对'.format(self.items))
182198
self.Next.setEnabled(True)
183199
self.transList = list(self.transJson.items())
184200
self.onClickNext()

ui/translation.ico

166 KB
Binary file not shown.

0 commit comments

Comments
 (0)