Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect user's language while he/she is typing

Tags:

javascript

guys i have a problem with this thing , i must know which language the user is using while typing in an input text to handle some bad shaped things and i even looked at this in here but it didn't helped me , can u guys help me with the right code ?
the languages i must know are english and persian :| thanks :) i need some javascript helps

<input type="text" id="give_me_the_lang">
<div id="show_lang_in_here"></div>
like image 680
user3590110 Avatar asked Jan 20 '15 09:01

user3590110


People also ask

How do you identify text language?

Google Translate - If you need to determine the language of an entire web page or an online document, paste the URL of that page in the Google Translate box and choose “Detect Language” as the source language.

How do I find the language in Excel?

On the Review tab, in the Language group, click Language. Click Set Proofing Language. In the Language dialog box, select the Detect language automatically check box. Review the languages shown above the double line in the Mark selected text as list.


1 Answers

Here is the solution that doesn't really detect language, but just checks if users input contains only English alphabet. If it does, it writes "English" in div, otherwise it writes "Persian". Code:

document.getElementById("give_me_the_lang").addEventListener("keyup", function() {
  if (/^[a-zA-Z]+$/.test(this.value)) {
    document.getElementById("show_lang_in_here").innerHTML = "English";
  } else {
    document.getElementById("show_lang_in_here").innerHTML = "Persian";
  }
});
<input type="text" id="give_me_the_lang">
<div id="show_lang_in_here"></div>

Expression ^[a-zA-Z]+$ only checks the letters of English alphabet, if you write in number or any other character, the result will be "Persian".

like image 161
Crepi Avatar answered Nov 15 '22 13:11

Crepi