I am trying to make a simple web based application which will translate a non-english (non-EN) string into English (EN) language. For this I am using Google's Translation API (v1- JS).
At first I am detecting the language provided in a div tag (note that page encoding is set to UTF-8).
Then I am trying to translate the text if it is in non EN language and then it is to be displayed just below the detected language tag.
I am able to obtain the language detected but the translation never happens! :(
Any help would be much appreciated..
Here's the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script
src="" type="text/javascript"></script>
<script type="text/javascript">
/*
* How to detect the language of text.
*/
google.load("language", "1");
function initialize() {
var content = document.getElementById('content');
// Setting the text in the div.
content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="detected"><\/div><div id="transtext"/>';
// Grabbing the text to translate
var text = document.getElementById("text").innerHTML;
// Detect the language of the text.
google.language.detect(text, function(result) {
var detected = document.getElementById("detected");
// If there wasn't an error in the request
if (!result.error) {
var langCode = result.language;
var langName;
// Loop through the languages enum so that we can find the actual name of the language.
// Learn about the languages enum here:
// .html#LangNameArray
for ( var i in google.language.Languages) {
var thisLangCode = google.language.Languages[i];
if (thisLangCode == langCode) {
// find the language code, store the language name.
langName = i;
break;
}
}
// See the detected language.
detected.innerHTML = 'Detected: "' + result.language
+ '" - aka "' + langName + '"';
}
});
google.language.translate(text, 'es', 'en', function(result) {
var translated = document.getElementById("transtext");
if (result.transtext) {
translated.innerHTML = result.transtext;
}
});
}
google.setOnLoadCallback(initialize);
</script>
</head>
<body style="font-family: Arial; border: 0 none;">
<div id="content">Loading...</div>
</body>
</html>
I am trying to make a simple web based application which will translate a non-english (non-EN) string into English (EN) language. For this I am using Google's Translation API (v1- JS).
At first I am detecting the language provided in a div tag (note that page encoding is set to UTF-8).
Then I am trying to translate the text if it is in non EN language and then it is to be displayed just below the detected language tag.
I am able to obtain the language detected but the translation never happens! :(
Any help would be much appreciated..
Here's the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script
src="http://www.google./jsapi?key=mykeyfrxwexdfwezfdhfxcewx" type="text/javascript"></script>
<script type="text/javascript">
/*
* How to detect the language of text.
*/
google.load("language", "1");
function initialize() {
var content = document.getElementById('content');
// Setting the text in the div.
content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="detected"><\/div><div id="transtext"/>';
// Grabbing the text to translate
var text = document.getElementById("text").innerHTML;
// Detect the language of the text.
google.language.detect(text, function(result) {
var detected = document.getElementById("detected");
// If there wasn't an error in the request
if (!result.error) {
var langCode = result.language;
var langName;
// Loop through the languages enum so that we can find the actual name of the language.
// Learn about the languages enum here:
// http://code.google./apis/ajaxlanguage/documentation/reference.html#LangNameArray
for ( var i in google.language.Languages) {
var thisLangCode = google.language.Languages[i];
if (thisLangCode == langCode) {
// find the language code, store the language name.
langName = i;
break;
}
}
// See the detected language.
detected.innerHTML = 'Detected: "' + result.language
+ '" - aka "' + langName + '"';
}
});
google.language.translate(text, 'es', 'en', function(result) {
var translated = document.getElementById("transtext");
if (result.transtext) {
translated.innerHTML = result.transtext;
}
});
}
google.setOnLoadCallback(initialize);
</script>
</head>
<body style="font-family: Arial; border: 0 none;">
<div id="content">Loading...</div>
</body>
</html>
Share
Improve this question
edited Feb 11, 2014 at 20:30
Constantine
991 silver badge10 bronze badges
asked Aug 12, 2011 at 7:36
echo9echo9
1,3092 gold badges14 silver badges15 bronze badges
2
- What exactly goes wrong? What errors do you get? – Pekka Commented Aug 12, 2011 at 7:43
- I am able to detect the language but I am not able to see the translated text. I am not able to guess what's wrong with this code. No error(s) crop up. :( ? – echo9 Commented Aug 12, 2011 at 7:45
1 Answer
Reset to default 33 points to note .
You are not actually calling translate within the callback of detect. The translate result does not contain a property transtext . The one you need is named translation . I doubt you want to post your API key in a public domain
Find modified code below
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script
src="http://www.google./jsapi?key=xxxxxxxxxxxxxxx>" type="text/javascript"></script>
<script type="text/javascript">
/*
* How to detect the language of text.
*/
google.load("language", "1");
function initialize() {
var content = document.getElementById('content');
// Setting the text in the div.
content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="detected"><\/div><div id="transtext"/>';
// Grabbing the text to translate
var text = document.getElementById("text").innerHTML;
// Detect the language of the text.
google.language.detect(text, function(result) {
var detected = document.getElementById("detected");
// If there wasn't an error in the request
if (!result.error) {
var langCode = result.language;
var langName;
// Loop through the languages enum so that we can find the actual name of the language.
// Learn about the languages enum here:
// http://code.google./apis/ajaxlanguage/documentation/reference.html#LangNameArray
for ( var i in google.language.Languages) {
var thisLangCode = google.language.Languages[i];
if (thisLangCode == langCode) {
// find the language code, store the language name.
langName = i;
break;
}
}
// See the detected language.
detected.innerHTML = 'Detected: "' + result.language
+ '" - aka "' + langName + '"';
google.language.translate(text, result.language, 'en', function(result) {
var translated = document.getElementById("transtext");
if (result.translation) {
translated.innerHTML = result.translation;
}
});
}
});
}
google.setOnLoadCallback(initialize);
</script>
</head>
<body style="font-family: Arial; border: 0 none;">
<div id="content">Loading...</div>
</body>
</html>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204114a4616501.html
评论列表(0条)