forked from gpbl/isomorphic500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntlUtils.js
74 lines (53 loc) · 1.99 KB
/
IntlUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const debug = require("debug")("isomorphic500");
const IntlUtils = {
hasBuiltInLocaleData(locale) {
return Intl.NumberFormat.supportedLocalesOf(locale)[0] === locale &&
Intl.DateTimeFormat.supportedLocalesOf(locale)[0] === locale;
},
// Returns a promise which is resolved when Intl chunk has been
// downloaded and required
loadIntlPolyfill(locale) {
return new Promise((resolve) => {
if (window.Intl && IntlUtils.hasBuiltInLocaleData(locale)) {
// all fine: Intl is in the global scope and the locale data is available
return resolve();
}
debug("Intl or locale data for %s not available, downloading the polyfill...", locale);
// Create a intl chunk with webpack and run the callback once it has
// been download
require.ensure(["intl"], (require) => {
const IntlPolyfill = require("intl");
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
debug("Intl polyfill for %s has been loaded", locale);
resolve();
}, "intl");
});
},
// Returns a promise which is resolved when locale datas chunks
// has been downloaded and required
loadLocaleData(locale) {
return new Promise((resolve) => {
if (IntlUtils.hasBuiltInLocaleData(locale)) {
debug("Locale data for %s is already available", locale);
return resolve();
}
if (locale === "it") {
require.ensure(["intl/locale-data/jsonp/it"], (require) => {
require("intl/locale-data/jsonp/it");
debug("Locale data for %s has been downloaded", locale);
resolve();
}, "locale-it");
}
else {
// english is the fallback
require.ensure(["intl/locale-data/jsonp/en"], (require) => {
require("intl/locale-data/jsonp/en");
debug("Locale data for %s has been downloaded", locale);
resolve();
}, "locale-en");
}
});
}
};
export default IntlUtils;