30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
|
|
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
|
||
|
|
|
||
|
|
|
||
|
|
class TencentCloud:
|
||
|
|
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}
|