feat(translator): 添加百度翻译配置和工具类
- 创建 BaiduTranslatorConfig 配置类用于管理百度翻译API配置 - 实现 BaiduTranslatorUtils 工具类提供翻译功能 - 添加翻译请求参数构建和MD5签名生成方法 - 实现根据货币代码自动推断目标语言的功能 - 集成HTTP请求和JSON响应解析功能 - 添加完整的错误处理和日志记录机制
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
package com.mtkj.mtpay.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 百度翻译配置
|
||||||
|
* 注意:不使用 @Component,而是通过 @EnableConfigurationProperties 在启动类中启用
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "baidu.translator")
|
||||||
|
public class BaiduTranslatorConfig {
|
||||||
|
/**
|
||||||
|
* API host
|
||||||
|
*/
|
||||||
|
private String transApiHost;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* App ID (对应配置中的 app-id)
|
||||||
|
* 注意:使用 appid(全小写)以匹配配置文件中的 app-id
|
||||||
|
*/
|
||||||
|
private String appid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Security Key
|
||||||
|
*/
|
||||||
|
private String securityKey;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package com.mtkj.mtpay.util;
|
||||||
|
|
||||||
|
import com.mtkj.mtpay.config.BaiduTranslatorConfig;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 百度翻译工具类
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class BaiduTranslatorUtils {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BaiduTranslatorConfig baiduTranslatorConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 翻译文本
|
||||||
|
* @param query 待翻译文本
|
||||||
|
* @param to 目标语言代码(如:en-英语, th-泰语, vie-越南语, may-马来语, id-印尼语, fil-菲律宾语)
|
||||||
|
* @return 翻译结果,如果翻译失败则返回原文
|
||||||
|
*/
|
||||||
|
public String getTransResult(String query, String to) {
|
||||||
|
if (query == null || query.trim().isEmpty()) {
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查配置是否已加载
|
||||||
|
if (baiduTranslatorConfig == null) {
|
||||||
|
log.error("百度翻译配置未加载,无法翻译,原文: {}", query);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
String transApiHost = baiduTranslatorConfig.getTransApiHost();
|
||||||
|
if (transApiHost == null || transApiHost.trim().isEmpty()) {
|
||||||
|
log.error("百度翻译API地址未配置,无法翻译,原文: {}", query);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
String appid = baiduTranslatorConfig.getAppid();
|
||||||
|
String securityKey = baiduTranslatorConfig.getSecurityKey();
|
||||||
|
if (appid == null || appid.trim().isEmpty() || securityKey == null || securityKey.trim().isEmpty()) {
|
||||||
|
log.error("百度翻译AppID或SecurityKey未配置,无法翻译,原文: {}", query);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Map<String, String> params = this.buildParams(query, to);
|
||||||
|
String response = HttpGet.get(transApiHost, params);
|
||||||
|
|
||||||
|
if (response == null || response.trim().isEmpty()) {
|
||||||
|
log.warn("翻译API返回空响应,原文: {}", query);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析JSON响应
|
||||||
|
try {
|
||||||
|
com.fasterxml.jackson.databind.JsonNode rootNode = new com.fasterxml.jackson.databind.ObjectMapper().readTree(response);
|
||||||
|
|
||||||
|
// 检查是否有错误
|
||||||
|
com.fasterxml.jackson.databind.JsonNode errorCodeNode = rootNode.path("error_code");
|
||||||
|
if (!errorCodeNode.isMissingNode()) {
|
||||||
|
String errorCode = errorCodeNode.asText();
|
||||||
|
String errorMsg = rootNode.path("error_msg").asText();
|
||||||
|
log.error("翻译API返回错误,错误码: {}, 错误信息: {}, 原文: {}", errorCode, errorMsg, query);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
com.fasterxml.jackson.databind.JsonNode transResultNode = rootNode.path("trans_result");
|
||||||
|
if (transResultNode.isArray() && !transResultNode.isEmpty()) {
|
||||||
|
com.fasterxml.jackson.databind.JsonNode firstResult = transResultNode.get(0);
|
||||||
|
String translated = firstResult.path("dst").asText();
|
||||||
|
log.debug("翻译成功,原文: {}, 译文: {}, 目标语言: {}", query, translated, to);
|
||||||
|
return translated;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("解析翻译API响应失败,响应内容: {}, 原文: {}", response, query, e);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("翻译失败,原文: {}, 目标语言: {}", query, to, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建翻译请求参数
|
||||||
|
*/
|
||||||
|
private Map<String, String> buildParams(String query, String to) {
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put("q", query);
|
||||||
|
params.put("from", "auto"); // 自动检测源语言
|
||||||
|
params.put("to", to);
|
||||||
|
params.put("appid", baiduTranslatorConfig.getAppid());
|
||||||
|
String salt = String.valueOf(System.currentTimeMillis());
|
||||||
|
params.put("salt", salt);
|
||||||
|
String src = baiduTranslatorConfig.getAppid() + query + salt + baiduTranslatorConfig.getSecurityKey();
|
||||||
|
params.put("sign", MD5.md5(src));
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据货币代码推断目标语言
|
||||||
|
* @param currency 货币代码(如:USD, THB, VND, MYR, IDR, PHP, SGD)
|
||||||
|
* @return 目标语言代码(百度翻译API支持的语言代码)
|
||||||
|
*/
|
||||||
|
public String getLanguageByCurrency(String currency) {
|
||||||
|
if (currency == null) {
|
||||||
|
return "en"; // 默认英语
|
||||||
|
}
|
||||||
|
|
||||||
|
// 货币代码到语言代码的映射(使用百度翻译API支持的语言代码)
|
||||||
|
Map<String, String> currencyToLanguage = new HashMap<>();
|
||||||
|
currencyToLanguage.put("USD", "en"); // 美元 -> 英语
|
||||||
|
currencyToLanguage.put("THB", "th"); // 泰铢 -> 泰语
|
||||||
|
currencyToLanguage.put("VND", "vie"); // 越南盾 -> 越南语
|
||||||
|
currencyToLanguage.put("MYR", "may"); // 马来西亚林吉特 -> 马来语
|
||||||
|
currencyToLanguage.put("IDR", "id"); // 印尼盾 -> 印尼语
|
||||||
|
currencyToLanguage.put("PHP", "fil"); // 菲律宾比索 -> 菲律宾语
|
||||||
|
currencyToLanguage.put("SGD", "en"); // 新加坡元 -> 英语
|
||||||
|
currencyToLanguage.put("CNY", "zh"); // 人民币 -> 中文
|
||||||
|
|
||||||
|
return currencyToLanguage.getOrDefault(currency.toUpperCase(), "en");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user