Use Google's translation API to add translation functions for websites

Source: Internet
Author: User

Google's translation API can be divided into free translation and literal translation. Google's translation API can easily provide translation functions for websites.

First, you need to look at the developer guide page to load Google's JsCode
<SCRIPT type = "text/JavaScript" src = "http://www.google.com/jsapi"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
Google. Load ("language", "1 ");
</SCRIPT>

Then, Google. Language. Translate can be used to complete the translation. Here is an example document. For API instructions, see here.

You can use the developer guide page to easily obtain data, but the data is one-time. Fortunately, Google also provides other methods to return data, such as JSON.

Google translation API must use the get method to obtain data. The returned data format is JSON and the structure is as follows:
{
"Responsedata ":{
"Translatedtext": The-translated-text,
"Detectedsourcelanguage "? : The-source-language
},
"Responsedetails": NULL | string-on-error,
& Quot; responsestatus & quot;: 200 | error-code
}

The project uses the getjson method of jquery to obtain data and use it multiple times on the page, which is very flexible.

During this period, an error occurs, indicating that the corss domain requests data across domain names. Shown as: access to restricted URI denied "Code:" 1012 jquery

You can look at others' opinions, but I suggest you look at jquery's getjson method or look down.

Because of the security considerations of Web Services, cross-domain request is forbidden, a new josn concept jsonp was proposed to solve this problem, fortunately, Google, Flickr, and other data providers support jsonp.

Here are some documents to help you solve this problem.

Note that the transmitted parameters must be encoded; otherwise, 400 is returned.

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.