### 動機 在進行移動端全球化的時候,我們需要根據語言類型準備格式相同,文本不同的好多個文件,如果一個一個翻譯顯然很浪費時間,如果整篇複製到Google翻譯通常翻譯出來的文本是沒辦法直接用的,所以我通過有道雲API實現了一個翻譯iOS全球化文件的工具類。Android可以重寫文本匹配的部分。 ## ...
動機
在進行移動端全球化的時候,我們需要根據語言類型準備格式相同,文本不同的好多個文件,如果一個一個翻譯顯然很浪費時間,如果整篇複製到Google翻譯通常翻譯出來的文本是沒辦法直接用的,所以我通過有道雲API實現了一個翻譯iOS全球化文件的工具類。Android可以重寫文本匹配的部分。
使用方法
直接修改兩個文件的路徑就可以了。需要註意有道雲的API據說一分鐘限制請求1000次,超了的話等明天應該就可以用了。
package test;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
/**
* 全球化快速翻譯工具
*/
public class test {
public static void main(String[] args) {
String inputFile = "/Users/topjoy/git/GoogleDemo/ZeusServer/src/main/java/test/FCLocalizable.strings";
String outputFile = "/Users/topjoy/git/GoogleDemo/ZeusServer/src/main/java/test/FCLocalizable2.strings";
try {
// 打開輸入文件
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
// 創建輸出文件
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
// 逐行讀取輸入文件內容
String line;
while ((line = reader.readLine()) != null) {
// TODO 根據IOS或Android,重寫這裡的匹配代碼,這裡為iOS示例
String[] parts = line.split("=");
if(!line.contains("=")){
// 寫入新的行到輸出文件
writer.write(line);
writer.newLine();
continue;
}
String b = parts[1].trim().replace("\"", "").replace(";", "");
// 使用翻譯方法生成新的字元串
String finalLine = line;
String translatedStr = translateString(b);
if (finalLine.contains("%")){
System.out.printf("此字元串存在特殊符號\n");
}else{
System.out.printf(b + " --> " + translatedStr + "\n");
}
// 替換原始字元串中的b
String newLine = finalLine.replace(b, translatedStr.isEmpty() ? b : translatedStr);
// 寫入新的行到輸出文件
writer.write(newLine);
writer.newLine();
}
// 關閉文件
reader.close();
writer.close();
System.out.println("替換完成並生成備份文件。");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文本翻譯方法
* 示例為有道雲翻譯API,使用時根據自己的API進行修改
* @param inputStr 待翻譯文本
* @return 翻譯文本
* @throws IOException IO異常
*/
public static String translateString(String inputStr) throws IOException {
// 根據具體需求實現你的翻譯邏輯
String url = "http://fanyi.youdao.com/translate"+"?&doctype=json&type=EN2ZH-CHT&i="+inputStr;
OkHttpClient client = new OkHttpClient();
// 翻譯服務不需要加密
Request request = null;
Request.Builder builder = new Request.Builder()
.url(url)
// .addHeader("Signature", getSignature(date, target, inputStr))
// .addHeader("Date", date)
// .addHeader("timestamp", date)
.get();
request = builder.build();
Response response = client.newCall(request).execute();
String resp = "";
ResponseBody responseBody = response.body();
if (responseBody != null && response.isSuccessful() && !responseBody.toString().isEmpty())
resp = responseBody.string();
System.out.printf(resp+"\n");
JSONObject jsonObject = new JSONObject(resp);
JSONArray array = jsonObject.optJSONArray("translateResult");
if (array == null){
return "";
}
JSONArray array1 = array.optJSONArray(0);
if (array1 == null){
return "";
}
JSONObject jsonObject1 = array1.optJSONObject(0);
if (jsonObject1 == null){
return "";
}
String tgt = jsonObject1.optString("tgt");
if (!tgt.isEmpty()){
return tgt;
}
return "";
}
}
完成
本文來自博客園,作者:兩小無猜,轉載請註明原文鏈接:https://www.cnblogs.com/charlottepl/p/17526136.html