Skip to content

Programming Code Alignment in Arabic (RTL) Locale #141

Description

@YasserYG8

Issue Analysis: Programming Code Alignment in Arabic (RTL) Locale

This document provides a detailed analysis of the code rendering bug when switching the website locale to Arabic, and proposes clean solutions to resolve it.


🔍 Issue Description

When a user switches the site language to Arabic, the programming code entered or displayed in the workspace gets visual alignment and syntax structure corruption.

Symptom:

  • The code alignment shifts to the right margin.
  • Punctuation marks (like colons :, parentheses (), brackets {}) flip or render in the wrong positions due to the browser's Unicode Bidirectional algorithm.
  • Indentation (spaces/tabs) behaves unexpectedly.

🕵️ Root Cause Analysis

1. The dir="rtl" Inheritance

In Next.js, the root layout (layout.tsx) dynamically injects the text direction of the page:

<html
  lang={lang as Locale}
  dir={lang === "ar" ? "rtl" : "ltr"}
  ...
>

When lang === "ar", the browser applies direction: rtl and text-align: right globally. By default, all child HTML elements inherit these styles.

2. Unicode Bidirectional Algorithm (Bidi)

Browsers render text using the Bidi algorithm to handle mixed LTR (Left-to-Right) and RTL (Right-to-Left) text. While this is necessary for Arabic characters, it corrupts programming languages (like Python, JS, Go, Rust) because:

  • Programming code is strictly Left-to-Right.
  • Symbols like ( and ) are categorized by Unicode as "mirrored characters" that flip direction based on the text context.
  • When nested inside an RTL block, the browser renders the parenthesis backwards or moves the colon to the wrong side of the line.

💡 Proposed Solutions

Solution 1: CSS Override (Recommended, Global & Low Touch)

The most robust solution is to target all code blocks, textareas, and code-editors in the CSS stylesheet globals.css and force them to remain LTR:

/* Force code-related blocks to stay Left-to-Right in all locales */
pre, 
code, 
textarea,
.code-editor,
[data-code-block] {
  direction: ltr !important;
  unicode-bidi: plaintext !important;
  text-align: left !important;
}

Why it works:

  • direction: ltr !important: Forces layout flow from Left-to-Right.
  • unicode-bidi: plaintext !important: Prevents the Bidi algorithm from trying to re-order the characters inside the editor or code blocks.
  • text-align: left !important: Moves the alignment back to the left margin.

Solution 2: HTML Attribute Override (Component Level)

Alternatively, you can apply the dir="ltr" attribute directly to the textareas and preview blocks inside the UI components:

A. In the Code Workspace Input:

Add the dir="ltr" attribute to the textarea inside CodeWorkspace.tsx:

<textarea
  dir="ltr"
  className="w-full font-mono text-sm ..."
  placeholder={inputPlaceholder}
  // ...
/>

B. In Code Highlight / Docs Preview:

Ensure any code viewer or syntax highlighter block uses:

<pre dir="ltr">
  <code>...</code>
</pre>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions