java调用腾讯云接口-第七篇(翻译)

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=
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值