This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
453 lines (403 loc) · 12.9 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright 2019 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express
* or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/
import { fromJS, Map as iMap } from 'immutable';
import typeScope from '../utils/typeScope';
import { getModuleBaseUrl } from '../utils/modules';
export const defaultLocale = 'en-US';
/* istanbul ignore next */
// This import will resolve in the build output
// eslint-disable-next-line import/no-unresolved -- load based on server or browser
const localePacks = global.BROWSER ? require('./localePacks.client') : require('./localePacks.node');
export const UPDATE_LOCALE = `${typeScope}/intl/UPDATE_LOCALE`;
export const LANGUAGE_PACK_REQUEST = `${typeScope}/intl/LANGUAGE_PACK_REQUEST`;
export const LANGUAGE_PACK_SUCCESS = `${typeScope}/intl/LANGUAGE_PACK_SUCCESS`;
export const LANGUAGE_PACK_FAILURE = `${typeScope}/intl/LANGUAGE_PACK_FAILURE`;
export const LANGUAGE_PACK_DEFERRED_FORCE_LOAD = `${typeScope}/intl/LANGUAGE_PACK_DEFERRED_FORCE_LOAD`;
function buildInitialState({ req } = {}) {
let activeLocale = null;
if (!req) {
const { navigator } = global;
activeLocale = (navigator && navigator.language) || defaultLocale;
} else {
const acceptsLanguages = req.acceptsLanguages();
activeLocale = acceptsLanguages && acceptsLanguages[0];
if (!activeLocale || activeLocale === '*') {
activeLocale = defaultLocale;
}
}
return fromJS({
activeLocale,
});
}
// eslint-disable-next-line default-param-last -- reducers have default params first
export default function reducer(state = buildInitialState(), action) {
switch (action.type) {
case UPDATE_LOCALE: {
return state.set('activeLocale', action.locale);
}
case LANGUAGE_PACK_REQUEST: {
const { locale, componentKey, promise } = action;
const langPackState = state.getIn(['languagePacks', locale, componentKey], iMap());
return langPackState.get('_loadedOnServer')
? state
: state.updateIn(['languagePacks', locale, componentKey], iMap(), (nextState) => nextState.withMutations((map) => map
.set('data', map.get('data', iMap()))
.set('isLoading', true)
.set('promise', promise)
.delete('error')
.delete('errorExpiration')
));
}
case LANGUAGE_PACK_SUCCESS: {
const { locale, componentKey, data } = action;
return state.updateIn(['languagePacks', locale, componentKey], iMap(), (nextState) => nextState.withMutations((map) => map
.set('data', iMap(data))
.set('isLoading', false)
.set('_loadedOnServer', !global.BROWSER)
.delete('promise')
.delete('error')
.delete('errorExpiration')
));
}
case LANGUAGE_PACK_FAILURE: {
const { locale, componentKey, error } = action;
if (state.getIn(['languagePacks', locale, componentKey, '_loadedOnServer'])) {
return state;
}
return state.updateIn(['languagePacks', locale, componentKey], iMap(), (nextState) => nextState.withMutations((map) => map
.set('data', map.get('data', iMap()))
.set('isLoading', false)
.set('errorExpiration', Date.now() + 10e3)
.set('error', error)
.delete('promise')
));
}
case LANGUAGE_PACK_DEFERRED_FORCE_LOAD: {
const { locale, componentKey } = action;
return state.setIn(['languagePacks', locale, componentKey, '_pendingDeferredForceLoad'], true);
}
default:
return state;
}
}
reducer.buildInitialState = buildInitialState;
// when running on the server, store previous requests for a language pack
// reduces network errors causing a needless 500 and removes the I/O time
/* istanbul ignore next */
const serverLangPackCache = global.BROWSER ? null : require('./server-cache');
export function deferredForceLoad(locale, componentKey) {
return {
type: LANGUAGE_PACK_DEFERRED_FORCE_LOAD,
locale,
componentKey,
};
}
const isLoaded = ({ getState, locale, componentKey }) => {
const state = getState();
return state.getIn(['intl', 'languagePacks', locale, componentKey])
&& !state.getIn(['intl', 'languagePacks', locale, componentKey, 'isLoading'])
&& !state.getIn(['intl', 'languagePacks', locale, componentKey, 'error']);
};
const getLoadingPromise = ({ getState, locale, componentKey }) => {
const state = getState();
return state.getIn(['intl', 'languagePacks', locale, componentKey, 'promise']);
};
const langPackToIguazu = ({ getState, componentKey }) => {
const state = getState();
const locale = state.getIn(['intl', 'activeLocale']);
const langPackState = state.getIn(['intl', 'languagePacks', locale, componentKey], iMap({
data: iMap(),
}));
return {
locale,
status: langPackState.get('isLoading', true) ? 'loading' : 'complete',
data: langPackState.get('data').toJS(),
error: langPackState.get('error'),
errorExpiration: langPackState.get('errorExpiration'),
};
};
const getUrl = ({ getState, langPackLocale, componentKey }) => {
const state = getState();
const moduleBaseUrl = getModuleBaseUrl(componentKey);
const localeFilename = state.getIn(['config', 'localeFilename']);
return `${[
moduleBaseUrl,
langPackLocale.toLowerCase(),
localeFilename || componentKey,
].join('/')}.json`;
};
const fetchLanguagePack = ({
dispatch,
getState,
url,
locale,
fallbackLocale,
fallbackUrl,
componentKey,
fetchClient,
retry = false,
}) => {
let defaultUrl;
if (!global.BROWSER) {
defaultUrl = getUrl({ getState, langPackLocale: locale, componentKey });
const cached = serverLangPackCache.get(url, defaultUrl);
if (cached) {
// eslint-disable-next-line no-console -- logging
console.debug('using serverLangPackCache for %s', url);
return Promise.resolve(cached);
}
}
return fetchClient(url)
.then((response) => {
if (response.ok) {
return response.json();
}
return Promise.reject(response);
})
.then((data) => {
if (!global.BROWSER) {
// eslint-disable-next-line no-console -- logging
console.debug('setting serverLangPackCache: url %s, data %o', url, data);
serverLangPackCache.set(url, data, defaultUrl);
}
return data;
})
.catch((errorOrResponse) => {
if (errorOrResponse instanceof Error) {
return Promise.reject(errorOrResponse);
}
const { status, statusText, url: responseUrl } = errorOrResponse;
if (status === 404 && fallbackLocale && locale !== fallbackLocale && !retry) {
// eslint-disable-next-line no-console -- logging
console.warn('Missing %s language pack for %s, falling back to %s.', locale, componentKey, fallbackLocale);
return fetchLanguagePack({
getState,
dispatch,
url: fallbackUrl || getUrl({
getState,
langPackLocale: fallbackLocale,
componentKey,
}),
locale: fallbackLocale,
fetchClient,
componentKey,
retry,
});
}
if (status === 404) return Promise.resolve({});
const error = new Error(`${statusText} (${responseUrl})`);
error.response = errorOrResponse;
return Promise.reject(error);
});
};
function deferredForcedLoadLanguagePack({
dispatch,
locale,
url,
fallbackLocale,
fallbackUrl,
componentKey,
loadLanguagePackAction,
}) {
if (!global.BROWSER) return;
dispatch(deferredForceLoad(locale, componentKey));
const callback = () => dispatch(loadLanguagePackAction(componentKey, {
locale,
url,
force: true,
fallbackLocale,
fallbackUrl,
}));
if (typeof window.requestIdleCallback === 'function') {
window.requestIdleCallback(callback);
} else {
setTimeout(callback, 10e6);
}
}
const getResourceFromState = ({
dispatch,
getState,
locale,
url,
fallbackLocale,
fallbackUrl,
componentKey,
loadLanguagePackAction,
}) => {
const state = getState();
const langPack = state.getIn(['intl', 'languagePacks', locale, componentKey]);
// eslint-disable-next-line no-underscore-dangle -- properties have underscore
if (langPack && langPack._loadedOnServer && !langPack._pendingDeferredForceLoad) {
// Make a request on the client once idle even though the language
// pack was loaded on the server, so that it can be cached by the
// service worker for offline browsing.
deferredForcedLoadLanguagePack({
dispatch,
locale,
url,
fallbackLocale,
fallbackUrl,
componentKey,
loadLanguagePackAction,
});
}
return langPack;
};
export function loadLanguagePack(
componentKey,
{
locale: givenLocale,
force = false,
url,
fallbackLocale,
fallbackUrl,
} = {}
) {
return (dispatch, getState, { fetchClient }) => {
const intlState = getState().get('intl');
const locale = givenLocale
|| intlState.get('nextLocale')
|| intlState.get('activeLocale');
if (!locale) {
return Promise.reject(new Error('Failed to load language pack. No locale was set or given'));
}
if (fallbackUrl && !fallbackLocale) {
return Promise.reject(new Error('Fallback locale is required when fallback URL is provided for language pack'));
}
const existingPromise = getLoadingPromise({ getState, locale, componentKey });
if (existingPromise && !force) {
return existingPromise;
}
if (isLoaded({ getState, locale, componentKey }) && !force) {
return Promise.resolve(getResourceFromState({
dispatch,
getState,
locale,
url,
fallbackLocale,
fallbackUrl,
componentKey,
loadLanguagePackAction: loadLanguagePack,
}));
}
const promise = fetchLanguagePack({
getState,
dispatch,
url: url || getUrl({
getState,
langPackLocale: locale,
componentKey,
}),
locale,
fallbackLocale,
fallbackUrl,
fetchClient,
componentKey,
});
// Dispatch request
dispatch({
type: LANGUAGE_PACK_REQUEST,
componentKey,
locale,
promise,
});
// Handle Success or Failure Dispatch as Side Effect
promise
.then((data) => dispatch({
type: LANGUAGE_PACK_SUCCESS,
lastFetched: Date.now(),
componentKey,
locale,
data: fromJS(data),
}))
.catch((error) => dispatch({
type: LANGUAGE_PACK_FAILURE,
error,
componentKey,
locale,
}))
.finally(() => getResourceFromState({
dispatch,
getState,
locale,
url,
fallbackLocale,
fallbackUrl,
componentKey,
loadLanguagePackAction: loadLanguagePack,
}));
// Return original promise to have custom handling possible
return promise;
};
}
export function queryLanguagePack(componentKey, { fallbackLocale } = {}) {
return (dispatch, getState) => {
const iguazuState = langPackToIguazu({ getState, componentKey });
if (iguazuState.error) {
const isExpired = Date.now() > iguazuState.errorExpiration;
if (!isExpired) {
return {
...iguazuState,
promise: Promise.reject(iguazuState.error),
};
}
}
const promise = dispatch(loadLanguagePack(componentKey, {
locale: iguazuState.locale,
fallbackLocale,
}));
return {
...langPackToIguazu({ getState, componentKey }),
promise,
};
};
}
export const loadedLocales = new Set();
export function getLocalePack(locale) {
const localeArray = locale.split('-');
let localePack;
while (localeArray.length > 0 && !localePack) {
localePack = localePacks[localeArray.join('-')];
localeArray.pop();
}
if (localePack) {
if (!loadedLocales.has(locale)) {
loadedLocales.add(locale);
return localePack();
}
return Promise.resolve();
}
return Promise.reject(new Error(`No locale bundle available for "${locale}" (type ${typeof locale})`));
}
export function updateLocale(locale) {
return (dispatch) => {
if (!locale) {
return Promise.reject(new Error('No locale was given'));
}
const action = {
type: UPDATE_LOCALE,
locale,
};
if (globalThis.window?.useNativeIntl || process.env.ONE_CONFIG_USE_NATIVE_INTL === 'true') {
return Promise.resolve().then(() => {
dispatch(action);
});
}
// supporting files for locale dependent libraries
return getLocalePack(locale)
.then(() => {
dispatch(action);
});
};
}