UNPKG

1.45 kBPlain TextView Raw
1import * as rtlDetect from 'rtl-detect';
2
3import { Localization } from './Localization.types';
4
5export 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
17 // The native format is en-US
18 return locale.replace('_', '-');
19 },
20 get locales(): string[] {
21 const { languages = [] } = navigator;
22 return Array.from(languages);
23 },
24 get timezone(): string {
25 const defaultTimeZone = 'Etc/UTC';
26 if (typeof Intl === 'undefined') {
27 return defaultTimeZone;
28 }
29 return Intl.DateTimeFormat().resolvedOptions().timeZone || defaultTimeZone;
30 },
31 get isoCurrencyCodes(): string[] {
32 // TODO: Bacon: Add this - very low priority
33 return [];
34 },
35 get country(): string | undefined {
36 const { locale } = this;
37 if (typeof locale === 'string' && locale.length) {
38 const isoCountryCode = locale.substring(locale.lastIndexOf('-') + 1);
39 return isoCountryCode.toUpperCase();
40 }
41 return undefined;
42 },
43 async getLocalizationAsync(): Promise<Localization> {
44 const { country, isoCurrencyCodes, timezone, locales, locale, isRTL } = this;
45 return {
46 country,
47 isoCurrencyCodes,
48 timezone,
49 locales,
50 locale,
51 isRTL,
52 };
53 },
54};