Google Ajax translation language API example sample reference

Source: Internet
Author: User
Google translation API class reference



 

 

 <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "/> <br/> <title> Google language API sample </title> <br/> <MCE: script src = "http://www.google.com/jsapi" mce_src = "http://www.google.com/jsapi"> </MCE: SCRIPT> <br/> <MCE: Script Type = "text/JavaScript"> <! -- <Br/> Google. load ("language", "1"); <br/> function initialize () <br/>{</P> <p >}< br/> Google. setonloadcallback (initialize); </P> <p> // ---------------------- language translation --------------------------- <br/> // translate Chinese into English <br/> function transtoen (cntxt) <br/>{< br/> var text = cntxt; <br/> // translate from Chinese to English, and have the callback of the Request <br/> // if you change "ZH-CN" to an empty string ", the language is automatically detected and translated into English. <br/> Google. language. translate (text, 'zh-cn', 'en', transcallback ); <br/>}< br/> // translate English into Chinese <br/> function transtoch (entxt) <br/>{< br/> var text = entxt; <br/> Google. language. translate (text, 'en', 'zh-cn', transcallback ); <br/>}< br/> // translated callback function <br/> function transcallback (result) <br/>{< br/> If (result. translation) <br/>{< br/> alert (result. translation); <br/>}< br/> else alert ("translation error"); <B R/>}< br/> // -------------------- language translation samples </P> <p> // ---------------------- language detection --------------------------- <br/> function detect (detecttxt) <br/>{< br/> var text = detecttxt; <br/> // detect the language of the text. <br/> Google. language. detect (text, detectcallback); <br/>}< br/> // callback function <br/> function detectcallback (result) <br/> {<br/> // If There wasn' t an error in The request <br/> If (! Result. error) <br/>{< br/> var langcode = result. language; <br/> var langname; <br/> // loop through the languages Enum so that we can find the actual name of the language. <br/> // learn about the ages Enum here: <br/> // http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray <br/> for (L in Google. language. ages) <br/>{< br/> If (Google. language. ages [l] = langcode) <br/> {<br/> // if we find the language code <br/> langname = L; <br/> break; <br/>}< br/> alert ("language:" + langname + "," + langcode ); <br/>}< br/> // -------------------- language detection -------------------------------- </P> <p> function chinesebtn_onclick () <br/>{< br/> transtoen (document. getelementbyid ("chinesetxt "). value); <br/>}< br/> function englishbtn_onclick () <br/>{< br/> var TXT = document. getelementbyid ("englishtxt "). value; <br/> transtoch (txt); <br/>}< br/> function detectlanguage_onclick () <br/>{< br/> var TXT = document. getelementbyid ("languagetxt "). value; <br/> detect (txt); <br/>}</P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body style = "font-family: Arial; Border: 0 none;" mce_style = "font-family: Arial; border: 0 none; "> <br/> <% -- <br/> <Div style =" float: Left; width: 800px; "> <br/> <Div style =" float: Left; width: 400px; "> <br/> <p> translate Chinese into English </P> <br/> <textarea rows =" 10 "Cols =" 30 "id =" chinesetxt "> </textarea> <br/> <input id = "chinesebtn" type = "button" value = "OK" onclick = "Return chinesebtn_onclick () "/> <br/> </div> <br/> <Div style =" float: Left; width: 400px; "> <br/> <p> translate English into Chinese </P> <br/> <textarea rows =" 10 "Cols =" 30 "id =" englishtxt "> </textarea> <br/> <input id = "englishbtn" type = "button" value = "OK" onclick = "Return englishbtn_onclick () "/> <br/> </div> <br/> <Div style =" float: Left; width: 400px; "> <br/> <p> language detection </P> <br/> <textarea rows =" 10 "Cols =" 30 "id =" languagetxt "> </textarea> <br/> <input id = "detectlanguage" type = "button" value = "OK" onclick = "Return detectlanguage_onclick () "/> <br/> </div> <br/> -- %> <br/> </body> <br/> </ HTML> <br/> This article is from the smallkonrad csdn blog, reprinted please indicate the source: http://blog.csdn.net/smallkonrad/archive/2009/06/30/4310777.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.