少年幃禿的煩惱


心情也微微的...凸

最近的學習心得 都改放到 少年幃禿的煩惱@Google Sites

2008/07/02

Google AJAX Language API (2) - Introduction

這個範例會示範如何偵測一段文字, 並且把他翻譯成英文.

&lt;html> &lt;head> <script type="text/javascript" src="//www.google.com/jsapi"></script> <script type="text/javascript"> google.load("language", "1"); function initialize() { document.getElementById('hello_button').addEventListener('click', function() { var text = document.getElementById("hello_text").value; google.language.detect(text, function(result) { if (!result.error && result.language) { document.getElementById('hello_language').innerHTML = result.language; google.language.translate(text, result.language, "en", function(result) { var translated = document.getElementById("hello_translation"); if (result.translation) { translated.innerHTML = result.translation; } }); } }); }, false); } google.setOnLoadCallback(initialize); </script> &lt;/head> &lt;body> <input id='hello_text' type='text' value='你好, 很高興見到你.'/> <input id='hello_button' type='button' value='Translate'/> <div id="hello_language"></div> <div id="hello_translation"></div> &lt;/body> &lt;/html>