The following describes how to use Google Translate APIs:
:
Source code: the comments are included in the source code. Copy the code into HTML to run it.
<HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> <br/> <! -- Add Google's Ajax language API on the page, you can enter this address to download the js api file --> <br/> <SCRIPT type = "text/JavaScript" src = "http://www.google.com/jsapi"> </SCRIPT> <br/> <script type = "text/JavaScript"> <br/> // load the language version. The current version number is 1. <br/> Google. load ("language", "1"); // version 1 </P> <p> function initialize () {<br/> // obtain the content to be translated <br/> var text = document. getelementbyid ("text "). value; </P> <p> // language of the country to be translated. The default value is Chinese. <br/> var tolanguage = country e NT. getelementbyid ("language "). value; </P> <p> // call the arg1 object to be translated for Google Object Detection: the text to be translated, arg2: callback function <br/> Google. language. detect (text, function (result) {<br/> // if no error occurs in the returned value (detect this function is used to detect and filter the function) <br/> If (! Result. error & result. language) {<br/> // call the Google language API translation function <br/> // arg1: the content to be translated. arg2: the language format of the object after filtering, arg3: type to be converted to, arg4: callback function <br/> Google. language. translate (text, result. language, tolanguage, function (result) {<br/> // obtain the textarea object for displaying the translation result <br/> var translated = document. getelementbyid ("Translation"); <br/> // if the object is successfully translated, it is similar to that of the Java JDBC result set object. next () method <br/> If (result. translation) {<br/> // set the html value of the textarea object to be displayed. <br/> translated. innerhtml = result. translation; <br/>}< br/> }); <br/>}</P> <p> // setonloadcallback: Call the initialize function after the binding page is loaded. This function is similar to the $ (docuemnt) of jquery ). ready (function () {}) <br/> Google. setonloadcallback (initialize); </P> <p> // select the language to be translated into another language. <br/> function to_language (STR) {<br/> document. getelementbyid ("language "). value = STR; <br/> If (STR = "ZH") {<br/> document. getelementbyid ("show "). innertext = ""; <br/>} else if (STR = "en") {<br/> document. getelementbyid ("show "). innertext = "Chinese-English Translation "; <br/>}< br/> </SCRIPT> <br/> </pead> </P> <p> <body> <br/> <Div align = "center"> <br/> <div> <br/> <input type = "hidden" id = "language" value = "ZH"/> <br /> <Div id = "show" style = "color: blue; font-size: large; "> English translation </div> <br/> <textarea name =" text "id =" text "Cols = "60" rows = "10"> hello, world </textarea> <br/> </div> <br/> <HR/> <br/> <div> <br/> <textarea name = "text" ID = "Translation" Cols = "60" rows = "10"> </textarea> <br/> </div> <br/> <div> <br/> select the translation type: <br/> <input type = "button" onclick = "to_language ('en')" value = ""/> & nbsp; <br/> <input type = "button" onclick = "to_language ('zh ')" value = ""/> & nbsp; <br/> <input type = "Submit" value = "Translation" onclick = "initialize () "/> <br/> </div> <br/> </body> <br/> </ptml>
Abbreviated National Language Form:
Language |
Language code |
Afrikaans |
af |
Albanian |
sq |
Arabic |
ar |
Belarusian |
be |
Bulgarian |
bg |
Catalan |
ca |
Chinese Simplified |
zh-CN |
Chinese Traditional |
zh-TW |
Croatian |
hr |
Czech |
cs |
Danish |
da |
Dutch |
nl |
English |
en |
Estonian |
et |
Filipino |
tl |
Finnish |
fi |
French |
fr |
Galician |
gl |
German |
de |
Greek |
el |
Haitian Creole |
ht |
Hebrew |
iw |
Hindi |
hi |
Hungarian |
hu |
Icelandic |
is |
Indonesian |
id |
Irish |
ga |
Italian |
it |
Japan |
ja |
Latvian |
lv |
Lithuanian |
lt |
Macedonian |
mk |
Malay |
ms |
Maltese |
mt |
Norwegian |
no |
Persian |
fa |
Polish |
pl |
Portuguese |
pt |
Romanian |
ro |
Russian |
ru |
Serbian |
sr |
Slovak |
sk |
Slovenian |
sl |
Spanish |
es |
Swahili |
sw |
Swedish |
sv |
Thai |
th |
Turkish |
tr |
Ukrainian |
uk |
Vietnamese |
vi |
Welsh |
cy |
Yiddish |
yi |