0

I know I can look into the navigator object for the language using JavaScript. Is there a similar way to detect whether that language is a right to left language automatically, without having to have any configuration?

Edit I can't rely on the dir attribute, since that is what I'm attempting to accomplish.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445

2 Answers2

0

You could look for the html attribute that specifies this, e.g.

<html dir="ltr" lang="en-US">

In JavaScript you can get this property with:

var dir = document.documentElement.getAttribute('dir');

Of course this relies on the site proving the information.

Update to answer following update to question:

You can pull the text direction for each language from Unicode's CLDR

Using navigator.language is apparently not reliable. I'd render the HTML page on the back end after detecting preferred languages via the HTTP Accept-Language header. It'll probably be easier to maintain your database of text directions on the server side too.

Community
  • 1
  • 1
Tim
  • 8,036
  • 2
  • 36
  • 52
0

I know that you wrote "without any configuration" but you can create a dictionary of RTL languages, check the browser language and set accordingly the dir parameter.

The dictionary will not change in the near future, you can barely consider it as a constant like π and not a configuration

Guido Preite
  • 14,905
  • 4
  • 36
  • 65