[WIP] preliminary intl polyfill

partial implementation of intl-polyfill. Doesn't include all the www langauges, and we don't want to have to update www each time there is a language added, so this needs to move into scratch-l10n somehow.

polyfill.min.js no longer needs the intl.js polyfill, so removed. Should still look at whether the other parts of polyfill.min are needed, but they're unrelated to intl.
This commit is contained in:
Chris Garrity 2021-08-05 17:56:32 -04:00
parent f5fa49fa45
commit cb968f3842
3 changed files with 54 additions and 745 deletions

View file

@ -1,12 +1,12 @@
const jar = require('./lib/jar'); const jar = require('./lib/jar');
import {intlPolyfill} from './lib/intl-polyfill'; import intlPolyfill from './lib/intl-polyfill';
/** /**
* ----------------------------------------------------------------------------- * -----------------------------------------------------------------------------
* L10N * L10N
* ----------------------------------------------------------------------------- * -----------------------------------------------------------------------------
*/ */
(() => { (async () => {
/* /*
* Bind locale code from cookie if available. Uses navigator language API as a fallback. * Bind locale code from cookie if available. Uses navigator language API as a fallback.
* *
@ -36,7 +36,7 @@ import {intlPolyfill} from './lib/intl-polyfill';
window._locale = updateLocale(); window._locale = updateLocale();
document.documentElement.lang = window._locale; document.documentElement.lang = window._locale;
intlPolyfill(window._locale); await intlPolyfill(window._locale);
})(); })();
/** /**

View file

@ -8,14 +8,24 @@
// safari <14 // safari <14
// The plural rules is required for safari 12. // The plural rules is required for safari 12.
import {shouldPolyfill as shouldPolyfillLocale} from '@formatjs/intl-locale/should-polyfill'; import {shouldPolyfill as shouldPolyfillLocale} from '@formatjs/intl-locale/should-polyfill';
import {shouldPolyfill as shouldPolyfillRelativeTimeFormat} from '@formatjs/intl-relativetimeformat/should-polyfill' import {shouldPolyfill as shouldPolyfillRelativeTimeFormat} from '@formatjs/intl-relativetimeformat/should-polyfill';
import {shouldPolyfill as shouldPolyfillPluralRules} from '@formatjs/intl-pluralrules/should-polyfill'; import {shouldPolyfill as shouldPolyfillPluralRules} from '@formatjs/intl-pluralrules/should-polyfill';
/**
* polyfill all the parts needed from intl
* @param {string} locale currently selected locale
* @return {Promise} returns a promise that resolves when everything is loaded
*/
const intlPolyfill = async function (locale) {
if (!(shouldPolyfillLocale() ||
shouldPolyfillPluralRules(locale) ||
shouldPolyfillRelativeTimeFormat(locale))) {
console.log('NOT polyfilling');
return;
}
async function polyfill(locale) { if (shouldPolyfillRelativeTimeFormat(locale)) {
if (!shouldPolyfillRelativeTimeFormat(locale)) return; await import('@formatjs/intl-relativetimeformat/polyfill');
}
// Load the polyfill 1st BEFORE loading data
await import('@formatjs/intl-relativetimeformat/polyfill')
if (shouldPolyfillPluralRules(locale)) { if (shouldPolyfillPluralRules(locale)) {
await import('@formatjs/intl-pluralrules/polyfill'); await import('@formatjs/intl-pluralrules/polyfill');
@ -38,7 +48,7 @@ async function polyfill(locale) {
case 'fr': case 'fr':
await import('@formatjs/intl-relativetimeformat/locale-data/fr'); await import('@formatjs/intl-relativetimeformat/locale-data/fr');
await import('@formatjs/intl-pluralrules/locale-data/fr'); await import('@formatjs/intl-pluralrules/locale-data/fr');
break break;
case 'ja': case 'ja':
await import('@formatjs/intl-relativetimeformat/locale-data/ja'); await import('@formatjs/intl-relativetimeformat/locale-data/ja');
await import('@formatjs/intl-pluralrules/locale-data/ja'); await import('@formatjs/intl-pluralrules/locale-data/ja');
@ -46,6 +56,7 @@ async function polyfill(locale) {
case 'tr': case 'tr':
await import('@formatjs/intl-relativetimeformat/locale-data/tr'); await import('@formatjs/intl-relativetimeformat/locale-data/tr');
await import('@formatjs/intl-pluralrules/locale-data/tr'); await import('@formatjs/intl-pluralrules/locale-data/tr');
break;
case 'zh': case 'zh':
case 'zh-CN': case 'zh-CN':
case 'zh-TW': case 'zh-TW':
@ -57,5 +68,6 @@ async function polyfill(locale) {
await import('@formatjs/intl-pluralrules/locale-data/en'); await import('@formatjs/intl-pluralrules/locale-data/en');
break; break;
} }
} };
export default polyfill;
export default intlPolyfill;

File diff suppressed because one or more lines are too long