【问题标题】:Translation by Google Cloud Translate API returns html escaped textGoogle Cloud Translate API 翻译返回 html 转义文本
【发布时间】:2020-03-10 17:53:36
【问题描述】:

在我的应用程序中,我尝试使用 Google Cloud Translation API 将一些西班牙语文本翻译成英语。我注意到我在翻译后收到的文本是 HTML 转义的,这意味着 "'" 显示为 "&#39<nospace>;"">" 显示为 ">" 等等。

我还通过 curl 脚本使用其余 API 检查了翻译,它给了我相同的转义结果 -

curl --request GET 'https://translation.googleapis.com/language/translate/v2?key=$GOOGLE_API_KEY&q=Es%20un%20brillante%20d%C3%ADa%20soleado&target=en'

对这个 curl 的响应是 -

    {
  "data": {
    "translations": [
      {
        "translatedText": "It's a bright sunny day",
        "detectedSourceLanguage": "es"
      }
    ]
  }
}

当我尝试在在线谷歌翻译器(即https://translate.google.com)中将相同的西班牙语文本翻译成英文时,我得到的英文文本是“这是一个阳光明媚的日子”。

我的第一个问题:是因为某种原因而这样做还是这是一个错误?

为了取消转义文本,我使用org.apache.commons.text.StringEscapeUtils.unescapeHtml4() -

StringEscapeUtils.unescapeHtml4(translation.getTranslatedText());

我的第二个问题:这是取消转义翻译文本的正确方法吗?

【问题讨论】:

    标签: google-cloud-platform google-translation-api google-cloud-translate


    【解决方案1】:

    这个问题类似于Google Translate API outputs HTML entities

    由于未明确提供翻译格式,Google Translation API 采用默认格式,即 HTML。因此,它将 html 编码字符串作为翻译文本返回。如果格式明确提供为“文本”,则不会发生 html 编码。

    现在请求翻译的 curl 应该是这样的 -

    curl --request GET 'https://translation.googleapis.com/language/translate/v2?key=$GOOGLE_API_KEY&q=Es%20un%20brillante%20d%C3%ADa%20soleado&target=en&format=text'
    

    响应是 -

        {
      "data": {
        "translations": [
          {
            "translatedText": "It's a bright sunny day",
            "detectedSourceLanguage": "es"
          }
        ]
      }
    }
    

    因此,这里不需要对 html 进行反转义,因为可以避免编码。

    【讨论】:

      【解决方案2】:

      默认情况下 HTML 将被转义,但 format 参数会将 '" 更改为 '"

      对于java,您可以访问format field

      public static Translate.TranslateOption format(String format)
      

      设置源文本的格式,可以是 HTML(默认)或 纯文本。 html 的值表示 HTML 和 text 的值 表示纯文本。

      例如这里的引号:

      $ curl -s -X POST -H "Content-Type: application/json"     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token)     --data "{
        'q': 'The three \'pyramids\' in the Giza pyramid complex.',
        'source': 'en',
        'target': 'es' 
      }" "https://translation.googleapis.com/language/translate/v2"
      {
        "data": {
          "translations": [
            {
              "translatedText": "Las tres 'pirámides' en el complejo piramidal de Giza."
            }
          ]
        }
      }
      

      将留在format = text

      curl -s -X POST -H "Content-Type: application/json"     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token)     --data "{
        'q': 'The three \'pyramids\' in the Giza pyramid complex.',
        'source': 'en',
        'target': 'es',
        'format': 'text'
      }" "https://translation.googleapis.com/language/translate/v2"
      {
        "data": {
          "translations": [
            {
              "translatedText": "Las tres 'pirámides' en el complejo piramidal de Giza."
            }
          ]
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-25
        • 2020-02-11
        • 1970-01-01
        • 2013-08-07
        相关资源
        最近更新 更多