Skip to content

Instantly share code, notes, and snippets.

@rodneyrehm
Created August 21, 2017 08:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodneyrehm/fb8237007c70c41e2307df34cfa7117a to your computer and use it in GitHub Desktop.
Save rodneyrehm/fb8237007c70c41e2307df34cfa7117a to your computer and use it in GitHub Desktop.
Webpack: load Intl polyfill with languages
const needsPolyfill = !window.Intl
/* eslint-disable import/no-webpack-loader-syntax */
const intl = require('bundle-loader?lazy&name=intl!intl')
/* eslint-enable import/no-webpack-loader-syntax */
const polyfilled = needsPolyfill && new Promise(resolve => {
intl(resolve)
})
export default Promise.all([ polyfilled ])
// see https://webpack.js.org/loaders/bundle-loader/
// we have to statically reference each language file so that webpack can resolve them at build time
// see https://github.com/andyearnshaw/Intl.js/tree/master/locale-data/jsonp
const load = {
/* eslint-disable import/no-webpack-loader-syntax */
'de': require('bundle-loader?lazy&name=intl-de!intl/locale-data/jsonp/de.js'),
'el': require('bundle-loader?lazy&name=intl-el!intl/locale-data/jsonp/el.js'),
'en-US': require('bundle-loader?lazy&name=intl-en-US!intl/locale-data/jsonp/en-US.js'),
'en': require('bundle-loader?lazy&name=intl-en!intl/locale-data/jsonp/en-GB.js'),
'fi': require('bundle-loader?lazy&name=intl-fi!intl/locale-data/jsonp/fi.js'),
'hr': require('bundle-loader?lazy&name=intl-hr!intl/locale-data/jsonp/hr.js'),
'it': require('bundle-loader?lazy&name=intl-it!intl/locale-data/jsonp/it.js'),
'nb-NO': require('bundle-loader?lazy&name=intl-nb-NO!intl/locale-data/jsonp/nb-NO.js'),
'nl-NL': require('bundle-loader?lazy&name=intl-nl-NL!intl/locale-data/jsonp/nl-NL.js'),
'nn': require('bundle-loader?lazy&name=intl-nn!intl/locale-data/jsonp/nn.js'),
'sk': require('bundle-loader?lazy&name=intl-sk!intl/locale-data/jsonp/sk.js'),
/* eslint-enable import/no-webpack-loader-syntax */
}
export function loadIntlLocale (locale) {
if (!needsPolyfill) {
return Promise.resolve()
}
return polyfilled.then(() => {
return new Promise((resolve, reject) => {
const loader = load[locale]
if (!loader) {
reject(new Error(`Intl locale "${locale}" not registered`))
}
loader(resolve)
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment