添加可选的翻译功能

This commit is contained in:
PikachuLiker
2020-11-02 22:18:17 +08:00
parent c1039872b9
commit fb453763df
4 changed files with 34 additions and 2 deletions

View File

@@ -451,3 +451,11 @@ def translateTag_to_sc(tag):
return tag
else:
return tag
def translate(src:str,target_language:str="zh_cn"):
url = f"https://translate.google.cn/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl=auto&tl={target_language}&q={src}"
result = get_html(url=url,return_type="object")
translate_list = [i["trans"] for i in result.json()["sentences"]]
return "".join(translate_list)

View File

@@ -31,3 +31,7 @@ folders=failed,JAV_output
[debug_mode]
switch=0
[transalte]
switch=0 # 是否开启翻译功能
values=title,outline #需要翻译的变量

View File

@@ -43,7 +43,10 @@ class Config:
return self.conf.getboolean("common", "auto_exit")
def transalte_to_sc(self) -> bool:
return self.conf.getboolean("common", "transalte_to_sc")
def is_transalte(self) -> bool:
return self.conf.getboolean("transalte", "switch")
def transalte_values(self) -> bool:
return self.conf.get("transalte", "values")
def proxy(self) -> [str, int, int, str]:
try:
sec = "proxy"
@@ -138,6 +141,11 @@ class Config:
conf.add_section(sec7)
conf.set(sec7, "switch", "0")
sec8 = "transalte"
conf.add_section(sec8)
conf.set(sec8, "switch", "0")
conf.set(sec8, "values", "title,outline")
return conf
@@ -157,3 +165,5 @@ if __name__ == "__main__":
print(config.escape_literals())
print(config.escape_folder())
print(config.debug())
print(config.is_transalte())
print(config.transalte_values())

12
core.py
View File

@@ -207,10 +207,20 @@ def get_data_from_json(file_number, filepath, conf: config.Config): # 从JSON
json_data['release'] = release
json_data['cover_small'] = cover_small
json_data['tag'] = tag
json_data['naming_rule'] = eval(conf.naming_rule())
json_data['location_rule'] = location_rule
json_data['year'] = year
json_data['actor_list'] = actor_list
if conf.is_transalte():
translate_values = conf.transalte_values().split(",")
for translate_value in translate_values:
json_data[translate_value] = translate(json_data[translate_value])
naming_rule=""
for i in conf.naming_rule().split("+"):
if i not in json_data:
naming_rule+=i.strip("'").strip('"')
else:
naming_rule+=json_data[i]
json_data['naming_rule'] = naming_rule
return json_data