Google API - 将字符串翻译成 EN 语言 :(
我正在尝试制作一个简单的基于网络的应用程序,它将非英语(非 EN)字符串翻译为英语(EN)语言。为此,我使用 Google 的翻译 API (v1-JS)。
首先,我检测 div 标签中提供的语言(请注意,页面编码设置为 UTF-8)。
然后我尝试翻译非 EN 语言的文本,然后将其显示在检测到的语言标签下方。
我能够获取检测到的语言,但翻译从未发生! :(
任何帮助将不胜感激..
这是代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/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.com/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.com/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>
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.org/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.com/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.com/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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
3点注意事项。
您实际上并未在检测回调中调用翻译。
翻译结果不包含属性 transtext 。您需要的是名为 Translation 的工具。
我怀疑您想在公共域中发布您的 API 密钥
在下面查找修改后的代码
3 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