(localeString: string)
| 50 | * [Intl.Locale]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale}. |
| 51 | */ |
| 52 | export function isRTL(localeString: string): boolean { |
| 53 | // If the Intl.Locale API is available, use it to get the locale's text direction. |
| 54 | if (Intl.Locale) { |
| 55 | let locale = new Intl.Locale(localeString).maximize(); |
| 56 | |
| 57 | // Use the text info object to get the direction if possible. |
| 58 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTextInfo |
| 59 | let textInfo = |
| 60 | // @ts-ignore - this was implemented as a property by some browsers before it was standardized as a function. |
| 61 | typeof locale.getTextInfo === 'function' ? locale.getTextInfo() : locale.textInfo; |
| 62 | if (textInfo) { |
| 63 | return textInfo.direction === 'rtl'; |
| 64 | } |
| 65 | |
| 66 | // Fallback: guess using the script. |
| 67 | // This is more accurate than guessing by language, since languages can be written in multiple scripts. |
| 68 | if (locale.script) { |
| 69 | return RTL_SCRIPTS.has(locale.script); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // If not, just guess by the language (first part of the locale) |
| 74 | let lang = localeString.split('-')[0]; |
| 75 | return RTL_LANGS.has(lang); |
| 76 | } |
no test coverage detected