3

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

Maha Khairy
  • 352
  • 5
  • 15

3 Answers3

1

ok here is the solution that worked for me. Note that in my application I know for sure the input language will be one of two Languages either input is English Or Arabic so here 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 that is what I Needed , if a single character is Arabic the whole text should be validated as Arabic so I added a variable isEnglish = true by default and will only be changed if a character in the string is not English I iterated the characters in the string using the charCodeAt(index) which returns the ISO Latin-1 Character Number . using the table in the this page I was able to decide that the maximum number in this set that represents English chars was 160 and

Maha Khairy
  • 352
  • 5
  • 15
0

You can use ActiveXObject to send an input key say 'a' and read its unicode/ascii value. If its 97 its English else not. Check out this question :

Get the current keyboard layout language in JavaScript

Community
  • 1
  • 1
Shaunak
  • 17,377
  • 5
  • 53
  • 84
  • the same solution doesn't work but it helped me figure something out, I'll need to know more about it though before I consider it solved, and I'll post what I found here – Maha Khairy Mar 12 '12 at 10:15
0

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`
  }
});
Starx
  • 77,474
  • 47
  • 185
  • 261
  • thanks for helping me with my problem but I tried this solution and it takes a lot of time and using jquery was to save time in the first place, besides I think it always return the same value for both languages – Maha Khairy Mar 12 '12 at 10:18
  • @MahaKhairy, Its not that unuseful. It returns almost correct languages if it has plenty of characters to read and all are in UTF8 encoding. But it is not always 100% – Starx Mar 12 '12 at 10:34
  • thanks for your comment I didn't know a lot about this google api, probably the enough characters condition was not applied but my application should work even with one char, anyway the call takes a lot of time and I need to apply that validation with each keypress – Maha Khairy Mar 12 '12 at 11:00