mirror of
https://gitee.com/gfdgd-xi/deep-wine-runner
synced 2025-12-14 02:52:03 +08:00
更新翻译
This commit is contained in:
50
trans/__init__.py
Normal file
50
trans/__init__.py
Normal file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import json
|
||||
import requests
|
||||
import traceback
|
||||
|
||||
class Trans():
|
||||
isTrans = False
|
||||
word = {}
|
||||
fileName = ""
|
||||
|
||||
def __init__(self, lang="zh_CN", fileName=f"trans.json") -> None:
|
||||
self.fileName = fileName
|
||||
self.isTrans = (lang != "zh_CN")
|
||||
if self.isTrans:
|
||||
try:
|
||||
if not os.path.exists(fileName):
|
||||
with open(fileName, "w") as file:
|
||||
file.write("{}")
|
||||
with open(fileName, "r") as file:
|
||||
self.word = json.loads(file.read())
|
||||
except:
|
||||
traceback.print_exc()
|
||||
self.isTrans = False
|
||||
|
||||
def transe(self, temp, text) -> str:
|
||||
if not self.isTrans:
|
||||
return text
|
||||
try:
|
||||
return self.word[text]
|
||||
except:
|
||||
# 机翻
|
||||
data = { 'doctype': 'json', 'type': 'auto','i': text}
|
||||
jsonReturn = requests.post("http://fanyi.youdao.com/translate", data=data).json()["translateResult"]
|
||||
transText = ""
|
||||
for i in jsonReturn:
|
||||
print(i[0])
|
||||
transText += f'{i[0]["tgt"]}\n'
|
||||
if "\n" in text:
|
||||
transText = transText.replace("\n\n", "\n")[:-1]
|
||||
else:
|
||||
transText = transText[:-1]
|
||||
self.word[text] = transText
|
||||
try:
|
||||
with open(self.fileName, "w") as file:
|
||||
file.write(json.dumps(self.word, ensure_ascii=False))
|
||||
except:
|
||||
traceback.print_exc()
|
||||
print(f"{text}=>{transText}")
|
||||
return transText
|
||||
BIN
trans/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
trans/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
1
trans/deepin-wine-runner.json
Normal file
1
trans/deepin-wine-runner.json
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user