1 | import * as rtlDetect from 'rtl-detect';
|
2 |
|
3 | import { Localization } from './Localization.types';
|
4 |
|
5 | export default {
|
6 | get isRTL(): boolean {
|
7 | return rtlDetect.isRtlLang(this.locale);
|
8 | },
|
9 | get locale(): string {
|
10 | const locale =
|
11 | navigator.language ||
|
12 | navigator['systemLanguage'] ||
|
13 | navigator['browserLanguage'] ||
|
14 | navigator['userLanguage'] ||
|
15 | this.locales[0];
|
16 | return locale;
|
17 | },
|
18 | get locales(): string[] {
|
19 | const { languages = [] } = navigator;
|
20 | return Array.from(languages);
|
21 | },
|
22 | get timezone(): string {
|
23 | const defaultTimeZone = 'Etc/UTC';
|
24 | if (typeof Intl === 'undefined') {
|
25 | return defaultTimeZone;
|
26 | }
|
27 | return Intl.DateTimeFormat().resolvedOptions().timeZone || defaultTimeZone;
|
28 | },
|
29 | get isoCurrencyCodes(): string[] {
|
30 |
|
31 | return [];
|
32 | },
|
33 | get country(): string | undefined {
|
34 | const { locale } = this;
|
35 | if (typeof locale === 'string' && locale.length) {
|
36 | const isoCountryCode = locale.substring(locale.lastIndexOf('-') + 1);
|
37 | return isoCountryCode.toUpperCase();
|
38 | }
|
39 | return undefined;
|
40 | },
|
41 | async getLocalizationAsync(): Promise<Localization> {
|
42 | const { country, isoCurrencyCodes, timezone, locales, locale, isRTL } = this;
|
43 | return {
|
44 | country,
|
45 | isoCurrencyCodes,
|
46 | timezone,
|
47 | locales,
|
48 | locale,
|
49 | isRTL,
|
50 | };
|
51 | },
|
52 | };
|