Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify if user is typing in RTL or LTR language?

I want imitate google's input, It automatically change input's typing direction based on the language you're typing in.

How can I identify if user is typing in RTL or LTR language? It must work cross-browser.

like image 732
Alvarez Avatar asked Oct 25 '12 15:10

Alvarez


People also ask

How do you convert RTL to LTR?

Shortcuts: Ctrl+Shift+J -- switch to RTL Ctrl+Shift+E -- switch to LTR Note 1: If one or both of the shortcuts don't work, it means that something else is mapped to them.

Is English LTR or RTL?

For example, the en-US locale (for US English) specifies left-to-right. Most Western languages, as well as many others around the world, are written LTR. The opposite of LTR, RTL (Right To Left) is used in other common languages, including Arabic ( ar ) and Hebrew ( he ).

How do you type RTL?

To switch between RTL (Right-To-Left) and LTR (Left-To-Right) text directions, you need to click Ctrl + Shift : Ctrl + Left Shift for LTR .

How do I display text left to right in HTML?

Setting up a right-to-left pageAdd dir="rtl" to the html tag any time the overall document direction is right-to-left (RTL). This sets the default base direction for the whole document. All block elements in the document will inherit this setting unless the direction is explicitly overridden.


2 Answers

You should use the attribute dir="auto"

e.g.

<html dir="auto">

This way the browser will look at the first strongly typed character and adjust the text automatically.

For reference, here is the W3C documentation on this: http://www.w3.org/International/tutorials/new-bidi-xhtml/qa-html-dir

like image 145
Toby Avatar answered Sep 23 '22 18:09

Toby


If you want to mimic Google's directionality recognition algorithm, you will need to listen to input change, recognize whether the character inserted was RTL or LTR (or neutral) and change the textbox's dir="" attribute accordingly.

Google's algorithm, for the most part, seems to calculate the majority of strong characters in the string and decide the directionality from that. If you type RTL it will switch the context to RTL, and if you then switch to LTR for the same paragraph, it may switch the context again to LTR if those characters outnumber the RTL ones.

For comparison, Facebook uses a direction algorithm as well, but it is slightly different - it seems to use the first strong character to decide the direction of the paragraph rather than the overall number.

(For the record, Google also seems to have several algorithms for this; Gmail behaves slightly differently than Google Hangouts which is different than how the input in Google search is aligning itself. In these things, there are mostly no "right" or "wrong" answers but rather what fits your use case)

Whichever method you choose to implement, you first need to identify what the user is typing. There are several ways to do this, but I would recommend the following:

  • Read a little about Unicode BiDirectional Algorithm (especially about "strong" type characters) http://unicode.org/reports/tr9/
  • Find a good way to identify strong characters in your context. An example of a regex to do that can be found in MediaWiki's Language file (where group 1 is LTR and group 2 is RTL): https://github.com/wikimedia/mediawiki/blob/6f19bac69546b8a5cc06f91a81e364bf905dee7f/languages/Language.php#L174

You can create a JavaScript method that listens to the user's input, uses the regex above to identify which strong character is used (either by first character or by counting them all, whichever works best for your use and scale) -- and change the textbox's dir="" attribute accordingly.

Make sure you later display the submitted text with the correct alignment later, so you may have to either use something to store the alignment you picked or to re-recognize whenever you render it. Either way, don't forget that the display needs the same dir="" attribute as well.

like image 25
mooeypoo Avatar answered Sep 23 '22 18:09

mooeypoo