Google translation Interface

Source: Internet
Author: User

Google Translate Ajax API is a member of the Google Ajax API series.

The loading procedure is as follows:

First, introduce the Google Ajax api library

<SCRIPT src = "http://www.google.com/jsapi? Key = ABQIAAAAS14iyZ8v9R4CXuPaiVlSoRRi_j0U6kJrkFvY4-OX2XYmEAa76BTi8E3KzP2xXxQsqKYZGJs6BWUacQ "type =" text/JavaScript "> </SCRIPT>

The key is valid for the specified domain name and can be applied for free.

 

Then, before referencing the relevant API interfaces of translate, you must introduce the loaded translate module. You can call the following JavaScript code in the

Google. Load ("language", "1 ");

"Language" is the module name and "1" is the version

Then you can use the Google Translate interface.

 

First, we will introduce the most important interface: Google. Language. Translate. Its function prototype is:

Google. Language. Translate (Text | content, srclang, destlang, callback)

The first parameter is the content to be translated.

The second parameter is the language type of the content. This parameter can be null, so that Google will automatically detect

The third parameter is the target language type, that is, the language you want to translate

The fourth parameter is the translation result callback function. Its prototype is generally as follows:

Function (result)

{

}

 

Here, result. error is the translated error code, indicating whether there are errors.

Result. Translation is the translated content.

 

Next we will introduce another interface

Google. Language. Detect (text, callback)

This interface is used to detect the language in which the related content belongs.

The first parameter text indicates the content of the language type to be detected.

The second parameter is a callback function used to return the language type. Its prototype is:

Function (result)

{

}

Result. Error indicates the returned error code.

Result. language indicates the language type returned.

 

The following two examples are from the official Google website.

Language Translation

This example shows a simple translation of a javascript string:

google.language.translate("Hello world", "en", "es", function(result) {
  if (!result.error) {
    var container = document.getElementById("translation");
    container.innerHTML = result.translation;
  }
});

Language Detection

This example shows language detection of a JavaScript string. The language code is returned:

var text = "¿Dónde está el baño?";
google.language.detect(text, function(result) {
  if (!result.error) {
    var language = 'unknown';
    for (l in google.language.Languages) {
      if (google.language.Languages[l] == result.language) {
        language = l;
        break;
      }
    }
    var container = document.getElementById("detection");
    container.innerHTML = text + " is: " + language + "";
  }
});

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.