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"]:
for _js in self.jsons:
maxsim = 0 maxsim = 0
savet = None savet = None
for jc in self.json: for jc in _js:
dis = winsharedutils.distance_ratio(content, jc) dis = winsharedutils.distance_ratio(content, jc)
if dis > maxsim: if dis > maxsim:
maxsim = dis maxsim = dis
if maxsim * 100 >= globalconfig["premtsimi2"]: if maxsim * 100 >= globalconfig["premtsimi2"]:
if type(self.json[jc]) == str: if type(_js[jc]) == str:
savet = self.json[jc] savet = _js[jc]
elif ( elif _js[jc]["userTrans"] and _js[jc]["userTrans"] != "":
self.json[jc]["userTrans"] savet = _js[jc]["userTrans"]
and self.json[jc]["userTrans"] != ""
):
savet = self.json[jc]["userTrans"]
elif ( elif (
self.json[jc]["machineTrans"] _js[jc]["machineTrans"]
and self.json[jc]["machineTrans"] != "" and _js[jc]["machineTrans"] != ""
): ):
savet = self.json[jc]["machineTrans"] savet = _js[jc]["machineTrans"]
if savet is None: if savet is None:
raise Exception(f"can't find: {content}") continue
return savet
else: else:
if content not in self.json: collect.append(savet)
raise Exception(f"can't find: {content}")
if type(self.json[content]) == str: else:
return self.json[content] for _js in self.jsons:
elif ( if content not in _js:
self.json[content]["userTrans"] continue
and self.json[content]["userTrans"] != "" if type(_js[content]) == str:
): collect.append(_js[content])
return self.json[content]["userTrans"] elif _js[content]["userTrans"] and _js[content]["userTrans"] != "":
collect.append(_js[content]["userTrans"])
elif ( elif (
self.json[content]["machineTrans"] _js[content]["machineTrans"] and _js[content]["machineTrans"] != ""
and self.json[content]["machineTrans"] != ""
): ):
return self.json[content]["machineTrans"] collect.append(_js[content]["machineTrans"])
if len(collect) == 0:
raise Exception(f"can't find: {content}")
return "\n".join(collect)