Pinokyo
Pinokyo

Reputation: 566

html direction by language? (if english dir=ltr if arabic dir=rtl)

Can I do that?

I mean if, for example, the contents of the <div> is Hebrew or Arabic, float the <div> right and give it a dir="rtl" attribute. And, if the contents of the <div> is English, float to the left and change the dir attribute to "ltr".

Ss that possible?

Upvotes: 3

Views: 9368

Answers (3)

Gideon
Gideon

Reputation: 18491

If you don't know the language, you can use regular expression to find hebrew or arabic characters, and then set the direction accordingly.

Use regular expressions to find Hebrew: Javascript - how to find hebrew?

All unicode blocks, which you can use to edit the above solution to fit also Arabic: http://www.regular-expressions.info/unicode.html#block

Upvotes: 2

Paweł Dyda
Paweł Dyda

Reputation: 18662

You need to manually put dir attribute for for each div:

<div lang="ar" dir="rtl"><!-- some content here --></div>
<div lang="en" dir="ltr"><!-- some content here --></div>

This would automatically change flow. However, you might need to adjust certain elements depending on the language. You can do that by CSS lang pseudo-attribute:

p:lang(ar) { text-align: right; }
p:lang(en) { text-align: left; }

Of course you could possibly do that:

div:lang(en) { direction: ltr; }
div:lang(ar) { direction: rtl; }

This is unrecommended method. Since you need to put language somehow, you could also put dir attribute. If you do that from some programming language, directionality could usually be determined for given spoken language (Locale, Culture, whatever). But that would be another question.

Upvotes: 0

user753676
user753676

Reputation:

well f the user has another language version you can check this (dont know where you save the user language, in a cookie?) fetch this with php or javascript and load the right stylesheet with the right rules

Upvotes: 0

Related Questions