我想在一个文本框中键入英语,并希望将阿拉伯语中的文本转换为另一个文本

时间:2018-03-07 07:33:17

标签: javascript google-translate transliteration

我的要求很明确......我想在一个文本框中键入英文,并希望将阿拉伯语中的文本转换为另一个文本,如果我使用退格编辑第一个文本框,则删除等相同的效果应该在文本框2中进行翻译.........我使用了以下代码

    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<script type="text/javascript">
    google.load("elements", "1", {
        packages: "transliteration"
    });
</script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
    function OnLoad() {
        var options = {
            sourceLanguage:
            google.elements.transliteration.LanguageCode.ENGLISH,
            destinationLanguage:
            [google.elements.transliteration.LanguageCode.ARABIC],
            shortcutKey: 'ctrl+g',
            transliterationEnabled: true
        };

        var control = new google.elements.transliteration.TransliterationControl(options);
        control.makeTransliteratable(["txtHindi"]);
        var keyVal = 32; // Space key
        $("#txtEnglish").on('keydown', function (event) {
            if (event.keyCode === 32) {
                var engText = $("#txtEnglish").val() + " ";
                var engTextArray = engText.split(" ");
                $("#txtHindi").val($("#txtHindi").val() + engTextArray[engTextArray.length - 2]);

                document.getElementById("txtHindi").focus();
                $("#txtHindi").trigger({
                    type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
                });
            }
        });

        $("#txtHindi").bind("keyup", function (event) {
            setTimeout(function () { $("#txtEnglish").val($("#txtEnglish").val() + " "); document.getElementById("txtEnglish").focus() }, 0);
        });
    } //end onLoad function

    google.setOnLoadCallback(OnLoad);


</script> 

    <script type="text/javascript">
        function clearText() {

            document.getElementById("txtEnglish").value = ""
            document.getElementById("txtHindi").value = ""
        }

    </script>

</head>
    <body>
       English Text: <input size="40" type="text" id="txtEnglish"/> <br/>
       Arabic Text`enter code here` : <input size="40" type="text" id="txtHindi"/> 
        <input type="button" id="CmdShowKeyboard" value="Clear" onclick="clearText()">
</body>
</html>

它的工作正常,但是当我使用Backspaces或删除从文本框中删除文本时,它不可行并在第二个文本框中输出错误建议

1 个答案:

答案 0 :(得分:1)

我认为这应该有用

$("#txtEnglish").on('keydown', function (event) {
    if (event.keyCode === 32 || event.keyCode === 8) {
        var engText = $("#txtEnglish").val() + " ";
        var engTextArray = engText.split(" ");
        if(event.keyCode === 32)
        {
           $("#txtHindi").val($("#txtHindi").val() + engTextArray[engTextArray.length - 2]);
        }

        document.getElementById("txtHindi").focus();
        $("#txtHindi").trigger({
                    type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
                });
            }
        });

        $("#txtHindi").bind("keyup", function (event) {
            setTimeout(function(){
                if (event.keyCode === 32) {
                    $("#txtEnglish").val($("#txtEnglish").val()+ ' ');
                }
                else if(event.keyCode === 8)
                {
                    $("#txtEnglish").val($("#txtEnglish").val().substring(0,$("#txtEnglish").val().length - 1));
                }
                document.getElementById("txtEnglish").focus()
            },0);
        });