【问题标题】:Sample Code for Google Translate API谷歌翻译 API 的示例代码
【发布时间】:2016-05-10 10:30:25
【问题描述】:

我想在 Eclipse 中使用 Google Translate API,并且我查看了 Google API 网站,但没有任何 Java 中的 Google Translate API 示例代码。有人可以提供一些并解释如何开始。我已经阅读了有关此 API 的文档。

【问题讨论】:

    标签: api google-api google-translate


    【解决方案1】:

    https://cloud.google.com/translate/v2/quickstart#JSONP

    <html>
      <head>
        <title>Translate API Example</title>
      </head>
      <body>
        <div id="sourceText">Hello world</div>
        <div id="translation"></div>
        <script>
          function translateText(response) {
            document.getElementById("translation").innerHTML += "<br>" + response.data.translations[0].translatedText;
          }
        </script>
        <script>
          var newScript = document.createElement('script');
          newScript.type = 'text/javascript';
          var sourceText = escape(document.getElementById("sourceText").innerHTML);
          // WARNING: Your API key will be visible in the page source.
          // To prevent misuse, restrict your key to designated domains or use a
          // proxy to hide your key.
          var source = 'https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&source=en&target=de&callback=translateText&q=' + sourceText;
          newScript.src = source;
    
          document.getElementsByTagName('head')[0].appendChild(newScript);
        </script>
      </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      Google Client Libraries 现在是推荐使用的 API。您现在可以在 documentation 中找到特定于 Java 的 Translation API Client Library 示例。

      要报告新 Java specific Client Libraries 的任何问题,您可以直接在 GitHub Issue tracker 上提出问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-25
        • 1970-01-01
        • 2011-12-26
        • 2010-10-10
        • 2015-05-19
        相关资源
        最近更新 更多