The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 08:15 UTC
fix(web): Arabic paragraphs with many English terms now read right to left (#26095)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>
Ahmed Mostafa committed
Nov 1, 2026 at 22:00 UTC
510aa5af40dc74fa60f918d5ef1aeb5d33665ba6
2 files changed
+56
-14
apps/web/src/textDirection.test.ts
+25
@@ -13,9 +13,34 @@ describe("textDirection", () => {
13
"rtl",
14
],
15
["English quoting one Arabic word", "translate the label مرحبا for the login screen", "ltr"],
16
+ [
17
+ "tied sentences use the whole-text word majority",
18
+ "Done. تم إصلاح المشكلة في الملف الرئيسي",
19
+ "rtl",
20
+ ],
21
["a tie, by its first word", "React مكتبة", "ltr"],
22
["a tie that opens in Arabic", "مكتبة React", "rtl"],
23
["Arabic around numbers and punctuation", "عدّل السطر 42، ثم شغّل الاختبارات (3)", "rtl"],
24
+ [
25
+ "mostly Arabic sentences beside one long English one",
26
+ "هذا مثال على paragraph يحتوي على نص عربي و English words معًا. Arabic text should flow right-to-left while English words like JavaScript, React, and API should remain left-to-right within the same line. الأرقام 1234567890 ورموز مثل @username و #hashtag و 100$ تظهر في الاتجاه الصحيح. هذه جملة لاختبار RTL rendering في المتصفح أو المحرر.",
27
+ "rtl",
28
+ ],
29
+ [
30
+ "mostly English sentences beside one Arabic one",
31
+ "Run the migration first. ثم شغّل الاختبارات كلها. Then deploy to staging.",
32
+ "ltr",
33
+ ],
34
+ [
35
+ "Arabic with inline abbreviations",
36
+ "هذه جملة عربية تتحدث عن e.g. React i.e. TypeScript JavaScript",
37
+ "rtl",
38
+ ],
39
+ [
40
+ "an abbreviation at a sentence boundary",
41
+ "This long English sentence describes the technology sector in the U.S. هذه جملة عربية. وهذه جملة عربية أخرى.",
42
+ "rtl",
43
+ ],
44
])("reads %s", (_, text, direction) => {
45
expect(textDirection(text)).toBe(direction);
46
});
packages/shared/src/textDirection.ts
+31
-14
@@ -4,26 +4,43 @@ export type TextDirection = "ltr" | "rtl";
4
// supplementary right-to-left blocks.
5
const RTL_LETTER = /[-ࣿיִ-﷿ﹰ-\u{10800}-\u{10FFF}\u{1E800}-\u{1EFFF}]/u;
6
const LETTER = /\p{L}/u;
7
+const SENTENCE_END = /(?<=[.!?؟])(?<!\b(?:e\.g|i\.e)\.)\s+|\n+/i;
8
+
9
+function majority(
10
+ units: Iterable<TextDirection | undefined>,
11
+ tieDirection?: () => TextDirection | undefined,
12
+): TextDirection | undefined {
13
+ let rtl = 0;
14
+ let ltr = 0;
15
+ let first: TextDirection | undefined;
16
+ for (const direction of units) {
17
+ if (!direction) continue;
18
+ first ??= direction;
19
+ if (direction === "rtl") rtl += 1;
20
+ else ltr += 1;
21
+ }
22
+ if (rtl === ltr) return tieDirection ? tieDirection() : first;
23
+ return rtl > ltr ? "rtl" : "ltr";
24
+}
25
+
26
+function wordDirection(word: string): TextDirection | undefined {
27
+ const letter = LETTER.exec(word)?.[0];
28
+ if (!letter) return undefined;
29
+ return RTL_LETTER.test(letter) ? "rtl" : "ltr";
30
+}
31
32
/**
33
* `dir="auto"` misreads Arabic prose that opens with an English term.
10
- * Count words so mixed prose follows its main language; break ties with the first word.
34
+ * Each sentence follows its main language by word count, and the text follows most of its
35
+ * sentences, so one long English sentence cannot outvote the Arabic ones around it.
36
+ * Sentence ties use the whole-text word majority, then the first word.
37
*/
38
export function textDirection(text: string): TextDirection | undefined {
39
if (!RTL_LETTER.test(text)) return LETTER.test(text) ? "ltr" : undefined;
14
- let rtlWords = 0;
15
- let ltrWords = 0;
16
- let first: TextDirection | undefined;
17
- for (const word of text.split(/\s+/)) {
18
- const letter = LETTER.exec(word)?.[0];
19
- if (!letter) continue;
20
- const direction = RTL_LETTER.test(letter) ? "rtl" : "ltr";
21
- first ??= direction;
22
- if (direction === "rtl") rtlWords += 1;
23
- else ltrWords += 1;
24
- }
25
- if (rtlWords === ltrWords) return first;
26
- return rtlWords > ltrWords ? "rtl" : "ltr";
40
+ return majority(
41
+ text.split(SENTENCE_END).map((sentence) => majority(sentence.split(/\s+/).map(wordDirection))),
42
+ () => majority(text.split(/\s+/).map(wordDirection)),
43
+ );
44
}
45
46
const MARKDOWN_CODE_OR_URL =