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 DetectionThis 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 + "";
}
});