从Google Translate API获取中文罗马化

时间:2010-05-01 13:29:28

标签: ajax xmlhttprequest google-translate

Google语言翻译API可以干净利落地翻译成中文:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script>
  google.load('language','1');
  function googletrans(text) {
    google.language.translate(text,'en','zh',function(result) {
      alert(result.translation);
    });
  }
</script>
<input onchange="googletrans(this.value);">
Example input: "Hello"
Result: "你好"

我的问题是我无法获得罗马化(使用英文字母发音)。这是known issue

现在,数据就在translate.google.com上(示例输入:“Hello”结果:“Nǐhǎo”),我甚至可以通过将浏览器指向:

来查看
http://translate.google.com/translate_a/t?client=t&text=hello&hl=en&sl=en&tl=zh-CN&otf=2&pc=0
Result:
{"sentences":[{"trans":"你好","orig":"hello","translit":"Nǐ hǎo"}],
 "dict":[{"pos":"interjection","terms":["喂"]}],"src":"en"}

但不知何故,当我尝试使用ajax获取此URL时,它失败了(XMLHttpRequest Exception 101)。有没有办法用ajax检索这个罗马化数据?