【发布时间】:2020-06-03 03:18:32
【问题描述】:
我在 Java 中使用 Google Translate API,每当我发送带有双引号的英文文本进行翻译时,我都会得到这种奇怪的格式:
原文(英文):
"Fresh Air"
谷歌的回应(pt):
"Ar Fresco"
Google 的回应:
"Aire Fresco"
想要的结果:
"Ar Fresco"(葡萄牙语)和"Aire Fresco"(西班牙语)
用于检索翻译的 Java 代码:
String google_key = "SOME_GOOGLE_API_KEY";
List<String> result = new ArrayList<String>();
try {
Translate t = new Translate.Builder(
com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(),
com.google.api.client.json.gson.GsonFactory.getDefaultInstance(), null)
.setApplicationName("myApp").build();
List<String> totranslate = new ArrayList<String>();
totranslate.add(sourceText);
Translate.Translations.List list = t.new Translations().list(
totranslate,
targetLanguage.getCode());
list.setKey(google_key);
TranslationsListResponse response = list.execute();
for (TranslationsResource tr : response.getTranslations()) {
result.add(tr.getTranslatedText());
}
System.out.println("Google translation: ["+sourceText+"]("+targetLanguage+"): "+result.get(0));
return result.get(0);
} catch (Exception e) {
e.printStackTrace();
return null;
}
我错过了什么?有没有办法避免这种结果并像原始源文本一样获得双引号?
【问题讨论】:
标签: java google-translate google-translation-api