UNPKG

terra-i18n

Version:

The terra-i18n package provides on-demand internationalization of React components.

77 lines (76 loc) 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadIntl = exports.default = void 0; var _intlLoaders = _interopRequireDefault(require("intlLoaders")); var _intlLocalesSupported = _interopRequireDefault(require("intl-locales-supported")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } /* eslint-disable import/no-unresolved, compat/compat, no-console */ var supportedIntlConstructors = function supportedIntlConstructors() { /** * Use try-catch to check if Intl is provided by the browser. In some instances checking Intl will throw an * error and crash the page with little information. * * Reference: https://github.com/cerner/terra-core/issues/2820 */ var constructors; try { if ((typeof Intl === "undefined" ? "undefined" : _typeof(Intl)) === 'object' && typeof Intl.DateTimeFormat === 'function' && typeof Intl.NumberFormat === 'function') { /** * intl-locales-supported accesses the 'supportedLocalesOf' property of each of these constructors. * When certain polyfills are used, the polyfill may not have the 'supportedLocalesOf' property. * For example, when using the date-time-format-timezone Intl.DateTimeFormat becomes Intl.DateTimeFormatPolyfill which does not support this property. * * Reference: https://github.com/cerner/terra-core/issues/2914 */ if (Object.prototype.hasOwnProperty.call(Intl.DateTimeFormat, 'supportedLocalesOf') && Object.prototype.hasOwnProperty.call(Intl.NumberFormat, 'supportedLocalesOf')) { constructors = [Intl.DateTimeFormat, Intl.NumberFormat]; } else { constructors = []; } } } catch (error) { constructors = []; } return constructors; }; var loadFallbackIntl = function loadFallbackIntl(localeContext) { try { if (!(0, _intlLocalesSupported.default)(['en'], supportedIntlConstructors())) { _intlLoaders.default.en(); } if (process.env.NODE_ENV !== 'production') { console.warn("Locale data was not supplied for the ".concat(localeContext, ". Using en data as the fallback locale data.")); } } catch (e) { throw new Error("Locale data was not supplied for the ".concat(localeContext, ", or the en fallback locale.")); } }; var loadIntl = exports.loadIntl = function loadIntl(locale) { var fallbackLocale = locale.split('-').length > 1 ? locale.split('-')[0] : false; try { if (!(0, _intlLocalesSupported.default)([locale], supportedIntlConstructors())) { _intlLoaders.default[locale](); } } catch (e) { if (fallbackLocale) { try { if (!(0, _intlLocalesSupported.default)([fallbackLocale], supportedIntlConstructors())) { _intlLoaders.default[fallbackLocale](); } if (process.env.NODE_ENV !== 'production') { console.warn("Locale data was not supplied for the ".concat(locale, " locale. Using ").concat(fallbackLocale, " data as the fallback locale data.")); } } catch (error) { var localeContext = "".concat(locale, " or ").concat(fallbackLocale, " locales"); loadFallbackIntl(localeContext); } } else { var _localeContext = "".concat(locale, " locale"); loadFallbackIntl(_localeContext); } } }; var _default = exports.default = loadIntl; /* eslint-enable import/no-unresolved, compat/compat, no-console */