~cytrogen/masto-fe

ref: f8dc013b0d81d5d463c086e291ce99ab1d796a9d masto-fe/app/javascript/flavours/glitch/polyfills/intl.ts -rw-r--r-- 4.0 KiB
f8dc013b — Cytrogen [feature] Add language selector to local settings 7 days ago
                                                                                
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
102
103
104
105
106
// import { shouldPolyfill as shouldPolyfillCanonicalLocales } from '@formatjs/intl-getcanonicallocales/should-polyfill';
// import { shouldPolyfill as shouldPolyfillLocale } from '@formatjs/intl-locale/should-polyfill';
import { shouldPolyfill as shoudPolyfillPluralRules } from "@formatjs/intl-pluralrules/should-polyfill";
// import { shouldPolyfill as shouldPolyfillNumberFormat } from '@formatjs/intl-numberformat/should-polyfill';
// import { shouldPolyfill as shouldPolyfillIntlDateTimeFormat } from '@formatjs/intl-datetimeformat/should-polyfill';
// import { shouldPolyfill as shouldPolyfillIntlRelativeTimeFormat } from '@formatjs/intl-relativetimeformat/should-polyfill';

// async function loadGetCanonicalLocalesPolyfill() {
//   // This platform already supports Intl.getCanonicalLocales
//   if (shouldPolyfillCanonicalLocales()) {
//     await import('@formatjs/intl-getcanonicallocales/polyfill');
//   }
// }

// async function loadLocalePolyfill() {
//   // This platform already supports Intl.Locale
//   if (shouldPolyfillLocale()) {
//     await import('@formatjs/intl-locale/polyfill');
//   }
// }

// async function loadIntlNumberFormatPolyfill(locale: string) {
//   const unsupportedLocale = shouldPolyfillNumberFormat(locale);
//   // This locale is supported
//   if (!unsupportedLocale) {
//     return;
//   }
//   // Load the polyfill 1st BEFORE loading data
//   await import('@formatjs/intl-numberformat/polyfill-force');
//   await import(`@formatjs/intl-numberformat/locale-data/${unsupportedLocale}`);
// }

// async function loadIntlDateTimeFormatPolyfill(locale: string) {
//   const unsupportedLocale = shouldPolyfillIntlDateTimeFormat(locale);
//   // This locale is supported
//   if (!unsupportedLocale) {
//     return;
//   }
//   // Load the polyfill 1st BEFORE loading data
//   await import('@formatjs/intl-datetimeformat/polyfill-force');

//   // Parallelize CLDR data loading
//   const dataPolyfills = [
//     import('@formatjs/intl-datetimeformat/add-all-tz'),
//     import(`@formatjs/intl-datetimeformat/locale-data/${unsupportedLocale}`),
//   ];
//   await Promise.all(dataPolyfills);
// }

async function loadIntlPluralRulesPolyfills(locale: string) {
  const unsupportedLocale = shoudPolyfillPluralRules(locale);
  // This locale is supported
  if (!unsupportedLocale) {
    return;
  }
  // Load the polyfill 1st BEFORE loading data
  await import(
    /* webpackChunkName: "i18n-pluralrules-polyfill" */ "@formatjs/intl-pluralrules/polyfill-force",
  );
  await import(
    /* webpackChunkName: "i18n-pluralrules-polyfill-[request]" */ `@formatjs/intl-pluralrules/locale-data/${unsupportedLocale}`,
  );
}

// async function loadIntlRelativeTimeFormatPolyfill(locale: string) {
//   const unsupportedLocale = shouldPolyfillIntlRelativeTimeFormat(locale);
//   // This locale is supported
//   if (!unsupportedLocale) {
//     return;
//   }
//   // Load the polyfill 1st BEFORE loading data
//   await import(
//     /* webpackChunkName: "i18n-relativetimeformat-polyfill" */
//     '@formatjs/intl-relativetimeformat/polyfill-force'
//   );
//   await import(
//     /* webpackChunkName: "i18n-relativetimeformat-polyfill-[request]" */
//     `@formatjs/intl-relativetimeformat/locale-data/${unsupportedLocale}`
//   );
// }

export async function loadIntlPolyfills() {
   
  const locale = document.querySelector("html")?.lang || "en";

  // order is important here

  // Supported in IE11 and most other browsers, not useful
  // await loadGetCanonicalLocalesPolyfill()

  // Supported in IE11 and most other browsers, not useful
  // await loadLocalePolyfill()

  // Supported in IE11 and most other browsers, not useful
  // await loadIntlNumberFormatPolyfill(locale)

  // Supported in IE11 and most other browsers, not useful
  // await loadIntlDateTimeFormatPolyfill(locale)

  // Supported from Safari 13+, may still be useful
  await loadIntlPluralRulesPolyfills(locale);

  // This is not used yet in the codebase yet
  // Supported from Safari 14+
  // await loadIntlRelativeTimeFormatPolyfill(locale);
}