hi
I want to detect input language of client. so
i tried in java script with(navigator)it dosent work for my perpose
i also used ( HTTP_ACCEPT_LANGUAGE) it return system language not keyboard language?
what i must do?

Dani AI

Generated

You cannot reliably read the user’s keyboard layout from JavaScript. navigator.language and Accept-Language describe browser/UI preferences, not the active keyboard, and key events expose characters, not layout. For ’s goal (Persian vs. English aligning right/left), let the text itself drive direction instead of trying to detect the keyboard. Modern browsers can do this natively, and users can still override when needed as suggested.

Minimal markup/CSS that auto-aligns as the user types:

<input type="text" dir="auto" placeholder="Type Persian or English">
<textarea dir="auto" placeholder="..."></textarea>
input, textarea { text-align: start; }   /* left for LTR, right for RTL */

For older browsers that lack good dir="auto" support, add a tiny fallback that sets direction based on the first strong character (covers Persian/Arabic/Hebrew vs. Latin). It avoids flipping on digits/punctuation:

<script>
(function () {
  function firstStrongDir(s) {
    for (var i = 0; i < s.length; i++) {
      var c = s.charCodeAt(i);
      if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) return 'ltr';                 // A-Z a-z
      if ((c >= 0x0590 && c <= 0x05FF) || (c >= 0x0600 && c <= 0x08FF)) return 'rtl';   // Hebrew + Arabic ranges
    }
    return 'ltr';
  }
  document.addEventListener('input', function (e) {
    var el = e.target;
    if (el && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA')) {
      el.dir = firstStrongDir(el.value);
    }
  }, true);
})();
</script>

Tip: also provide a small LTR/RTL toggle next to the field so users can assert their intent when mixing languages, aligning with ’s usability point.

Recommended Answers

All 4 Replies

There is a way to do it in Flash, but I am not sure about JavaScript alone.

What are you trying to do, exactly?
Are you trying to adjust the page content based on some setting? If so, that is not a Good Thing.
My current keyboard configuration has nothing to do with the language(s) I want to see in pages I visit.

Offer the user a button to click - and, please, no flags.

forexample if client type in Persian textbox align must be "rtl"
and if he is typing in English textbox align must be "ltr"

if client type in Persian textbox align must be "rtl"
and if he is typing in English textbox align must be "ltr"

This little demo

<html> 
  <head> 
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org"> 
    <title></title> 
  </head> 
  <body> 
    <textarea onclick="shoDir(this)" id='one' dir='rtl'> 
آزمون
</textarea> 
    <textarea onclick="shoDir(this)" id='two' dir='ltr'> 
test
</textarea> 
<script type="text/javascript"> 
function shoDir(that){
    alert(that.getAttribute('dir'))}
    </script> 
  </body> 
</html>

shows how to test the 'dir' attribute of a textarea. The code to set it is quite similar: .setAttribute('dir','rtl') or .setAttribute('dir','ltr') .

I don't have the required language support, so I can't test whether the attribute accessible to javascript changes when the keyboard is used to change the active language for input.

Caution: this toy does not specify a charset or codepage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.