谷歌翻译小接口

内容:<input type="text" id="source" onchange="submitChange()"><br><br>
语言选择:<select id="langpair">
	<option value="zh-CN|en">汉译英</option>
	<option value="en|zh-CN">英译汉</option>

</select>
<br><br>
翻译结果:<div id="results_body" style="color: red"></div>

<div class=line></div>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
		google.load("language", "1"); 
		google.setOnLoadCallback(submitChange); 
		function submitChange() { 
			var value = document.getElementById('source').value; 
			var langpair = document.getElementById('langpair'); 
			var pair = langpair.options[langpair.selectedIndex].value.split('|'); 
			var src = pair[0]; var dest = pair[1]; 
			google.language.translate(value, src, dest, translateResult); 
			return false; 
		} 
		function translateResult(result) { 
			var resultBody = document.getElementById("results_body"); 
			if (result.translation) { 
				resultBody.innerHTML = result.translation; 
			} else { 
				resultBody.innerHTML = '';
			}
		}
</script>

posted @ 2011-10-09 14:24  y0umer  阅读(1162)  评论(0编辑  收藏  举报