Source

polyfill/polyfillIntl.js

  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var isNullOrUndef_1 = __importDefault(require("../validators/isNullOrUndef"));
  7. /**
  8. * !! Caution:
  9. * This might cause a large bundle size.
  10. * The intl.js core itself is large (3xx KB).
  11. * Use only when only few languages are supported.
  12. *
  13. * Polyfill for:
  14. * Intl.NumberFormat,
  15. * Intl.DateTimeFormat,
  16. * Intl.RelativeTimeFormat
  17. *
  18. * Require packages:
  19. * intl
  20. * relative-time-format
  21. * @category polyfill
  22. * @module polyfillIntl
  23. */
  24. var polyfillIntl = function (intlDataGetters, relativeTimeFormatDataGetters) {
  25. // global here is window equivalent
  26. if ((0, isNullOrUndef_1.default)(global.Intl)) {
  27. // Add Intl.NumberFormat and Intl.DateTimeFormat polyfill
  28. var IntlPolyfill = require('intl');
  29. // Add Intl.RelativeTimeFormat polyfill
  30. var RelativeTimeFormat_1 = require('relative-time-format').default;
  31. // Import data for Intl.NumberFormat and Intl.DateTimeFormat
  32. intlDataGetters.forEach(function (getData) { return getData(); });
  33. // Import data for Intl.RelativeTimeFormat
  34. relativeTimeFormatDataGetters.forEach(function (getData) {
  35. var locale = getData();
  36. RelativeTimeFormat_1.addLocale(locale);
  37. });
  38. global.Intl = IntlPolyfill;
  39. // @ts-expect-error: This is a polyfill
  40. global.Intl.RelativeTimeFormat = RelativeTimeFormat_1;
  41. }
  42. else {
  43. // Safari NumberFormat does not have formatToParts so need polyfill too
  44. var isNumberFormatAvailable = global.Intl.NumberFormat
  45. && new global.Intl.NumberFormat().formatToParts;
  46. if (!isNumberFormatAvailable || !global.Intl.DateTimeFormat) {
  47. // Add Intl.NumberFormat and Intl.DateTimeFormat polyfill
  48. var IntlPolyfill = require('intl');
  49. // Import data for Intl.NumberFormat and Intl.DateTimeFormat
  50. intlDataGetters.forEach(function (getData) { return getData(); });
  51. if (!isNumberFormatAvailable)
  52. global.Intl.NumberFormat = IntlPolyfill.NumberFormat;
  53. if (!global.Intl.DateTimeFormat)
  54. global.Intl.NumberFormat = IntlPolyfill.DateTimeFormat;
  55. }
  56. // @ts-expect-error: This is validating
  57. if (!global.Intl.RelativeTimeFormat) {
  58. // Add Intl.RelativeTimeFormat polyfill
  59. var RelativeTimeFormat_2 = require('relative-time-format').default;
  60. // Import data for Intl.RelativeTimeFormat
  61. relativeTimeFormatDataGetters.forEach(function (getData) {
  62. var locale = getData();
  63. RelativeTimeFormat_2.addLocale(locale);
  64. });
  65. // @ts-expect-error: This is a polyfill
  66. global.Intl.RelativeTimeFormat = RelativeTimeFormat_2;
  67. }
  68. }
  69. };
  70. exports.default = polyfillIntl;