intl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // import { shouldPolyfill as shouldPolyfillCanonicalLocales } from '@formatjs/intl-getcanonicallocales/should-polyfill';
  2. // import { shouldPolyfill as shouldPolyfillLocale } from '@formatjs/intl-locale/should-polyfill';
  3. import { shouldPolyfill as shoudPolyfillPluralRules } from '@formatjs/intl-pluralrules/should-polyfill';
  4. // import { shouldPolyfill as shouldPolyfillNumberFormat } from '@formatjs/intl-numberformat/should-polyfill';
  5. // import { shouldPolyfill as shouldPolyfillIntlDateTimeFormat } from '@formatjs/intl-datetimeformat/should-polyfill';
  6. // import { shouldPolyfill as shouldPolyfillIntlRelativeTimeFormat } from '@formatjs/intl-relativetimeformat/should-polyfill';
  7. // async function loadGetCanonicalLocalesPolyfill() {
  8. // // This platform already supports Intl.getCanonicalLocales
  9. // if (shouldPolyfillCanonicalLocales()) {
  10. // await import('@formatjs/intl-getcanonicallocales/polyfill');
  11. // }
  12. // }
  13. // async function loadLocalePolyfill() {
  14. // // This platform already supports Intl.Locale
  15. // if (shouldPolyfillLocale()) {
  16. // await import('@formatjs/intl-locale/polyfill');
  17. // }
  18. // }
  19. // async function loadIntlNumberFormatPolyfill(locale: string) {
  20. // const unsupportedLocale = shouldPolyfillNumberFormat(locale);
  21. // // This locale is supported
  22. // if (!unsupportedLocale) {
  23. // return;
  24. // }
  25. // // Load the polyfill 1st BEFORE loading data
  26. // await import('@formatjs/intl-numberformat/polyfill-force');
  27. // await import(`@formatjs/intl-numberformat/locale-data/${unsupportedLocale}`);
  28. // }
  29. // async function loadIntlDateTimeFormatPolyfill(locale: string) {
  30. // const unsupportedLocale = shouldPolyfillIntlDateTimeFormat(locale);
  31. // // This locale is supported
  32. // if (!unsupportedLocale) {
  33. // return;
  34. // }
  35. // // Load the polyfill 1st BEFORE loading data
  36. // await import('@formatjs/intl-datetimeformat/polyfill-force');
  37. // // Parallelize CLDR data loading
  38. // const dataPolyfills = [
  39. // import('@formatjs/intl-datetimeformat/add-all-tz'),
  40. // import(`@formatjs/intl-datetimeformat/locale-data/${unsupportedLocale}`),
  41. // ];
  42. // await Promise.all(dataPolyfills);
  43. // }
  44. async function loadIntlPluralRulesPolyfills(locale: string) {
  45. const unsupportedLocale = shoudPolyfillPluralRules(locale);
  46. // This locale is supported
  47. if (!unsupportedLocale) {
  48. return;
  49. }
  50. // Load the polyfill 1st BEFORE loading data
  51. await import(
  52. /* webpackChunkName: "i18n-pluralrules-polyfill" */ '@formatjs/intl-pluralrules/polyfill-force'
  53. );
  54. await import(
  55. /* webpackChunkName: "i18n-pluralrules-polyfill-[request]" */ `@formatjs/intl-pluralrules/locale-data/${unsupportedLocale}`
  56. );
  57. }
  58. // async function loadIntlRelativeTimeFormatPolyfill(locale: string) {
  59. // const unsupportedLocale = shouldPolyfillIntlRelativeTimeFormat(locale);
  60. // // This locale is supported
  61. // if (!unsupportedLocale) {
  62. // return;
  63. // }
  64. // // Load the polyfill 1st BEFORE loading data
  65. // await import(
  66. // /* webpackChunkName: "i18n-relativetimeformat-polyfill" */
  67. // '@formatjs/intl-relativetimeformat/polyfill-force'
  68. // );
  69. // await import(
  70. // /* webpackChunkName: "i18n-relativetimeformat-polyfill-[request]" */
  71. // `@formatjs/intl-relativetimeformat/locale-data/${unsupportedLocale}`
  72. // );
  73. // }
  74. export async function loadIntlPolyfills() {
  75. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- we want to match empty strings
  76. const locale = document.querySelector('html')?.lang || 'en';
  77. // order is important here
  78. // Supported in IE11 and most other browsers, not useful
  79. // await loadGetCanonicalLocalesPolyfill()
  80. // Supported in IE11 and most other browsers, not useful
  81. // await loadLocalePolyfill()
  82. // Supported in IE11 and most other browsers, not useful
  83. // await loadIntlNumberFormatPolyfill(locale)
  84. // Supported in IE11 and most other browsers, not useful
  85. // await loadIntlDateTimeFormatPolyfill(locale)
  86. // Supported from Safari 13+, may still be useful
  87. await loadIntlPluralRulesPolyfills(locale);
  88. // This is not used yet in the codebase yet
  89. // Supported from Safari 14+
  90. // await loadIntlRelativeTimeFormatPolyfill(locale);
  91. }