【问题标题】:Google Translate API double quotes谷歌翻译 API 双引号
【发布时间】: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


    【解决方案1】:

    google api 翻译默认将输入视为 HTML,您可以将格式更改为“文本”或“文本/纯文本” https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/translate/Translate.TranslateOption.html#format-java.lang.String-

    【讨论】:

      猜你喜欢
      • 2013-01-23
      • 1970-01-01
      • 2011-12-26
      • 2010-10-10
      • 2013-02-15
      • 2012-01-26
      • 2012-01-19
      相关资源
      最近更新 更多