Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Build a Fast Bilingual Website with RTL and LTR Support

Building a bilingual website is not the same as translating a few strings. The direction of the interface changes, typography behaves differently, components need to survive longer text, and search engines need a clear relationship between language versions. This guide presents a practical architecture for websites that support both English and Arabic without duplicating the entire frontend. 1.…

Constructing a rapid bilingual website that accommodates both English and Arabic, with support for right-to-left (RTL) and left-to-right (LTR) scripts, demands more than straightforward text translation. The layout, typography, and component behavior must adapt to the changing directionality of the content. This report outlines a systematic approach to building such a site, ensuring that all elements function seamlessly regardless of the chosen language.

Firstly, treat language and direction as fundamental aspects of the document's state. Clearly define the language and direction by setting the `lang` and `dir` attributes on the root HTML element. For Arabic, use `lang="ar" dir="rtl"` and for English, use `lang="en" dir="ltr"`. This not only aids browsers and screen readers in rendering text correctly but also ensures that bidirectional text flows properly. In a React application, derive these values from the active locale to maintain consistency across the application.

Secondly, replace conventional directional CSS declarations like `margin-left` and `padding-right` with logical properties such as `padding-inline` and `margin-block`. Apply these properties to components to ensure they automatically adjust to the document's direction. This eliminates the need for explicit right-to-left adjustments and prevents common bugs associated with manual directional changes.

Instead of using `margin-left` or `padding-right`, opt for `margin-inline-start` or `padding-inline-end`, which describe the purpose of the spacing rather than a physical side.

Thirdly, refrain from mirroring every visual element. While the layout should change direction, not all components should flip. Elements such as logos, photos, media controls, phone numbers, code snippets, and many charts should retain their original orientation. For directional icons, apply transformations only to those that convey movement. For example, use `transform: scaleX(-1);` on icons that indicate a directional change. Avoid reversing the entire container as this may distort images and text rendering unexpectedly.

Fourthly, treat translation content separately from the components. Instead of embedding language-specific copy within components, pass translations as props. For instance, define a `messages` object with locale-specific translations for both English and Arabic. This separation allows for easier management and validation of translations during the build process, preventing silent failures when a translation key is missing.

Fifthly, design components to accommodate text expansion, which is a significant concern in Arabic due to its divergent character width from English. Navigation labels, buttons, cards, and validation messages should have flexible widths to accommodate longer Arabic text. Avoid setting fixed widths for text controls, as this can lead to layout issues.

Instead, use `min-inline-size`, `inline-size: fit-content`, and `max-inline-size: 100%` to allow text to grow as needed. Rigorously test components with lengthy labels to ensure they maintain their integrity with complex content.

Sixthly, select fonts with careful consideration. A font that appears excellent in Latin characters may offer subpar Arabic glyphs. Choose a font family that explicitly supports Arabic or define a compatible pair. Define custom properties `--font-latin` and `--font-arabic` to specify different font families for English and Arabic content.

Subset font files, preload only the necessary weights, and use `font-display: swap` to enhance performance, as loading multiple weights for two writing systems can significantly impact page load times.

Seventhly, ensure that each language has a distinct, indexable URL. For example, use `/services/web-design/` for English and `/ar/services/web-design/` for Arabic, or `/en/services/web-design/` and `/ar/services/web-design/`. Avoid changing the language through JavaScript while keeping the URL unchanged, as this complicates sharing, analytics, caching, and search indexing.

Connect equivalent pages with `hreflang` attributes to facilitate search engine understanding and user navigation. Each page should also include a self-referencing canonical URL, confirming that the Arabic version is an alternate language page, not a duplicate.

Finally, test mixed-direction content thoroughly. Arabic interfaces often incorporate English elements like product names, email addresses, URLs, and numbers. Wrap these isolated fragments with `bdi` tags to prevent the browser from incorrectly inferring the text direction. For instance, wrap `hello@example.com` within `bdi` tags to ensure it renders correctly regardless of the surrounding text direction.

Test scenarios that include Arabic text with English brand names and phone numbers to verify that the site behaves correctly across all languages.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Friday 28 August →