|
@@ -0,0 +1,77 @@
|
|
|
+package cn.cslg.pas.service.common;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.dto.OPS.GetSearchBiblioParamsDTO;
|
|
|
+import cn.cslg.pas.common.utils.DataUtils;
|
|
|
+import cn.cslg.pas.common.utils.DateUtils;
|
|
|
+import cn.hutool.crypto.SecureUtil;
|
|
|
+import cn.hutool.crypto.digest.HMac;
|
|
|
+import com.aliyun.alimt20181012.models.TranslateGeneralResponse;
|
|
|
+import com.aliyun.tea.TeaException;
|
|
|
+import okhttp3.MediaType;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.RequestBody;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.Base64.Encoder;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class TranslateService {
|
|
|
+
|
|
|
+
|
|
|
+ public static com.aliyun.alimt20181012.Client createClient() throws Exception {
|
|
|
+
|
|
|
+ String key = "LTAI5tGyG1Q7fKprgg1nWhXj";
|
|
|
+ String sercet = "Y6Erboh5lEFiRPR4XK8oCPMvUzYGLN";
|
|
|
+ // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
|
|
+ // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
|
+ // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
|
|
+ .setAccessKeyId(key)
|
|
|
+ // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|
|
+ .setAccessKeySecret(sercet);
|
|
|
+ // Endpoint 请参考 https://api.aliyun.com/product/alimt
|
|
|
+ config.endpoint = "mt.aliyuncs.com";
|
|
|
+ return new com.aliyun.alimt20181012.Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String translateGeneral() throws Exception {
|
|
|
+ com.aliyun.alimt20181012.Client client = TranslateService.createClient();
|
|
|
+ com.aliyun.alimt20181012.models.TranslateGeneralRequest translateGeneralRequest = new com.aliyun.alimt20181012.models.TranslateGeneralRequest()
|
|
|
+ .setFormatType("text")
|
|
|
+ .setSourceLanguage("auto")
|
|
|
+ .setTargetLanguage("zh")
|
|
|
+ .setSourceText("my name is limeimei")
|
|
|
+ .setScene("general");
|
|
|
+ com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
|
|
+ try {
|
|
|
+ // 复制代码运行请自行打印 API 的返回值
|
|
|
+ TranslateGeneralResponse translateGeneralResponse = client.translateGeneralWithOptions(translateGeneralRequest, runtime);
|
|
|
+ String re= translateGeneralResponse.getBody().getData().translated;
|
|
|
+ return re;
|
|
|
+ } catch (TeaException error) {
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
+ // 错误 message
|
|
|
+ System.out.println(error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ System.out.println(error.getData().get("Recommend"));
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
+ } catch (Exception _error) {
|
|
|
+ TeaException error = new TeaException(_error.getMessage(), _error);
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
+ // 错误 message
|
|
|
+ System.out.println(error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ System.out.println(error.getData().get("Recommend"));
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+}
|