Sub topic 12
Supporting right-to-left languages
This chapter assumes you have set up react-i18next, can switch the locale at runtime, and already keep the lang and dir attributes on the html element in sync. It covers the CSS side: making the layout follow the text direction.
What changes in a right-to-left language
Arabic, Hebrew, Persian and Urdu are written right to left (RTL). The browser reverses text flow once dir="rtl" is set, but your stylesheet can still undo that. Anything written with left or right stays fixed to the physical side of the screen.
Setting the direction is the first step. i18next reports it for you:
import i18n from './i18n';
export function applyDirection(code: string) {
document.documentElement.lang = code;
document.documentElement.dir = i18n.dir(code); // 'rtl' for 'ar', 'he', 'fa'; otherwise 'ltr'
}Use logical properties instead of left and right
CSS logical properties describe sides relative to the writing direction. The start of a line is the left in English and the right in Arabic. The end is the opposite side.
| Physical property | Logical property |
|---|---|
| `margin-left` | `margin-inline-start` |
| `padding-right` | `padding-inline-end` |
| `border-left` | `border-inline-start` |
| `text-align: left` | `text-align: start` |
| `left: 0` | `inset-inline-start: 0` |
Here is a card that mirrors with no extra rules:
.card {
padding-inline: 1rem;
margin-inline-start: 2rem;
border-inline-start: 4px solid currentColor;
text-align: start;
}Under dir="ltr" the border and margin sit on the left. Under dir="rtl" they sit on the right. You write one rule, and the browser resolves it.
Flexbox and grid already follow direction. A flex-direction: row container lays its children out from the start side, so a row of buttons reverses by itself. Do not add a .rtl class with duplicate rules. That doubles your CSS and drifts out of sync.
Things that need manual handling
Some items should mirror and some should not.
- Directional icons. Arrows, back buttons and "next" chevrons should flip. Icons with no direction, such as a clock, a search glass or a play button, should stay as they are.
- Numbers and Latin text. Digits and embedded English words keep their own left-to-right order inside an RTL line. The browser's bidirectional algorithm handles this. Mixed content can still render oddly, so test it.
- User-supplied text. Names and comments may use a different direction than the page. Set
dir="auto"on the element that renders them.
You can flip an icon with a rule keyed to the document direction:
[dir='rtl'] .icon-directional {
transform: scaleX(-1);
}export function UserComment({ text }: { text: string }) {
return <p dir="auto">{text}</p>;
}Common mistakes
- Hard-coded
leftandrightin third-party CSS or inline styles. Search your codebase for them. - Setting
dironbodyonly. Portals and modals can render outside it, so set it onhtml. - Testing only with translated text. Switch the direction on its own and check every screen, since the direction is often the missing piece.
Exercise
A sidebar has margin-left: 24px and text-align: left. Rewrite both lines so they work in left-to-right and right-to-left languages, and explain why dir="rtl" alone does not fix the original.
Show answer
Use margin-inline-start: 24px and text-align: start. Setting dir="rtl" reverses text flow but does not change physical properties. margin-left stays on the left edge, so the sidebar's spacing would sit on the wrong side. Logical properties resolve against the document direction, so they move to the right edge in RTL.