Chapter 12·Scripts and Layout·~10 min·
Bidirectional Text and Unicode Isolation
Right-to-left scripts such as Arabic and Hebrew embed left-to-right runs for numbers, Latin brand names, and code identifiers. Unicode directional isolates prevent adjacent runs and neutral punctuation from reordering across boundaries.
Overview
Arabic and Hebrew text flows from right to left, while embedded numbers, Latin brand names, and code identifiers flow from left to right. Neutral punctuation placed between directional runs often attaches to the wrong side. Unicode directional isolates (`FSI` and `PDI`) contain each embedded value in its own directional run.
Core rule
Isolate every interpolated variable in bidirectional text using HTML <bdi> elements or Unicode FSI/PDI code points (U+2068 and U+2069).
Specification note: Unicode 6.3 introduced directional isolates (U+2068 FSI and U+2069 PDI) because earlier directional embeddings allowed interpolated values to alter surrounding sentence order.
Failure mode
Arabic, Hebrew, Persian, and Urdu flow from right to left, while embedded numbers, Latin identifiers, and URLs flow from left to right. The Unicode Bidirectional Algorithm (UAX #9) resolves static text runs automatically, but dynamic placeholders interpolated at runtime often corrupt adjacent neutral punctuation or merge with neighboring runs. For example, an unisolated price such as $999 or a version string with colons and slashes can reorder across an Arabic sentence boundary.
Arabic and Hebrew read right-to-left, but numbers, Latin brand names, and code remain left-to-right. Mixing them causes text to shuffle out of order.
Business stakes & user risk
Punctuation flipping to the wrong side of sentences and numbers inverting phone numbers or version strings (e.g., 'v2.4' becoming '4.2v') makes technical copy illegible.
Rule of thumb
Wrap dynamic user-generated content and Latin phrases in <bdi> (bidirectional isolate) elements inside RTL pages.
The Novice Trap vs. Production Standard
Contrast the intuitive assumption with the architecture required for global releases.
“Setting dir='rtl' on the page container handles all right-to-left formatting automatically.”
Naive approach: Plain inline interpolation
<p dir="rtl">رمز التفعيل الخاص بك هو: {code}.</p>Defect:If code is 'ABC-123', the trailing period and numbers jump to the far right, rendering a scrambled sequence.
The Unicode Bidirectional Algorithm (UBA) relies on surrounding character types. Neutral punctuation (parentheses, slashes, periods) easily attaches to the wrong text span.
Production standard: Bidirectional isolation
<p dir="rtl">رمز التفعيل الخاص بك هو: <bdi>{code}</bdi>.</p>Benefit:Creates an isolated directional boundary, preventing the embedded code from corrupting the surrounding Arabic punctuation.
Cross-Functional Review Questions
Questions for product managers, designers, engineers, and testers to ask during sprint reviews.
“Are all dynamic variables, user names, and phone numbers wrapped in <bdi> tags?”
“Are mixed Arabic-English UI layouts tested with realistic alphanumeric serial codes?”
“Do testers verify that parentheses, quotation marks, and question marks land on the correct side?”
“Are email addresses and URLs preserved in strict left-to-right reading order in RTL views?”
Mechanism
Directional classes and Unicode isolation
The Unicode Bidirectional Algorithm (UAX #9) assigns a directional class to every code point. Latin characters have strong left-to-right (L) directionality, while Arabic and Hebrew characters have strong right-to-left (R or AL) directionality. ASCII and Arabic-Indic digits have weak directionality, and punctuation, whitespace, and currency symbols have neutral or weak directionality.
Neutral characters take their direction from the surrounding strong characters. When an unisolated string such as $999 is interpolated into an Arabic sentence, the $ sign sits between a strong right-to-left Arabic letter and a weak digit. The algorithm resolves the $ as part of the right-to-left context and moves it to the right side of the number (999$).
Directional isolates prevent this reordering. Wrapping an interpolated value in Unicode First Strong Isolate (U+2068 FSI) and Pop Directional Isolate (U+2069 PDI), or in an HTML <bdi> element, evaluates the interior run independently by its own first strong character and treats the entire isolate as a single neutral object in the outer sentence.
Interactive demo
Guided exercises
- 1Keep the preset “Arabic with English brand name” and switch the strategy to “Plain interpolation”. The top preview matches the unisolated reference below it. The dollar sign in $999 jumps to the right of the digits (999$) because the preceding Arabic word pulls the weak currency symbol into the RTL run.
- 2Switch the strategy back to “FSI / PDI (recommended)”. The price renders accurately as $999, and the code-point inspector reveals the [FSI]…[PDI] boundary markers isolating each interpolated variable.
- 3Select the preset “Code snippet inside RTL paragraph” with “Plain interpolation”. The identifier getValue() renders as ()getValue because the trailing parentheses are neutral characters bordered by right-to-left Arabic text.
- 4Edit the {price} input field to test “$999” and “999 USD” under plain interpolation. Without isolation, $999 inverts to 999$ and 999 USD inverts to USD 999, demonstrating that dynamic values corrupt layout unless isolated by the template.
Output · with FSI / PDI (recommended)
Rendered with dir="rtl" and lang="ar".
For comparison · without isolation
Every code path that inserts values without isolates renders this way.
The raw character sequence
اشتر [FSI]iPhone 14 Pro[PDI] الآن بسعر [FSI]$999[PDI] فقط، عرض حتى [FSI]12/31[PDI].
Cheat sheet
FSI/PDI: isolate a value and give it the direction of its first strong character. Fluent and the messageformat package (MessageFormat 2) add them by default; ICU MessageFormat libraries such as FormatJS do not.
LRE/RLE: legacy embedding controls, still found in older codebases. New code should use FSI/PDI.
LRM/RLM: invisible characters with a strong direction. One placed after trailing punctuation or symbols (the ++ in C++) sets the direction they take.
Implementation rules
- Wrap dynamic HTML interpolations in
<bdi>elements. The<bdi>element isolates the embedded text run so its internal directionality cannot affect surrounding punctuation. - Use Unicode isolate code points in plain-text channels. In push notifications, SMS messages, and document titles, wrap interpolated values in
U+2068(First Strong Isolate) andU+2069(Pop Directional Isolate):`\u2068${value}\u2069`. - Set
dir="auto"on user-generated text inputs and containers. The browser inspects the first strong directional character of each block and assigns the matching base direction. - Force
dir="ltr"on structured technical tokens. Wrap telephone numbers, IP addresses, file paths, and SKU codes in<bdi dir="ltr">so leading plus signs, dots, and slashes remain anchored in left-to-right order.
Operational context
Roles
Workflow stages
- ·Reviewing templates for right-to-left releases
- ·Formatting prices, phone numbers, and codes in RTL sentences
- ·Auditing message catalogs and notification pipelines
See it in a market: Saudi Arabia · an RTL market →
Production incident
Unisolated currency symbols, trailing parentheses in function names, and leading plus signs in international phone numbers frequently invert when interpolated into Arabic or Hebrew templates.