Java 百度翻譯 API 中文轉英文接入
業務上遇到了語言國際化的需求,需要將 中文的 json 字符串翻譯成英文,通過百度翻譯 API 接口來實現翻譯功能。
1、平台認證
登錄百度翻譯開放平台,找到通用翻譯模塊,提交申請。
申請鏈接:http://api.fanyi.baidu.com/product/11
申請通過后,就能直接使用了,默認為標准版,完全免費:
2、Java demo 配置
翻譯開放平台非常友好,提供了許多常用語言的 demo 下載,稍微修改下便能使用了。
頁面下載鏈接: http://api.fanyi.baidu.com/doc/21
demo 配置好 appid 及密鑰,運行便能看到控制台中文成功翻譯成了英文:
3、封裝接口
我的目標是將 一長串的中文 json 翻譯成英文 json, 上面的 demo 是滿足不了需求的,可以創建一個 springboot 項目,將 demo 代碼遷移到項目中,封裝一個接口實現業務需求。
項目結構如下:
3.1、自定義接口
先引入 fastJson
依賴:
<!--fastJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
接口參數接收一長串的中文 json ,翻譯完成后返回英文 json:
3.1.1、直接創建線程版本
package com.lin.translate.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lin.translate.config.TransApi;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/com/lin")
public class TranslateController {
// 在平台申請的APP_ID 詳見 http://api.fanyi.baidu.com/api/trans/product/desktop?req=developer
private static final String APP_ID = "";
private static final String SECURITY_KEY = "";
@GetMapping("/translate")
@ResponseBody
public Map<String, Map<String, String>> toTranslate(@RequestBody Map<String, Map<String, String>> map) throws InterruptedException {
TransApi api = new TransApi(APP_ID, SECURITY_KEY);
for(String key : map.keySet()) {
Map<String, String> childMap = map.get(key);
StringBuilder builder = new StringBuilder();
for (String childKey : childMap.keySet()) {
//需要翻譯的中文
builder.append(childMap.get(childKey)).append("\n");
}
//創建線程
Thread thread = new Thread() {
@Override
public void run() {
String result = api.getTransResult(builder.toString(), "auto", "en");
System.out.println(result);
//轉成map
Map<String, String> mapResult = JSON.parseObject(result, Map.class);
List<Map<String, String>> transResult = (List<Map<String, String>>)JSONArray.parse(JSON.toJSONString(mapResult.get("trans_result")));
int i = 0;
for (String childKey : childMap.keySet()) {
//獲取翻譯結果
String transQuery = transResult.get(i).get("dst");
try {
//解碼
transQuery= URLDecoder.decode(transQuery,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
childMap.put(childKey, transQuery);
i++;
}
try {
//睡眠一秒
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
//主線程阻塞,等待子線程結束
thread.join();
}
return map;
}
}
3.1.2、線程池版本
package com.lin.translate.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lin.translate.config.TransApi;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
@Controller
@RequestMapping("/com/lin")
public class ExecutorController {
// 在平台申請的APP_ID 詳見 http://api.fanyi.baidu.com/api/trans/product/desktop?req=developer
private static final String APP_ID = "";
private static final String SECURITY_KEY = "";
@GetMapping("/executorTranslate")
@ResponseBody
public Map<String, Map<String, String>> toTranslate(@RequestBody Map<String, Map<String, String>> map) throws InterruptedException {
TransApi api = new TransApi(APP_ID, SECURITY_KEY);
//創建線程池,核心線程1,最大線程數10,存貨時間1分鍾,任務隊列5,默認的線程工廠,拒絕策略為拒絕並拋出異常
ExecutorService executorService = new ThreadPoolExecutor(1, 10, 1, TimeUnit.MINUTES,
new ArrayBlockingQueue<>(5, true), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
for (String key : map.keySet()) {
Map<String, String> childMap = map.get(key);
StringBuilder builder = new StringBuilder();
for (String childKey : childMap.keySet()) {
//需要翻譯的中文
builder.append(childMap.get(childKey)).append("\n");
}
//執行線程
executorService.execute(() -> {
String result = api.getTransResult(builder.toString(), "auto", "en");
System.out.println("result:" + result);
//轉成map
Map<String, String> mapResult = JSON.parseObject(result, Map.class);
List<Map<String, String>> transResult = (List<Map<String, String>>) JSONArray.parse(JSON.toJSONString(mapResult.get("trans_result")));
int i = 0;
for (String childKey : childMap.keySet()) {
//獲取翻譯結果
String transQuery = transResult.get(i).get("dst");
try {
//解碼
transQuery = URLDecoder.decode(transQuery, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
childMap.put(childKey, transQuery);
i++;
}
});
//線程池等待時間,這里即阻塞2秒
executorService.awaitTermination(2, TimeUnit.SECONDS);
}
//任務執行完成后關閉線程池
executorService.shutdown();
return map;
}
}
3.2、demo 配置類代碼
HttpGet 類代碼如下:
package com.baidu.translate.demo;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
class HttpGet {
protected static final int SOCKET_TIMEOUT = 10000; // 10S
protected static final String GET = "GET";
public static String get(String host, Map<String, String> params) {
try {
// 設置SSLContext
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] { myX509TrustManager }, null);
String sendUrl = getUrlWithQueryString(host, params);
// System.out.println("URL:" + sendUrl);
URL uri = new URL(sendUrl); // 創建URL對象
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setSSLSocketFactory(sslcontext.getSocketFactory());
}
conn.setConnectTimeout(SOCKET_TIMEOUT); // 設置相應超時
conn.setRequestMethod(GET);
int statusCode = conn.getResponseCode();
if (statusCode != HttpURLConnection.HTTP_OK) {
System.out.println("Http錯誤碼:" + statusCode);
}
// 讀取服務器的數據
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line);
}
String text = builder.toString();
close(br); // 關閉數據流
close(is); // 關閉數據流
conn.disconnect(); // 斷開連接
return text;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static String getUrlWithQueryString(String url, Map<String, String> params) {
if (params == null) {
return url;
}
StringBuilder builder = new StringBuilder(url);
if (url.contains("?")) {
builder.append("&");
} else {
builder.append("?");
}
int i = 0;
for (String key : params.keySet()) {
String value = params.get(key);
if (value == null) { // 過濾空的key
continue;
}
if (i != 0) {
builder.append('&');
}
builder.append(key);
builder.append('=');
builder.append(encode(value));
i++;
}
return builder.toString();
}
protected static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 對輸入的字符串進行URL編碼, 即轉換為%20這種形式
*
* @param input 原文
* @return URL編碼. 如果編碼失敗, 則返回原文
*/
public static String encode(String input) {
if (input == null) {
return "";
}
try {
return URLEncoder.encode(input, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return input;
}
private static TrustManager myX509TrustManager = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
};
}
MD5 類代碼如下:
package com.lin.translate.config;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5編碼相關的類
*
* @author wangjingtao
*
*/
public class MD5 {
// 首先初始化一個字符數組,用來存放每個16進制字符
private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f' };
/**
* 獲得一個字符串的MD5值
*
* @param input 輸入的字符串
* @return 輸入字符串的MD5值
*
*/
public static String md5(String input) {
if (input == null) {
return null;
}
try {
// 拿到一個MD5轉換器(如果想要SHA1參數換成”SHA1”)
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
// 輸入的字符串轉換成字節數組
byte[] inputByteArray = input.getBytes("utf-8");
// inputByteArray是輸入字符串轉換得到的字節數組
messageDigest.update(inputByteArray);
// 轉換並返回結果,也是字節數組,包含16個元素
byte[] resultByteArray = messageDigest.digest();
// 字符數組轉換成字符串返回
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/**
* 獲取文件的MD5值
*
* @param file
* @return
*/
public static String md5(File file) {
try {
if (!file.isFile()) {
System.err.println("文件" + file.getAbsolutePath() + "不存在或者不是文件");
return null;
}
FileInputStream in = new FileInputStream(file);
String result = md5(in);
in.close();
return result;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String md5(InputStream in) {
try {
MessageDigest messagedigest = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024];
int read = 0;
while ((read = in.read(buffer)) != -1) {
messagedigest.update(buffer, 0, read);
}
in.close();
String result = byteArrayToHex(messagedigest.digest());
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String byteArrayToHex(byte[] byteArray) {
// new一個字符數組,這個就是用來組成結果字符串的(解釋一下:一個byte是八位二進制,也就是2位十六進制字符(2的8次方等於16的2次方))
char[] resultCharArray = new char[byteArray.length * 2];
// 遍歷字節數組,通過位運算(位運算效率高),轉換成字符放到字符數組中去
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b & 0xf];
}
// 字符數組組合成字符串返回
return new String(resultCharArray);
}
}
TransApi 類代碼如下:
package com.lin.translate.config;
import java.util.HashMap;
import java.util.Map;
public class TransApi {
private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";
private String appid;
private String securityKey;
public TransApi(String appid, String securityKey) {
this.appid = appid;
this.securityKey = securityKey;
}
public String getTransResult(String query, String from, String to) {
Map<String, String> params = buildParams(query, from, to);
return HttpGet.get(TRANS_API_HOST, params);
}
private Map<String, String> buildParams(String query, String from, String to) {
Map<String, String> params = new HashMap<String, String>();
params.put("q", query);
params.put("from", from);
params.put("to", to);
params.put("appid", appid);
// 隨機數
String salt = String.valueOf(System.currentTimeMillis());
params.put("salt", salt);
// 簽名
String src = appid + query + salt + securityKey; // 加密前的原文
params.put("sign", MD5.md5(src));
return params;
}
}
3.3、結果示例
原中文 json:
{
"login": {
"login": "登錄",
"loginLoading": "登錄中...",
"account": "賬號",
"password": "密碼",
"lang": "語言",
"setAddress": "設置服務地址",
"more": "更多"
},
"mDns": {
"local": "局域網",
"localText": "內的XYMERP服務",
"useAddress": "當前設置的地址",
"addressUnAvailable": "無法連接此服務器",
"setAddressTips": "未設置服務地址,立刻設置"
}
}
翻譯后的英文 json:
{
"login": {
"login": "Sign in",
"loginLoading": "Logging in",
"account": "account number",
"password": "password",
"lang": "language",
"setAddress": "Set service address",
"more": "more"
},
"mDns": {
"local": "LAN",
"localText": "Xymerp service in",
"useAddress": "Currently set address",
"addressUnAvailable": "Unable to connect to this server",
"setAddressTips": "Service address not set, set now"
}
}
以上結果滿足我的需求了,不滿足你們需求的就稍微修改下吧。