March 12, 2012

How to determine the language of keyboard input using JQuery

Question by Maha Khairy

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

all the solutions I came across determined the default language for the system and browser but nothing I found helped me determining the current input language

Answer by Starx

You can do this using Google’s 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`
  }
});
...

Please fill the form - I will response as fast as I can!