|
|
@ -6,7 +6,13 @@ import cn.hutool.http.HttpRequest; |
|
|
import cn.hutool.http.HttpResponse; |
|
|
import cn.hutool.http.HttpResponse; |
|
|
import cn.hutool.json.JSONObject; |
|
|
import cn.hutool.json.JSONObject; |
|
|
import cn.hutool.json.JSONUtil; |
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
|
|
import com.bx.implatform.entity.ImBaiduConfiguration; |
|
|
|
|
|
import com.bx.implatform.service.IImBaiduConfigurationService; |
|
|
|
|
|
import com.bx.implatform.service.ImAgentService; |
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
@ -15,6 +21,8 @@ import java.util.List; |
|
|
* 百度翻译工具类 |
|
|
* 百度翻译工具类 |
|
|
*/ |
|
|
*/ |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
|
|
|
@Component |
|
|
|
|
|
@RequiredArgsConstructor |
|
|
public class BaiduTranslationUtils { |
|
|
public class BaiduTranslationUtils { |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -32,6 +40,8 @@ public class BaiduTranslationUtils { |
|
|
*/ |
|
|
*/ |
|
|
private static final String SECRET_KEY = "bGdLDbZgM3WMOL09_1qI"; |
|
|
private static final String SECRET_KEY = "bGdLDbZgM3WMOL09_1qI"; |
|
|
|
|
|
|
|
|
|
|
|
private final IImBaiduConfigurationService baiduConfigurationService; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 翻译文本 |
|
|
* 翻译文本 |
|
|
* |
|
|
* |
|
|
@ -40,7 +50,7 @@ public class BaiduTranslationUtils { |
|
|
* @param to 目标语言 |
|
|
* @param to 目标语言 |
|
|
* @return 翻译结果 |
|
|
* @return 翻译结果 |
|
|
*/ |
|
|
*/ |
|
|
public static String translate(String query, String from, String to) { |
|
|
public String translate(String query, String from, String to) { |
|
|
try { |
|
|
try { |
|
|
// 生成随机数
|
|
|
// 生成随机数
|
|
|
String salt = RandomUtil.randomNumbers(10); |
|
|
String salt = RandomUtil.randomNumbers(10); |
|
|
@ -91,7 +101,75 @@ public class BaiduTranslationUtils { |
|
|
* @param to 目标语言 |
|
|
* @param to 目标语言 |
|
|
* @return 翻译结果 |
|
|
* @return 翻译结果 |
|
|
*/ |
|
|
*/ |
|
|
public static String translate(String query, String to) { |
|
|
public String translate(String query, String to) { |
|
|
return translate(query, "auto", to); |
|
|
return translate(query, "auto", to); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 翻译文本 |
|
|
|
|
|
* |
|
|
|
|
|
* @param query 待翻译文本 |
|
|
|
|
|
* @param from 源语言,可设置为"auto"自动检测 |
|
|
|
|
|
* @param to 目标语言 |
|
|
|
|
|
* @return 翻译结果 |
|
|
|
|
|
*/ |
|
|
|
|
|
public String translateByAgentConfiguration(String query, String from, String to) { |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
ImBaiduConfiguration configuration = baiduConfigurationService.getByToken(); |
|
|
|
|
|
|
|
|
|
|
|
// 生成随机数
|
|
|
|
|
|
String salt = RandomUtil.randomNumbers(10); |
|
|
|
|
|
|
|
|
|
|
|
// 生成签名:appid+q+salt+密钥的MD5值
|
|
|
|
|
|
String signStr = configuration.getAppId() + query + salt + configuration.getSecretKey(); |
|
|
|
|
|
String sign = DigestUtil.md5Hex(signStr); |
|
|
|
|
|
|
|
|
|
|
|
String url = "https://fanyi-api.baidu.com/api/trans/vip/translate"; |
|
|
|
|
|
// 发送请求
|
|
|
|
|
|
HttpResponse response = HttpRequest.post(url) |
|
|
|
|
|
.form("q", query) |
|
|
|
|
|
.form("from", from) |
|
|
|
|
|
.form("to", to) |
|
|
|
|
|
.form("appid", configuration.getAppId()) |
|
|
|
|
|
.form("salt", salt) |
|
|
|
|
|
.form("sign", sign) |
|
|
|
|
|
.execute(); |
|
|
|
|
|
|
|
|
|
|
|
// 解析响应
|
|
|
|
|
|
String body = response.body(); |
|
|
|
|
|
JSONObject result = JSONUtil.parseObj(body); |
|
|
|
|
|
|
|
|
|
|
|
// 检查是否有错误
|
|
|
|
|
|
if (result.containsKey("error_code")) { |
|
|
|
|
|
log.error("百度翻译失败,错误码:{},错误信息:{}", result.getStr("error_code"), result.getStr("error_msg")); |
|
|
|
|
|
return result.getStr("error_msg") == null ? "system error" : "system error:" + result.getStr("error_msg"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取翻译结果
|
|
|
|
|
|
List<JSONObject> transResults = result.getBeanList("trans_result", JSONObject.class); |
|
|
|
|
|
if (transResults == null || transResults.isEmpty()) { |
|
|
|
|
|
log.error("百度翻译结果为空"); |
|
|
|
|
|
return "result empty"; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 返回第一条翻译结果
|
|
|
|
|
|
return transResults.get(0).getStr("dst"); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("百度翻译异常", e); |
|
|
|
|
|
return "translation unknown error"; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 翻译文本(自动检测源语言) |
|
|
|
|
|
* |
|
|
|
|
|
* @param query 待翻译文本 |
|
|
|
|
|
* @param to 目标语言 |
|
|
|
|
|
* @return 翻译结果 |
|
|
|
|
|
*/ |
|
|
|
|
|
public String translateByAgentConfiguration(String query, String to) { |
|
|
|
|
|
return translateByAgentConfiguration(query, "auto", to); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|