Using the AJAXLanguage API, you can translate and detect the language of a certain area on a web page using only JavaScript.
The API is newly developed, so there may be some bugs and minor deficiencies compared to a perfect documentation. We will patch these vulnerabilities, so please understand that you can join the AJAX APIs Developer Forum to give us feedback and discuss this API.
Audience This document has been prepared for people who have some familiarity with JavaScript programming and object-oriented programming concepts. There are many JavaScript tutorials on the Internet.
Introduction to the "Hello, World" program on the Google Ajax Language API The easiest way to start learning this API is to look at a simple example that will detect a given language and translate it into English.
<html>
<head>
<script type="text/javascript" src=" http://www.google.com/jsapi"></script >
<script type="text/javascript">
google.load("language", "1");
function initialize() {
var text = document.getElementById("text").innerHTML;
google.language.detect(text, function(result) {
if (!result.error && result.language) {
google.language.translate(text, result.language, "en",
function(result) {
var translated = document.getElementById("translation");
if (result.translation) {
translated.innerHTML = result.translation;
}
});
}
});
}
google.setOnLoadCallback(initialize);
</script>
</head>
<body>
<div id="text">Hello, nice to meet you. </div>
<div id="translation"></div>
</body>
</html>
You can view the example here and modify and run it as you like.
Include Ajax LanguageAPI in your web pages
In order to add the AJAXLanguage API to your web page, you need to utilize the Google AJAX API Loader. This public loading class allows you to load all the AJAX APIs you need, including the language API here. You need to also include the Google AJAX APIs script tag and call google.load("language","1");.
<script type="text/javascript" src=" http://www.google.com/jsapi"></script >
<script type="text/javascript">
google.load("language", "1");
</script>
The first script tag loads the google.load function, which allows you to load a specific Google API. google.load("language","1") loads the first version of the Language API. Currently, the AJAXLanguage API is at version 1, but a new version will be available soon. See the version discussion below for more information.
API updates
The second parameter of the google.load function is actually the version of the AJAXLanguage API you are using. Currently, the AJAX Language API is at version 1, but a new version will be available soon.
If we make important updates to the API in the future, we will change the version number and post a notice in the Google Code and AJAX APIS discussion. When this event occurs, we expect to continue supporting all versions for at least a month to give you enough time to migrate your code.
The AJAXLanguage API team provides regular updates on recent bug fixes and platform optimizations. These bug fixes should only improve performance and fix bugs, but it is possible that we may accidentally break some API users, please use the AJAX APIs discussion group to report such issues.
Example Language Translation This case shows a simple process of translating a JavaScript string variable.
google.language.translate("Hello world", "en", "es", function(result) {
if (!result.error) {
var container = document.getElementById("translation");
container.innerHTML = result.translation;
}
});
View case (translate.html)
Language Detection This case shows language detection for a JavaScript string. The language code will be returned.
var text = "¿Dónde está el baño?"$$
google.language.detect(text, function(result) {
if (!result.error) {
var language = 'unknown';
for (l ingoogle.language.Languages) {
if (google.language.Languages[l] == result.language) {
language = l;
break;
}
}
var container = document.getElementById("detection");
container.innerHTML = text + " is: " + language + ""$$
}
});
View the case (detection.html)
Source Detection during Translation The following case is similar to the basic translation case, but it shows how to translate text when the source language is not known. By passing in an empty string to represent an unknown source language, the system will automatically detect and translate it in a single call.
google.language.translate("Hello world", "", "es", function(result) {
if (!result.error) {
var container = document.getElementById("translation");
container.innerHTML = result.translation;
}
});
See the examples (autotranslate.html)
for more examples. Here are two additional examples of interactions. The first case performs language detection on a pre-entered text string, while also allowing other text to be entered. It also shows confidence and reliability factors (Translator's Note).
View case(detect.html)
Second additional case for translation. It also allows for interactions similar to those described above.
View the case (translate.html)
API details Supported languages
The GoogleAJAXLanguage API now supports the following languages. The technology is constantly improving and our team is working hard to expand this list, so please check back often. You can also visit Google Translate to view the recently updated list.
Arabic
Chinese (Simplified and Traditional)
Dutch
English
French
German
Greek
Italian
Japanese
Korean
Portuguese
Russian
Spanish
Supported language translation pairs
The GoogleAJAXLanguage API now supports the following language translation pairs. The technology is constantly improving and our team is working hard to expand this list, so please check back often. You can also visit Google Translate to view the recently updated list.
Arabic to English
Chinese to English
Chinese (Simplified to Traditional)
Chinese (Traditional to Simplified)
Dutch to English (Dutch to English)
English to Arabic
English to Chinese (Simplified)
English to Chinese (Traditional)
English to Dutch
English to French
English to German
English to Greek
English to Italian
English to Japanese
English to Korean
English to Portuguese
English to Russian
English to Spanish
French to English (French to English)
French to German
German to English
German to French
Greek to English
Italian to English (Italian to English)
Japanese to English
Korean to English (Korean to English)
Portuguese to English (Portuguese to English)
Russian to English (Russian to English)
Spanish to English
Problem Solving If you encounter a problem in your code:
Check the code. Please keep in mind that JavaScript is a case-sensitive language.
To use the JavaScript debugger, in Firefox you can use the JavaScript console or the FireBug extension. In IE, you can use the Microsoft Script Debugger.
Search the AJAXAPIs discussion group. If you can't find a post that answers your question, post your question in the discussion group and include a link to the page where you asked the question.