When an accounting app's Arabic or Hebrew text contains Latin-script names, codes or amounts, why can the line display scrambled, and what prevents it?
answer
- stored order is not display order
- strong, weak and neutral characters
- who decides the paragraph's direction
- a value resolved as its own unit
- brackets mirrored by the engine
basics
~20 sIn Arabic or Hebrew text, the Unicode Bidirectional Algorithm resolves spaces and punctuation from the surrounding text, so they can join the wrong run. Text components should take base direction from the message's language and isolate each inserted value.
solid answer
~50 sA right-to-left line in an accounting product rarely stays pure: it carries a supplier's Latin-script name, a reference code, an email address or an amount. The text engine orders it with the **Unicode Bidirectional Algorithm**. Letters are *strong*, digits are *weak*, and spaces and most punctuation are *neutral*: a neutral between runs of different direction takes the paragraph's base direction. That is why the period of "Halden & Co." can jump to the far side of the name, or a code like `48-17-B` can show as `B-48-17` in Hebrew or `B-17-48` in Arabic. The digits of a number always keep left-to-right order. Two habits prevent the damage: set each paragraph's **base direction** from the message's language instead of letting its first letter decide, and **isolate** every inserted value so it is resolved as its own unit. Parentheses that look reversed are drawn that way by the engine and must not be swapped by hand.
code
pseudocode · 7 linesmessage = translate("supplier_paid", locale) // "{supplier} PAID {amount}" in Arabic
text.baseDirection = directionOf(locale) // from the language, not the first letter
text.value = fill(message, {
supplier: isolate(supplierName, FIRST_STRONG), // may be Latin or Arabic
amount: isolate(formattedAmount, LEFT_TO_RIGHT)
})
// never: reverse(supplierName) or swapping "(" and ")" in a translationgo deeper
Recall that spaces and punctuation take their direction from surrounding text, that digits stay left to right, and that inserted values belong in isolates inside a paragraph whose direction comes from the message's language.
Explain how strong, weak and neutral characters resolve, why a trailing period or a following number joins the wrong run, and why an isolate fixes it where an embedding can leak.
Show how you would make isolation the text component's default for every inserted value, choose explicit or first-strong direction per field, and catch hand-reversed strings in review.
Weigh making bidirectional handling a guaranteed property of the design system's text components against leaving it to each product, and decide which mixed-direction cases must work before a right-to-left launch.
## A right-to-left line is rarely pure Arabic, Hebrew, Persian and Urdu are written right to left, but a line of Arabic in an accounting product almost always carries left-to-right material: - a supplier's Latin-script name, such as "Halden & Co."; - a purchase reference, such as `48-17-B`; - an email address, such as `[email protected]`; - an amount, such as 3,480.75. Text with both directions in one line is **bidirectional text**. It is stored in **logical order**, the order it is typed and read in, and the text engine computes the **visual order** when it draws the line. The authority for that computation is the **Unicode Bidirectional Algorithm**, published as Unicode Standard Annex #9. Whether the screen layout itself mirrors is a separate decision that the component library owns; this question is only about the order of characters inside one line. In the examples below, UPPERCASE stands for Arabic letters and lowercase for Latin letters, a common convention when right-to-left script cannot be shown. ## How the algorithm resolves a mixed line Every character has a **bidirectional type**: | Type | Examples | Behaviour | |---|---|---| | Strong left-to-right | Latin letters | Always run left to right | | Strong right-to-left | Arabic and Hebrew letters | Always run right to left | | Weak | Digits, plus and minus signs, number separators | Digits keep left-to-right order; their placement depends on nearby text | | Neutral | Spaces, most punctuation, `&`, quotation marks | Take a direction from the text around them | The steps that matter to a design system are: 1. **Base direction.** Each paragraph gets a base direction. A higher-level setting can state it explicitly; otherwise the algorithm's default takes it from the paragraph's first strong character. 2. **Weak characters.** Digits that follow Latin letters join the left-to-right run; digits that follow Arabic letters form a separate number run that still reads left to right. 3. **Neutral characters.** A neutral between two runs of the same direction takes that direction. A neutral between runs of different direction, or between a run and the paragraph edge, takes the base direction. For this step, a number that did not join a Latin run counts as right-to-left. 4. **Reordering.** Runs are laid out in the base direction, right-to-left runs are reversed, and left-to-right runs and numbers keep their internal order. ## What goes wrong without help None of these is a translation error: the stored text is right and only the display is wrong. 1. **The wrong base direction.** A translated Arabic message that starts with a placeholder, `{supplier} PAID THE INVOICE`, begins with a Latin letter once the name is filled in. If direction is detected from the first strong character, the whole line becomes a left-to-right paragraph and the Arabic phrase lands on the wrong side of the name. 2. **Punctuation on the wrong side.** In `INVOICE FROM halden & co. WAS PAID`, the period after "co" sits between a Latin run and an Arabic run, so it takes the right-to-left base direction and is drawn on the far side of the name, as `.halden & co`. 3. **A code split apart.** In Hebrew text, `48-17-B` keeps "48-17" together as one number, but the second hyphen sits between a number and a Latin letter and takes the base direction, so the line can show `B-48-17`. After Arabic letters the digits count as Arabic numbers, which a hyphen does not join, so each group becomes its own run and the line can show `B-17-48`. 4. **A number captured by a name.** In `{supplier} 3 INVOICES`, the 3 follows Latin letters, so it joins the name's left-to-right run and can appear on the name's other side. ## What the design system's text components decide - **Base direction comes from the language.** The text component sets each translated message's direction explicitly from its language, so a message that opens with a placeholder still lays out right to left. - **First-strong detection is for unknown content.** A note a user typed may be in either script, so detecting direction from its first strong character is reasonable there. - **Every inserted value is isolated.** An isolate makes the engine resolve the value as its own unit, and from outside the whole value behaves like one neutral character. Use a left-to-right isolate for codes, email addresses and amounts, and a first-strong isolate for names that may arrive in either script. Failures 2, 3 and 4 above disappear, and the first-strong rule skips isolated text, which also protects failure 1. - **Isolates, not embeddings.** Unicode 6.3 added the isolate controls: left-to-right, right-to-left and first-strong isolate, each closed by a pop directional isolate. The older embedding controls also set a direction, but the embedded text still influences how the characters after it resolve, so the number in failure 4 can still be captured. Platforms expose isolation as a markup element or a property of the inserted value; the text component should apply it by default. - **Never reverse strings by hand.** Text is stored and translated in logical order. A value reversed so that it "looks right" in one spot is reordered again by the engine and breaks. - **Digits stay left to right.** The engine already keeps an amount's digits in order, so no component should reverse them. Which digit shapes a locale uses is a formatting decision made elsewhere. ## Mirrored brackets are drawn by the text engine Parentheses, square brackets and similar paired characters carry the Unicode **Bidi_Mirrored** property. When one of them sits in a right-to-left run, the text engine draws its mirror image, so a typed `(` before an Arabic note appears on the right side, visually facing the note as an opening bracket should. Nobody designs this, and nobody should "fix" it: a translator who swaps `(` and `)` because the source looked backwards makes the engine mirror the swapped pair, and the note then displays with its brackets facing outward.
- Why are isolation controls preferred over the older embedding controls for a value inserted into right-to-left text?An embedding sets a direction for its contents, but those contents still influence how the characters after it resolve: a number right after an embedded Latin name can be pulled into the name's run. An isolate, added in Unicode 6.3, is resolved on its own and looks like a single neutral character from outside, so nothing leaks in either direction. The first-strong rule for paragraph direction also skips isolated text.
- How should a text component choose the base direction for a field showing user-typed notes of unknown language?The interface language is the wrong guide here, because a supplier may type a Latin-script note inside the Arabic interface. First-strong detection fits: each paragraph or value takes the direction of its first strong letter. It is still isolated from the labels around it. Messages the product translates itself keep the explicit direction of their language instead of a guess.
- Should a translator swap the parentheses in an Arabic string because the source text looked backwards?No. Parentheses are stored in logical order, the opening one before the enclosed text, and the text engine draws them as mirror images inside a right-to-left run because of their mirrored property. Swapping them by hand makes the engine mirror the swapped pair, so the brackets display facing outward.
- Digits inside Arabic or Hebrew text are displayed right to left like the letters.
- Reversing a Latin-script value's characters makes it read correctly in right-to-left text.
- Detecting direction from the first letter is fine for translated messages that start with a placeholder.
- Parentheses in right-to-left text must be swapped by hand to face the right way.
- Isolation only matters when right-to-left values are inserted into left-to-right text.
- The older embedding controls and the newer isolates behave the same way.