Repository URL to install this package:
|
Version:
5.0.0-rc.5 ▾
|
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.importMessages = importMessages;
exports.importLocaleData = importLocaleData;
exports.importIntl = importIntl;
var _interopRequireWildcard2 = _interopRequireDefault(require("@babel/runtime/helpers/interopRequireWildcard"));
var _utils = require("./utils");
/**
* Dynamic import of a translation file, typically a JSON file
* You have to define an alias (`resolve.alias`) in your webpack configuration
* i.e.
* {
* resolve: {
* alias: {
* i18n: path.resolve('./i18n')
* }
* },
* //...
* },
* @param {String} locale a locale, i.e. `en`
* @returns {Object} imported module, default export in `default` field
*/
async function importMessages(locale) {
try {
const normalizedLocale = (0, _utils.normalizeLocale)(locale);
const importedModule = await Promise.resolve(`i18n/${normalizedLocale}`).then(s => (0, _interopRequireWildcard2.default)(require(s)));
return importedModule.default;
} catch (error) {
// if the requested locale is not present, return an empty object
return {};
}
}
/**
* Dynamic import of `react-intl`'s locale data
*
* @see https://github.com/yahoo/react-intl/wiki#locale-data-in-browsers
* @param {String} locale a locale, i.e. `en`
* @returns {import('react-intl').LocaleData} locale data object
*/
async function importLocaleData(locale) {
const normalizedLocale = (0, _utils.normalizeLocale)(locale);
const [language] = normalizedLocale.split('-');
const importedModule = await Promise.resolve(`react-intl/locale-data/${language}`).then(s => (0, _interopRequireWildcard2.default)(require(s))); // react-intl's locale data provides additional specific locales, i.e. `en-GB`: we'll pick the base locale data
const [specificLocaleData] = importedModule.default.filter(localeData => localeData.locale === normalizedLocale);
const defaultLocaleData = importedModule.default.filter(localeData => localeData.locale === language);
return specificLocaleData || defaultLocaleData;
}
/**
* Dynamic import of the `Intl` polyfill, but only if there is no support
*/
async function importIntl() {
if (!global.Intl) {
await Promise.resolve().then(() => (0, _interopRequireWildcard2.default)(require('intl')));
}
}
//# sourceMappingURL=import.js.map