This commit is contained in:
恍兮惚兮 2024-06-10 00:25:35 +08:00
parent bf6a098ef9
commit 2161dfb86e

View File

@ -9,16 +9,16 @@ import gobject
class TS(basetrans):
def checkfilechanged(self, p1, p):
if self.paths != (p1, p):
self.json = {}
self.jsons = []
if p:
for pp in p:
if os.path.exists(pp):
with open(pp, "r", encoding="utf8") as f:
self.json.update(json.load(f))
self.jsons.append(json.load(f))
if p1:
if os.path.exists(p1):
with open(p1, "r", encoding="utf8") as f:
self.json.update(json.load(f))
self.jsons.append(json.load(f))
self.paths = (p1, p)
def unsafegetcurrentgameconfig(self):
@ -32,50 +32,51 @@ class TS(basetrans):
def inittranslator(self):
self.paths = (None, None)
self.checkfilechanged(
self.unsafegetcurrentgameconfig(), self.config["jsonfile"]
self.unsafegetcurrentgameconfig(), tuple(self.config["jsonfile"])
)
def translate(self, content):
self.checkfilechanged(
self.unsafegetcurrentgameconfig(), self.config["jsonfile"]
self.unsafegetcurrentgameconfig(), tuple(self.config["jsonfile"])
)
collect = []
if globalconfig["premtsimiuse"]:
maxsim = 0
savet = None
for jc in self.json:
dis = winsharedutils.distance_ratio(content, jc)
if dis > maxsim:
maxsim = dis
if maxsim * 100 >= globalconfig["premtsimi2"]:
if type(self.json[jc]) == str:
savet = self.json[jc]
elif (
self.json[jc]["userTrans"]
and self.json[jc]["userTrans"] != ""
):
savet = self.json[jc]["userTrans"]
for _js in self.jsons:
maxsim = 0
savet = None
for jc in _js:
dis = winsharedutils.distance_ratio(content, jc)
if dis > maxsim:
maxsim = dis
if maxsim * 100 >= globalconfig["premtsimi2"]:
if type(_js[jc]) == str:
savet = _js[jc]
elif _js[jc]["userTrans"] and _js[jc]["userTrans"] != "":
savet = _js[jc]["userTrans"]
elif (
_js[jc]["machineTrans"]
and _js[jc]["machineTrans"] != ""
):
savet = _js[jc]["machineTrans"]
if savet is None:
continue
else:
collect.append(savet)
elif (
self.json[jc]["machineTrans"]
and self.json[jc]["machineTrans"] != ""
):
savet = self.json[jc]["machineTrans"]
if savet is None:
raise Exception(f"can't find: {content}")
return savet
else:
if content not in self.json:
raise Exception(f"can't find: {content}")
if type(self.json[content]) == str:
return self.json[content]
elif (
self.json[content]["userTrans"]
and self.json[content]["userTrans"] != ""
):
return self.json[content]["userTrans"]
for _js in self.jsons:
if content not in _js:
continue
if type(_js[content]) == str:
collect.append(_js[content])
elif _js[content]["userTrans"] and _js[content]["userTrans"] != "":
collect.append(_js[content]["userTrans"])
elif (
self.json[content]["machineTrans"]
and self.json[content]["machineTrans"] != ""
):
return self.json[content]["machineTrans"]
elif (
_js[content]["machineTrans"] and _js[content]["machineTrans"] != ""
):
collect.append(_js[content]["machineTrans"])
if len(collect) == 0:
raise Exception(f"can't find: {content}")
return "\n".join(collect)