Skip to content

Commit 7d446e8

Browse files
committed
types-grammar, ch2: fixing discussion of RTL languages and logical character positions
1 parent f383274 commit 7d446e8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

types-grammar/ch2.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,23 @@ hebrewHello = "\u{5e9}\u{5dc}\u{5d5}\u{5dd}";
275275
console.log(hebrewHello); // שלום
276276
```
277277

278+
Notice that the first listed character in the string literal (`"\u{5e9}"`) is actually the right-most character when the string is rendered?
279+
280+
Even though Hebrew is an RTL language, you don't actually type the characters in the string literal in reversed (RTL) order the way they should be rendered. You enter the characters in logical order, where position `0` is the first character, position `1` is the second character, etc. The rendering layer is where RTL characters are reversed to be shown in their correct order.
281+
282+
That also means that if you access `hebrewHello[0]` (or `hebrewHello.charAt(0)`) -- to get the character as position `0` -- you get `"ש"` because that's logically the first character of the string, not `"ם"` (logically the last character of the string). Index-positional access follows the logical position, not the rendered position.
283+
284+
Here's the same example in another RTL language, Arabic:
285+
278286
```js
279287
arabicHello = "\u{631}\u{62d}\u{628}\u{627}";
280288

281289
console.log(arabicHello); // رحبا
282-
```
283290

284-
If you access `hebrewHello[0]` -- to get the character as position `0` -- you might expect the letter `"ם"`, but instead you get `"ש"` because Hebrew is RTL. So in other words, JS applies index positioning based on the LTR/RTL of the locale (including as embedded in the string contents themselves).
291+
console.log(arabicHello[0]); // ر
292+
```
285293

286-
JS programs can force the in-effect language/locale, using various `Intl` APIs such as `Intl.Collator`: [^INTLCollator]
294+
JS programs can force a specific language/locale, using various `Intl` APIs such as `Intl.Collator`: [^INTLCollator]
287295

288296
```js
289297
germanStringSorter = new Intl.Collator("de");
@@ -301,7 +309,6 @@ germanStringSorter.compare("Z","z");
301309
caseFirstSorter = new Intl.Collator("de",{ caseFirst: "upper", });
302310
caseFirstSorter.compare("Z","z");
303311
// -1 (or negative number)
304-
305312
```
306313

307314
Multiple-word strings can be segmented using `Intl.Segmenter`: [^INTLSegmenter]

0 commit comments

Comments
 (0)