Skip to content

Commit 510aa5a

Browse files
ahmedverona34-botclaudemaria-rcks
authored
fix(web): Arabic paragraphs with many English terms now read right to left (pingdotgg#26095)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 5b5bd16 commit 510aa5a

2 files changed

Lines changed: 56 additions & 14 deletions

File tree

‎apps/web/src/textDirection.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,34 @@ describe("textDirection", () => {
1313
"rtl",
1414
],
1515
["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+
],
1621
["a tie, by its first word", "React مكتبة", "ltr"],
1722
["a tie that opens in Arabic", "مكتبة React", "rtl"],
1823
["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+
],
1944
])("reads %s", (_, text, direction) => {
2045
expect(textDirection(text)).toBe(direction);
2146
});

‎packages/shared/src/textDirection.ts‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,43 @@ export type TextDirection = "ltr" | "rtl";
44
// supplementary right-to-left blocks.
55
const RTL_LETTER = /[֐-ࣿיִ-﷿ﹰ-\u{10800}-\u{10FFF}\u{1E800}-\u{1EFFF}]/u;
66
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+
}
731

832
/**
933
* `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.
1137
*/
1238
export function textDirection(text: string): TextDirection | undefined {
1339
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+
);
2744
}
2845

2946
const MARKDOWN_CODE_OR_URL =

0 commit comments

Comments
 (0)