2016-02-01 172 views
0

我想在eclipse中使用谷歌翻譯API,並且已經檢查過Google API網站,但是沒有Java中的Google翻譯API示例代碼。有人可以提供一些並解釋如何開始。我已經閱讀了關於這個API的文檔。谷歌翻譯API示例代碼

回答

0

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>