Shelves / Internationalisation
IN·06
Right-to-Left and Bidirectional Text
How Arabic, Hebrew, Urdu and Persian change layout and text: logical versus visual order, the Unicode Bidirectional Algorithm and why numbers and Latin words inside Arabic sentences move, direction for documents, paragraphs and user-generated text, isolation with dir="auto" and FSI/PDI, mirroring layouts with logical CSS properties and Compose and SwiftUI's start/end, which icons flip and which don't, numbers and digits in RTL, and bidi-control spoofing; with labs that mirror a header in Chromium and reorder mixed-direction strings.
Start here: when a name moves the number
TiffinBox launched in Dubai. Its English notification template was Order by {name}: {count} items. A customer named محمد placed an order, and the bidi lab showed what a screen displays:
| Text stored (logical order) | Displayed |
|---|---|
Order by محمد: 3 items |
Order by 3 :دمحم items |
| the same, with the name isolated (FSI … PDI) | Order by دمحم: 3 items |
(Arabic letters in the "displayed" column are shown in display order, so they read right to left.) Without isolation, the Unicode Bidirectional Algorithm treated : 3 as part of the Arabic run, so the count jumped to the other side of the name. Wrapping the inserted name in isolation characters kept the sentence intact.
A layout lab in Chromium showed the other half of RTL. A header with a title and a "more" icon, pushed apart with margin-left: auto:
| CSS | dir="ltr" |
dir="rtl" |
|---|---|---|
physical margin-left: auto on the icon |
title left, icon right ✔ | title right, icon also right, squeezed next to the title ✘ |
logical margin-inline-start: auto |
title left, icon right ✔ | title right, icon left ✔ (mirrored) |
And one security lab: a file named invoicefdp.exe displays as invoiceexe.pdf, because U+202E (RIGHT-TO-LEFT OVERRIDE) reverses everything after it. The program is an .exe that looks like a PDF.
Right-to-left support is two jobs: mirroring the layout and ordering mixed-direction text. Both are mostly done by the platform, if you let it.
How deep to go
Volume 17 defines six mastery levels. For RTL and bidi:
| Level | What you can do |
|---|---|
| L1 | Set document direction and use logical layout properties |
| L2 | Build screens that mirror correctly and display mixed-direction text with isolation |
| L3 | Explain the bidi algorithm's character types, runs, embedding levels and isolation |
| L4 | Diagnose garbled mixed text, mirroring mistakes and bidi spoofing |
| L5 | Set RTL design and engineering standards for a product, including icons, charts and input |
| L6 | Work on text layout engines or RTL-heavy design systems |
Which scripts, how many users
Right-to-left scripts include Arabic (Arabic, and adapted for Urdu, Persian, Pashto, Kurdish Sorani, Sindhi, Uyghur), Hebrew (and Yiddish), Syriac, Thaana (Dhivehi) and N'Ko. Hundreds of millions of people read them daily; for TiffinBox, Dubai (Arabic) and India itself (Urdu, Kashmiri, Sindhi are written in Arabic-derived scripts) both need RTL.
Logical order and the bidi algorithm
Text is stored and typed in logical order: the first character read is first in memory, whatever the direction. Display order is computed by the Unicode Bidirectional Algorithm (UBA, UAX #9), which every text engine implements.
Each code point has a bidi class:
| Type | Examples | Behaviour |
|---|---|---|
| strong L | Latin, Devanagari, Telugu, CJK letters | left to right |
| strong R / AL | Hebrew / Arabic letters | right to left |
| weak | European digits (EN), Arabic-Indic digits (AN), +, -, %, currency symbols |
take direction from context; digit sequences stay LTR internally |
| neutral | spaces, most punctuation, : ( ) |
take the direction of surrounding strong text |
| explicit formatting | LRE, RLE, LRO, RLO, PDF, LRI, RLI, FSI, PDI, LRM, RLM, ALM | control embedding, override or isolation |
Simplified, the algorithm:
- Determines the paragraph direction (from markup, or from the first strong character).
- Resolves weak and neutral characters from their neighbours.
- Assigns embedding levels (even = LTR, odd = RTL) and lays out runs: RTL runs are reversed in visual order; numbers inside them keep their own LTR order.
- Mirrors paired characters in RTL runs:
(displays as),<as>,[as].
Problems arise at the boundary between an inserted value and its surrounding sentence. The lab's English sentence with an Arabic name: : 3 sat between strong Arabic (the name) and strong Latin (items); the neutrals and the digit attached to the Arabic run, so the display became "Order by 3 :دمحم items". The lab's Arabic sentence with a phone number: +91 98765 43210 inside Arabic text displayed as 43210 98765 91+ because the spaces between digit groups were resolved as RTL, reordering the groups (each group kept its digits in order). A Hindi sentence with an English dish name (आज का खाना: Curd rice (2)) displayed correctly, since both scripts are LTR.
Isolation
Isolate every value inserted into a sentence, so its direction can't affect its neighbours:
| Context | How |
|---|---|
| Unicode text (notifications, logs, plain-text emails, native strings) | wrap in FSI (U+2068 FIRST STRONG ISOLATE) … PDI (U+2069); or LRI/RLI when direction is known |
| HTML | <bdi>{name}</bdi>, or dir="auto" on the element holding user content |
| Android | BidiFormatter.getInstance().unicodeWrap(name); TextDirectionHeuristics |
| iOS | NSAttributedString with base writing direction, or FSI/PDI |
| ICU message libraries | many wrap arguments in isolates automatically (FormatJS can) |
Use isolates (LRI, RLI, FSI + PDI), not the older embeddings and overrides (LRE, RLE, LRO, RLO + PDF), which leak into surrounding text. For phone numbers and other LTR data in RTL text, wrap them in LRI … PDI or in an element with dir="ltr".
Mirroring layouts
In RTL, the whole interface reads from right to left: navigation, reading order, alignment, progress, swipe directions.
Use logical properties
| Physical (don't) | Logical (do) |
|---|---|
CSS margin-left, padding-right, left, text-align: left, border-left |
margin-inline-start, padding-inline-end, inset-inline-start, text-align: start, border-inline-start |
Android paddingLeft, layout_marginRight, Gravity.LEFT |
paddingStart, layout_marginEnd, Gravity.START (and android:supportsRtl="true") |
Compose absolute modifiers (absolutePadding) |
padding(start = …), Arrangement.Start: Compose follows LocalLayoutDirection |
SwiftUI / UIKit .left, .right |
.leading, .trailing |
Set the document direction: <html dir="rtl" lang="ar"> from the active locale. Flexbox and grid follow the direction automatically; physical properties don't, which is exactly what the lab's header showed.
What flips and what doesn't
| Flip | Don't flip |
|---|---|
| back/forward arrows, chevrons showing navigation | media controls (play, fast-forward), since time runs left to right on progress bars worldwide |
| progress direction of steppers and sliders | clocks, circular refresh arrows (debated; follow platform guidance) |
| icons showing text direction (indent, list bullets) | checkmarks, logos, brand marks |
| send/reply icons pointing "forward" | images of real objects (a phone, a scooter) unless direction carries meaning |
| swipe-to-go-back gestures and drawer edges | numbers, phone numbers, code, URLs (they stay LTR) |
Android (autoMirrored on vector drawables) and iOS (imageFlippedForRightToLeftLayoutDirection, SF Symbols' automatic variants) mirror marked icons for you.
Charts, tables and forms
- Tables and lists mirror (first column on the right). Time-series charts are a judgment call; many Arabic products keep time running left to right; be consistent and ask native-speaker designers.
- Text fields: alignment follows the content's direction; email, URL, phone and code fields are LTR even in RTL layouts (
dir="ltr"on those inputs). - Mixed content (an Arabic product name with an English brand) relies on isolation and
dir="auto".
Digits and numbers in RTL locales
Arabic-speaking regions differ: Egypt and much of the Levant and Gulf commonly use Arabic-Indic digits (٠١٢٣٤٥٦٧٨٩) in many contexts; the Maghreb uses Western digits; the UAE uses both. CLDR's default numbering system per locale follows common usage: in Node.js 22's data, ar-EG and ar-SA default to Arabic-Indic digits (١٬٢٣٤٫٥), while ar, ar-AE and ar-MA default to Western digits (1,234.5 and 1.234,5), and users can override with -u-nu-. Numbers are always written most significant digit first and keep LTR order within the number. Accept both digit sets in input (chapter 04).
Bidi spoofing
Explicit direction controls can make text display differently from what it is:
- File names: the lab's
invoicefdp.exedisplayed asinvoiceexe.pdf. Attackers have used this in email attachments. - Source code: the "Trojan Source" attacks (CVE-2021-42574) put bidi controls in comments and strings so code reviewers saw different logic from what the compiler ran. Compilers, GitHub and many editors now warn about them.
- Display names and URLs: overrides and look-alike characters (chapter 07) impersonate other users or domains.
Defences: strip or reject bidi control characters (U+202A–U+202E, U+2066–U+2069, and U+200E/U+200F/U+061C marks where not needed) in identifiers, file names and display names; show warnings for them in code review and diff tools; display untrusted text inside isolates so it can't affect surrounding UI. The lab's detection regex found the U+202E character directly.
Testing RTL
- Pseudo-RTL (Android
ar-XB, Xcode's right-to-left pseudo-language, or forcingdir="rtl") on every screen, in CI screenshots. - Real Arabic or Hebrew content, including mixed strings with numbers, English names, phone numbers and emails.
- A native-speaking reviewer for designs: what feels natural (which charts flip, which icons) isn't fully specified anywhere.
- Bidi-control input in name and file fields.
Common traps
- Physical CSS and layout properties (left/right).
- Inserting user values into sentences without isolation.
- Mirroring everything, including media controls, logos and numbers.
- Forcing
dir="rtl"on email, URL and phone fields. - Reversing strings in code to "make them RTL".
- Allowing bidi controls in names and file names.
- Testing RTL only with Arabic UI strings and never with mixed user data.
Lab: mirroring and reordering
Setup. Two parts. Layout: Chromium (headless, Playwright, Node.js 22) renders a header row (a title and a "more" icon in a flex container) four ways: physical margin-left: auto and logical margin-inline-start: auto, each with dir="ltr" and dir="rtl"; the script reads the elements' positions. Text: Python 3.13 with python-bidi (its get_display, which implements the Unicode Bidirectional Algorithm including isolates) converts logical strings to visual order, the way a display would lay them out. get_display outputs a left-to-right sequence of characters, so Arabic in the "visual" column reads right to left. A regex searches for bidi control characters.
Results: layout
| CSS | dir="ltr" | dir="rtl" |
|---|---|---|
margin-left: auto (physical) |
title at left, icon at right | title at right, icon at right, squeezed next to the title |
margin-inline-start: auto (logical) |
title at left, icon at right | title at right, icon at left: correctly mirrored |
Results: text
| Case | Logical (stored) | Visual (displayed) |
|---|---|---|
| English sentence, Arabic name, count | Order by محمد: 3 items |
Order by 3 :دمحم items |
| the same, name in FSI…PDI | Order by محمد: 3 items |
Order by دمحم: 3 items |
| Hindi sentence, English dish | आज का खाना: Curd rice (2) |
unchanged (both LTR) |
| Arabic sentence, phone number | اتصل على +91 98765 43210 الآن |
نآلا 43210 98765 91+ ىلع لصتا |
| file name with U+202E | invoicefdp.exe |
invoiceexe.pdf |
The detection regex [--] found 0x202e in the file name.
What it shows
- Physical CSS doesn't mirror; logical properties do it with no extra code.
- Inserted values change their neighbours' order unless isolated; one isolate pair fixed the sentence.
- Numbers inside RTL text need care: each digit group stays in order, but space-separated groups can be reordered; wrap phone numbers in an LTR isolate.
- Overrides can disguise file types; detecting and stripping bidi controls is a one-line check.
Try it yourself
- Rebuild the header in Jetpack Compose with
Modifier.padding(start = …)andSpacer(Modifier.weight(1f)), then wrap it inCompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl). - Wrap the phone number in LRI (U+2066) … PDI and rerun
get_display. - In HTML, put
محمدinside<bdi>in the English sentence and compare with a plain<span>. - Make a file named with U+202E in a test folder and see how your OS's file manager, your email client and your app's upload screen display it.
- Run your app's main screens with
dir="rtl"(web) orar-XB(Android) and list every physical-direction bug.
Revision sheet: right-to-left and bidirectional text
Key ideas
- RTL scripts: Arabic (and Urdu, Persian), Hebrew and others; RTL support = mirrored layout + correct mixed-direction text.
- Text is stored in logical order; the Unicode Bidirectional Algorithm computes visual order from bidi classes (strong, weak, neutral, explicit).
- Isolate inserted values: FSI/LRI/RLI … PDI,
<bdi>,dir="auto",BidiFormatter. Prefer isolates to embeddings and overrides. - Use logical layout properties (start/end, inline-start/end, leading/trailing); set document direction from the locale.
- Flip navigation and progress icons; don't flip media controls, logos, numbers, phone numbers, URLs or code.
- Arabic locales differ in digits; accept both in input.
- Strip or reject bidi controls in names and file names; flag them in code review (Trojan Source).
- Test with pseudo-RTL, real mixed data and native reviewers.
Numbers to remember
- U+202E RLO; U+2066–U+2069 LRI, RLI, FSI, PDI; U+200E/U+200F LRM/RLM.
- CVE-2021-42574: Trojan Source.
Glossary
- Logical order: storage and reading order.
- Visual order: left-to-right position on screen.
- Bidi class: a code point's directional type.
- Embedding level: the depth of direction nesting; odd levels are RTL.
- Isolate: a bidi construct that keeps inserted text from affecting its surroundings.
- Mirroring: flipping layout or paired glyphs for RTL.
- Logical properties: start/end layout properties.