谷歌翻译
function translate(lang) {
var source = document.getElementById("article").innerHTML;
var len = source.length;
// Google Language API accepts 500 characters per request
var words = 500;
document.getElementById("translation").style.display='block';
document.getElementById("translation").innerHTML = "";
for(i=0; i<=(len/words); i++) {
google.language.translate (source.substr(i*words, words),
"en", lang, function (result) {
if (!result.error) {
document.getElementById("translation").innerHTML += result.translation;
} }); }
// Hide the text written in the original language
document.getElementById("article").style.display = 'none';
return false;
}
一般情况下,翻译一下就可以了。尝试单击几次后,翻译的页面顺序不正确。例如,页面:
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<ul>
<li>list 1</li>
<li>list 2</li>
<li>list 3</li>
</ul>
输出:
<p>Paragraph 3</p>
<ul>
<li>list 1</li>
<li>list 2</li>
<li>list 3</li>
</ul>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
知道为什么吗?
function translate(lang) {
var source = document.getElementById("article").innerHTML;
var len = source.length;
// Google Language API accepts 500 characters per request
var words = 500;
document.getElementById("translation").style.display='block';
document.getElementById("translation").innerHTML = "";
for(i=0; i<=(len/words); i++) {
google.language.translate (source.substr(i*words, words),
"en", lang, function (result) {
if (!result.error) {
document.getElementById("translation").innerHTML += result.translation;
} }); }
// Hide the text written in the original language
document.getElementById("article").style.display = 'none';
return false;
}
Normally, the translation alright. After attempting couple of click, the translated page is not in correct order. For example, the page:
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<ul>
<li>list 1</li>
<li>list 2</li>
<li>list 3</li>
</ul>
output:
<p>Paragraph 3</p>
<ul>
<li>list 1</li>
<li>list 2</li>
<li>list 3</li>
</ul>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法保证翻译响应返回给您的顺序。
发出任何其他类型的 AJAX 请求也会遇到同样的问题。
You can't guarantee the order that the translation responses are going to come back to you.
You would have the same problem making any other kind of AJAX request as well.