demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

Using Arabic, Hebrew, and Other Right-To-Left Languages In Web Pages

By default, text runs from left-to-right in web pages, as all Indo-European languages are written in this direction. However Arabic, Hebrew, Urdu and several other languages are written in the opposite direction, that being right-to-left. Getting the text itself onto the page is not a particular issue, but forming it to run in the right direction can be.

Step One: Set The Direction Of The Text

The base direction of text in a web page is determined by the did attribute in the <html> tag. did by default is set to “ltr” (left to right), and does not need to be explicitly declared. To set the opposite direction, we set did to “rtl” (right to left):

  1. <html dir=”rtl” xmlns="http://www.w3.org/1999/xhtml">

At the same time we should declare the language being used on the page (assuming that the body text is primarily in one language). I will use Hebrew for this example:

  1. <html lang="he" dir=”rtl” xml:lang="he" xmlns="http://www.w3.org/1999/xhtml">

(Arabic has far more variants, and thus more possible specific values, although ar is perfectly acceptable catch-all for specifying that the body text is in Arabic.)

So long as you are using UTF-8 encoding on your pages, as we discussed in setting up your HTML document, everything else should follow naturally. I would recommend that you follow general recommendations for creating body copy: writing text in a correctly configured word processor, and copying and pasting the text into appropriate markup in your document.

  1. <p>על רקע חילוקי הדעות בין ירושליה ישראלית</p>

Step Two: Deal With Exceptions

Assuming that the majority of the document was in Hebrew and you wanted to write an English paragraph somewhere in the body, you would declare that paragraph as an exception:

  1. <p lang=”en” dir=”ltr”>I have reverted to English for a moment</p>

If it was only a word or a short phrase inside a sentence that you wanted to effect, you would use <span> around the relevant text, with the same values for lang and did using in the <span> as used for the paragraph above.

Step 3: Form The Title

The one remaining issue is that text in the <title> tag will ignore directionality, and be left-to-right no matter what we have stated. We cannot use the did attribute there; instead, we use HTML entities, in the form of Unicode control characters:

  1. <title>&#x202B; שרי השביעייה דנו בתשובת ישראל לארה"ב&#x202C;</title>

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.