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