This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
/
IntlUtils.js
155 lines (126 loc) · 4.61 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Contains utils to download the locale data for the current language, eventually
// requiring the `Intl` polyfill for browser not supporting it
// It is used in client.js *before* rendering the root component.
import { addLocaleData } from "react-intl";
import isIntlLocaleSupported from "intl-locales-supported";
const debug = require("debug")("isomorphic500");
const IntlUtils = {
// Returns a promise which is resolved when Intl has been polyfilled
loadIntlPolyfill(locale) {
if (window.Intl && isIntlLocaleSupported(locale)) {
// all fine: Intl is in the global scope and the locale data is available
return Promise.resolve();
}
return new Promise(resolve => {
debug("Intl or locale data for %s not available, downloading the polyfill...", locale);
// When building: create a intl chunk with webpack
// When executing: run the callback once the chunk has been download.
require.ensure(["intl"], require => {
require("intl"); // apply the polyfill
debug("Intl polyfill for %s has been loaded", locale);
resolve();
}, "intl");
});
},
// Returns a promise which is resolved as the required locale-data chunks
// has been downloaded with webpack's require.ensure. For each language,
// we make two different chunks: one for browsers supporting `intl` and one
// for those who don't.
// The react-intl locale-data is required, for example, by the FormattedRelative
// component.
loadLocaleData(locale) {
const hasIntl = isIntlLocaleSupported(locale);
// Make sure ReactIntl is in the global scope: this is required for adding locale-data
// Since ReactIntl needs the `Intl` polyfill to be required (sic) we must place
// this require here, when loadIntlPolyfill is supposed to be present
require("expose?ReactIntl!react-intl");
return new Promise(resolve => {
switch (locale) {
// italian
case "it":
if (!hasIntl) {
require.ensure([
"intl/locale-data/jsonp/it",
"react-intl/locale-data/it"
], require => {
require("intl/locale-data/jsonp/it");
addLocaleData(require("react-intl/locale-data/it"));
debug("Intl and ReactIntl locale-data for %s has been downloaded", locale);
resolve();
}, "locale-it");
}
else {
require.ensure([
"react-intl/locale-data/it"
], require => {
addLocaleData(require("react-intl/locale-data/it"));
debug("ReactIntl locale-data for %s has been downloaded", locale);
resolve();
}, "locale-it-no-intl");
}
break;
// french
case "fr":
if (!hasIntl) {
require.ensure([
"intl/locale-data/jsonp/fr",
"react-intl/locale-data/fr"
], require => {
require("intl/locale-data/jsonp/fr");
addLocaleData(require("react-intl/locale-data/fr"));
debug("Intl and ReactIntl locale-data for %s has been downloaded", locale);
resolve();
}, "locale-fr");
}
else {
require.ensure([
"react-intl/locale-data/fr"
], require => {
addLocaleData(require("react-intl/locale-data/fr"));
debug("ReactIntl locale-data for %s has been downloaded", locale);
resolve();
}, "locale-fr-no-intl");
}
break;
// portugues
case "pt":
if (!hasIntl) {
require.ensure([
"intl/locale-data/jsonp/pt",
"react-intl/locale-data/pt"
], require => {
require("intl/locale-data/jsonp/pt");
addLocaleData(require("react-intl/locale-data/pt"));
debug("Intl locale-data for %s has been downloaded", locale);
resolve();
}, "locale-pt");
}
else {
require.ensure([
"react-intl/locale-data/pt"
], require => {
addLocaleData(require("react-intl/locale-data/pt"));
debug("ReactIntl locale-data for %s has been downloaded", locale);
resolve();
}, "locale-pt-no-intl");
}
break;
// english
default:
if (!hasIntl) {
require.ensure([
"intl/locale-data/jsonp/en"
], require => {
require("intl/locale-data/jsonp/en");
debug("Intl locale-data for %s has been downloaded", locale);
resolve();
}, "locale-en");
}
else {
resolve();
}
}
});
}
};
export default IntlUtils;