2026-01-12 17:16:33 +08:00
|
|
|
from tencentcloud.common import credential
|
|
|
|
|
from tencentcloud.tmt.v20180321 import tmt_client, models
|
|
|
|
|
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
|
|
|
|
|
import json
|
2026-01-13 16:51:39 +08:00
|
|
|
from TranslateParent import TranslateParent
|
2026-01-12 17:16:33 +08:00
|
|
|
|
|
|
|
|
|
2026-01-13 16:51:39 +08:00
|
|
|
class TencentCloud(TranslateParent):
|
2026-01-12 17:16:33 +08:00
|
|
|
def __init__(self, ):
|
|
|
|
|
from main import server
|
|
|
|
|
self.api_id, self.api_key = server.tell_tencent_api()
|
|
|
|
|
|
|
|
|
|
def translate_list(self, tag_list: list, target_language: str = "zh") -> dict:
|
|
|
|
|
try:
|
|
|
|
|
cred = credential.Credential(self.api_id, self.api_key)
|
|
|
|
|
client = tmt_client.TmtClient(cred, "ap-guangzhou")
|
|
|
|
|
req = models.TextTranslateBatchRequest()
|
|
|
|
|
params = {
|
|
|
|
|
"Source": "en",
|
|
|
|
|
"Target": target_language,
|
|
|
|
|
"ProjectId": 0,
|
|
|
|
|
"SourceTextList": tag_list,
|
|
|
|
|
}
|
|
|
|
|
req.from_json_string(json.dumps(params))
|
|
|
|
|
resp = client.TextTranslateBatch(req)
|
|
|
|
|
response = json.loads(resp.to_json_string())
|
|
|
|
|
return {"status": True, "translate_list": response["TargetTextList"]}
|
|
|
|
|
except TencentCloudSDKException as err:
|
|
|
|
|
print(err)
|
|
|
|
|
return {"status": False}
|