7. 翻译
7.1 需要依赖
<tencentcloud.version>3.1.317</tencentcloud.version>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>${tencentcloud.version}</version>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
</dependency>
7.2 使用
package com.smart.hnj.common.utils;
import com.smart.hnj.common.model.TencentSecret;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tmt.v20180321.TmtClient;
import com.tencentcloudapi.tmt.v20180321.models.*;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Translate {
public static String enToZh(String content) {
try {
String secretId = TencentSecret.SECRET_ID;
String secretKey = TencentSecret.SECRET_KEY;
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("tmt.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
TmtClient client = new TmtClient(cred, "ap-shanghai", clientProfile);
TextTranslateRequest req = new TextTranslateRequest();
req.setSourceText(content);
req.setSource("en");
req.setTarget("zh");
req.setProjectId(0L);
TextTranslateResponse resp = client.TextTranslate(req);
return resp.getTargetText();
} catch (TencentCloudSDKException e) {
log.info("腾讯云接口请求错误,错误信息:{}", e.toString());
return null;
}
}
public static String zhToEn(String content) {
try {
String secretId = TencentSecret.SECRET_ID;
String secretKey = TencentSecret.SECRET_KEY;
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("tmt.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
TmtClient client = new TmtClient(cred, "ap-shanghai", clientProfile);
TextTranslateRequest req = new TextTranslateRequest();
req.setSourceText(content);
req.setSource("zh");
req.setTarget("en");
req.setProjectId(0L);
TextTranslateResponse resp = client.TextTranslate(req);
return resp.getTargetText();
} catch (TencentCloudSDKException e) {
log.info("腾讯云接口请求错误,错误信息:{}", e.toString());
return null;
}
}
}
7.3 参考api地址
https://console.cloud.tencent.com/api/explorer?Product=tmt&Version=2018-03-21&Action=TextTranslate&SignVersion=