【发布时间】:2019-03-28 21:02:44
【问题描述】:
众所周知,使用谷歌音译时必须按空格键进行翻译。
但要求是转换从数据库获取的输入中已存储的值,而不是实时用户键入的值。
我怀疑我们可以用 google jspi 自动翻译输入值。
如果不按空格键就无法翻译,所以我尝试在单击特定按钮时按空格键到每个类,所以它会被翻译。
例如,这里是按钮,如果单击此按钮,则按空格键到每个类并自动翻译。
我尝试在值后添加空格,但没有空格键事件没有任何变化
$('#translate').click(function(){
$('.npl').each(function(){
$(this).val($(this).val()+'');
})
})
$('.npl').nepalize();
$.fn.nepalize = function(){
var that = this[0];
google.load("elements", "1", {
packages: "transliteration"
});
function onLoad() {
var options = {
sourceLanguage: 'en', // or google.elements.transliteration.LanguageCode.ENGLISH,
destinationLanguage: ['ne'], // or [google.elements.transliteration.LanguageCode.HINDI],
shortcutKey: 'ctrl+g',
transliterationEnabled: true
};
var control = new google.elements.transliteration.TransliterationControl(options);
// Enable transliteration in the textfields with the given Class.
var elements = document.getElementsByClassName('npl');
control.makeTransliteratable(elements);
}
google.setOnLoadCallback(onLoad);
}
$('#translate').click(function(){
$('.npl').each(function(){
$(this).val($(this).val()+'');
})
})
$('.npl').nepalize();
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
</head>
<body>
<button id='translate'>Translate</button>
<input class="npl" value='Hello'>
<input class="npl" value='How are you' />
</body>
</html>
【问题讨论】:
标签: jquery google-translate jsapi google-translator-toolkit