【发布时间】:2016-02-09 10:43:40
【问题描述】:
我在我的项目中使用 google 音译。我尝试了Google Transliterate API Developer's Guide 上提供的代码 sn-p。但是,可用的文档较少,我对此知之甚少。
代码的工作方式是仅在您按空格键时才会将单词转换为目标语言。按退格键后它会给出建议。我需要它以一种在您输入字符并同时提供建议时将单词转换为的方式工作。
以下是我从网站上尝试的 sn-p:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Google Transliterate API
google.load("elements", "1", {
packages: "transliteration"
});
function onLoad() {
var options = {
sourceLanguage: google.elements.transliteration.LanguageCode.ENGLISH,
destinationLanguage:[google.elements.transliteration.LanguageCode.HINDI],
shortcutKey: 'ctrl+g',
transliterationEnabled: true
};
// Create an instance on TransliterationControl with the required
// options.
var control = new google.elements.transliteration.TransliterationControl(options);
// Enable transliteration in the textbox with id
// 'transliterateTextarea'.
control.makeTransliteratable(['transliterateTextarea']);
}
google.setOnLoadCallback(onLoad);
</script>
</head>
<body>
Type in Hindi (Press Ctrl+g to toggle between English and Hindi)<br>
<textarea id="transliterateTextarea" style="width:600px;height:200px"></textarea>
</body>
</html>
【问题讨论】:
-
此代码有效。仅当您按空格键时,它才会将单词转换为目标语言。按退格键后它会给出建议。但我在输入字符时需要建议。帮我整理一下。
-
Google Transliterate API 已于 2011 年 5 月 26 日正式弃用。请参考此 URL 获取新 API cloud.google.com/translate/docs/quickstart
标签: javascript google-api search-suggestion transliteration