2019-12-08 00:32:00 +05:30
|
|
|
import logger from 'app/services/logger';
|
2019-11-11 14:10:05 +05:30
|
|
|
|
|
|
|
const needs = {
|
2019-11-27 14:33:32 +05:30
|
|
|
intl: !window.Intl,
|
|
|
|
plural: !window?.Intl?.PluralRules,
|
2019-12-07 16:58:52 +05:30
|
|
|
// @ts-ignore ts has a little bit outdated definitions
|
2019-11-27 14:33:32 +05:30
|
|
|
relative: !window?.Intl?.RelativeTimeFormat,
|
2019-11-11 14:10:05 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All modern browsers currently do support all required Intl APIs,
|
|
|
|
* for the outdated browsers we will polyfill this api on demand
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function polyfill(locale: string): Promise<void> {
|
2019-12-07 16:58:52 +05:30
|
|
|
const promises: Promise<void>[] = [];
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
if (
|
|
|
|
!needs.intl &&
|
|
|
|
Intl.DateTimeFormat.supportedLocalesOf([locale]).length === 0
|
|
|
|
) {
|
|
|
|
// fallback to polyfill in case, when browser does not support locale we need
|
|
|
|
needs.intl = true;
|
|
|
|
needs.plural = true;
|
|
|
|
needs.relative = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needs.intl) {
|
|
|
|
promises.push(polyfillIntl(locale));
|
|
|
|
} else {
|
|
|
|
if (needs.plural) {
|
|
|
|
promises.push(polyfillPlural(locale));
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (needs.relative) {
|
|
|
|
promises.push(polyfillRelative(locale));
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all(promises);
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn('Error loading intl polyfills', {
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async function polyfillIntl(locale: string): Promise<void> {
|
2019-11-27 14:33:32 +05:30
|
|
|
// do not rely on tests provided by polyfill implementation
|
|
|
|
// forcibly apply polyfill, because if this function was called
|
|
|
|
// this means that browser does not support some of locales
|
|
|
|
const { default: Intl } = await import(
|
|
|
|
/* webpackChunkName: "intl" */
|
|
|
|
'intl'
|
|
|
|
);
|
|
|
|
|
|
|
|
window.Intl = Intl;
|
|
|
|
|
|
|
|
// MUST be loaded in series with the main polyfill
|
|
|
|
await Promise.all([
|
|
|
|
import(
|
|
|
|
// WARNING: .js extension is required for proper function of ContextReplacementPlugin
|
|
|
|
/* webpackChunkName: "intl-[request]" */
|
|
|
|
`intl/locale-data/jsonp/${locale}.js`
|
|
|
|
),
|
|
|
|
polyfillPlural(locale),
|
|
|
|
polyfillRelative(locale),
|
|
|
|
]);
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async function polyfillPlural(locale: string): Promise<void> {
|
2019-11-27 14:33:32 +05:30
|
|
|
await import(
|
|
|
|
/* webpackChunkName: "intl-pluralrules" */
|
|
|
|
'@formatjs/intl-pluralrules/polyfill'
|
|
|
|
);
|
|
|
|
|
|
|
|
// MUST be loaded in series with the main polyfill
|
|
|
|
await import(
|
|
|
|
// WARNING: .js extension is required for proper function of ContextReplacementPlugin
|
|
|
|
/* webpackChunkName: "intl-pluralrules-[request]" */
|
|
|
|
`@formatjs/intl-pluralrules/dist/locale-data/${locale}.js`
|
|
|
|
);
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async function polyfillRelative(locale: string): Promise<void> {
|
2019-11-27 14:33:32 +05:30
|
|
|
await import(
|
|
|
|
/* webpackChunkName: "intl-relativetimeformat" */
|
|
|
|
'@formatjs/intl-relativetimeformat/polyfill'
|
|
|
|
);
|
|
|
|
|
|
|
|
// MUST be loaded in series with the main polyfill
|
|
|
|
await import(
|
|
|
|
// WARNING: .js extension is required for proper function of ContextReplacementPlugin
|
|
|
|
/* webpackChunkName: "intl-relativetimeformat-[request]" */
|
|
|
|
`@formatjs/intl-relativetimeformat/dist/locale-data/${locale}.js`
|
|
|
|
);
|
2019-11-11 14:10:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export default polyfill;
|