LunaTranslator/py/trans_lang.py

94 lines
2.2 KiB
Python
Raw Normal View History

2024-07-27 14:02:19 +08:00
import sys, time
2024-08-17 11:01:48 +08:00
sys.path.insert(0, "./LunaTranslator")
from translator.baidu_ai import TS
2024-07-27 14:02:19 +08:00
2024-01-08 23:37:00 +08:00
class TS1(TS):
2024-07-27 14:02:19 +08:00
@property
def srclang(self):
return "zh"
@property
def tgtlang(self):
return self.xxxxx
@tgtlang.setter
def tgtlang(self, _):
self.xxxxx = _
if __name__ == "__main__":
import os, json
f = "zh.json"
with open("./files/lang/" + f, "r", encoding="utf8") as ff:
js = ff.read()
js = json.loads(js)
xxx = {
"ru": "ru",
"en": "en",
"es": "spa",
"ko": "kor",
"fr": "fra",
"cht": "cht",
"ja": "jp",
"vi": "vie",
"tr": "tr",
"pl": "pl",
"uk": "ukr",
"it": "it",
"ar": "ara",
"th": "th",
"de": "de",
"sv": "swe",
"nl": "nl",
2024-07-27 20:35:42 +08:00
"cs": "cs",
2024-07-30 14:04:29 +08:00
"pt": "pt",
2024-07-27 14:02:19 +08:00
}
needpop = []
2024-01-08 23:37:00 +08:00
for k in js:
2024-07-27 14:02:19 +08:00
kk = False
try:
k.encode("ascii")
print(k)
kk = True
except:
pass
if k not in js or kk:
needpop.append(k)
2024-01-08 23:37:00 +08:00
for k in needpop:
2024-07-27 14:02:19 +08:00
js.pop(k)
with open(f"./files/lang/" + f, "w", encoding="utf8") as ff:
ff.write(json.dumps(js, ensure_ascii=False, sort_keys=False, indent=4))
a = TS1("baiduapi")
2024-01-08 23:37:00 +08:00
for kk in xxx:
2024-07-27 14:02:19 +08:00
with open(f"./files/lang/{kk}.json", "r", encoding="utf8") as ff:
jsen = json.loads(ff.read())
a.tgtlang = xxx[kk]
needpop = []
for k in jsen:
if k not in js:
needpop.append(k)
print(kk, needpop)
2024-01-08 23:37:00 +08:00
for k in needpop:
2024-07-27 14:02:19 +08:00
jsen.pop(k)
with open(f"./files/lang/{kk}.json", "w", encoding="utf8") as ff:
ff.write(json.dumps(jsen, ensure_ascii=False, sort_keys=False, indent=4))
2024-01-08 23:37:00 +08:00
for k in js:
2024-07-27 14:02:19 +08:00
if k not in jsen or jsen[k] == "":
jsen[k] = list(a.translate(k))[0]
print(k, jsen[k])
with open(f"./files/lang/{kk}.json", "w", encoding="utf8") as ff:
ff.write(
json.dumps(jsen, ensure_ascii=False, sort_keys=False, indent=4)
)