How to detect keyboard input language using JQuery

I have an application that accepts input in two languages ​​(English and Arabic) I am validating an input textbox, the validation logic is not the same for English and Arabic, so I need to know which language the user is typing.

all the solutions i came across determined the default language for the system and the browser, but nothing i found helped me determine the current input language

+3


source to share


3 answers


ok here is the solution that worked for me. Please note that in my application I know for sure that the input language will be one of two languages, either English or Arabic so this is what I did

            var msgText =  entered message text;
            var textLength = msgText.length; // entered message length
            var isEnglish = true;
            for (var index = 0; index <= textLength; index = index + 1) {

                if (msgText.charCodeAt(index) > 160) {
                    //Not English 
                    isEnglish=false;
                    break;
                }
            }

      



in the previous example, which I need, if one character is Arabic, all the text must be validated as Arabic, so I added the variable isEnglish = true by default and will only change if the character in the string is not English I repeated the characters in string using charCodeAt (index), which returns the ISO Latin-1 character number. using the table on this page i was able to work out that the maximum number in this set representing english characters is 160 and

+1


source


You can use ActiveXObject to send input say 'a' key and read its unicode / ascii value. If his English isn't English yet. Check this question:



Get current keyboard layout language in JavaScript

0


source


You can do this using the Google AJAX Language API

var content = "your text";
google.language.detect(content, function(result) {
  if (!result.error) {
    var language = 'unknown';
    for (lang in google.language.Languages) {
      if (google.language.Languages[lang] == result.language) {
        language = lang;
        break;
      }
    }
    // Now use the variable `language`
  }
});

      

0


source







All Articles