谷歌翻译

时间:2011-05-04 06:27:37

标签: google-translate

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>

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

您无法保证翻译回复将返回给您的顺序。

在制作任何其他类型的AJAX请求时也会遇到同样的问题。