-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathgetLocaleDirection.ts
More file actions
101 lines (89 loc) · 2.39 KB
/
getLocaleDirection.ts
File metadata and controls
101 lines (89 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { intlCache } from '../cache/IntlCache';
import _getLocaleProperties from './getLocaleProperties';
/**
* Get the text direction for a given locale code using the Intl.Locale API.
*
* @param {string} code - The locale code to check.
* @returns {string} - 'rtl' if the language is right-to-left, otherwise 'ltr'.
* @internal
*/
export function _getLocaleDirection(code: string): 'ltr' | 'rtl' {
// Extract via textInfo property
try {
const locale = intlCache.get('Locale', code);
const textInfoDirection = extractDirectionWithTextInfo(locale);
if (textInfoDirection) {
return textInfoDirection;
}
} catch {
// silent
}
// Fallback to simple heuristics
const { scriptCode, languageCode } = _getLocaleProperties(code);
// Handle RTL script or language
if (scriptCode) return isRtlScript(scriptCode) ? 'rtl' : 'ltr';
if (languageCode) return isRtlLanguage(languageCode) ? 'rtl' : 'ltr';
return 'ltr';
}
// ===== HELPER CONSTANTS ===== //
const RTL_SCRIPTS = new Set([
'arab',
'adlm',
'hebr',
'nkoo',
'rohg',
'samr',
'syrc',
'thaa',
'yezi',
]);
const RTL_LANGUAGES = new Set([
'ar',
'arc',
'ckb',
'dv',
'fa',
'he',
'iw',
'ku',
'lrc',
'nqo',
'ps',
'pnb',
'sd',
'syr',
'ug',
'ur',
'yi',
]);
// ===== HELPER FUNCTIONS ===== //
/**
* Handles extracting direction via textInfo property
* @param Locale - Intl.Locale object
* @returns {'ltr' | 'rtl'} - The direction of the locale
*
* Intl.Locale.prototype.getTextInfo() / textInfo property incorporated in ES2024 Specification.
* This is not supported by all browsers yet.
* See: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTextInfo#browser_compatibility}
*/
function extractDirectionWithTextInfo(
locale: Intl.Locale
): 'ltr' | 'rtl' | undefined {
if (
'textInfo' in locale &&
typeof locale.textInfo === 'object' &&
locale.textInfo !== null &&
'direction' in locale.textInfo &&
(locale.textInfo?.direction === 'rtl' ||
locale.textInfo?.direction === 'ltr')
) {
return locale.textInfo?.direction;
}
return undefined;
}
function isRtlScript(script: string | undefined): boolean {
return script ? RTL_SCRIPTS.has(script.toLowerCase()) : false;
}
function isRtlLanguage(language: string | undefined): boolean {
return language ? RTL_LANGUAGES.has(language.toLowerCase()) : false;
}