deep-wine-runner/trans/__init__.py

42 lines
1.0 KiB
Python
Raw Normal View History

2022-12-05 22:21:33 +08:00
#!/usr/bin/env python3
import os
import json
import requests
import traceback
2023-03-19 14:58:20 +08:00
typeList = [
"Auto",
"ZH_CN2JA",
"ZH_CN2KR",
"ZH_CN2EN"
]
2022-12-05 22:21:33 +08:00
class Trans():
isTrans = False
unCloudTrans = False
2022-12-05 22:21:33 +08:00
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:
2022-12-07 10:21:50 +08:00
return self.word[text].replace("", "(").replace("", ")")
2022-12-05 22:21:33 +08:00
except:
# 网络翻译接口已废弃
return text