-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathutils.js
71 lines (59 loc) · 2.12 KB
/
utils.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
export function warn(...args) {
if (console && console.warn) {
if (typeof args[0] === 'string') args[0] = `react-i18next:: ${args[0]}`;
console.warn.apply(console, args);
}
}
const alreadyWarned = {};
export function warnOnce(...args) {
if (typeof args[0] === 'string' && alreadyWarned[args[0]]) return;
if (typeof args[0] === 'string') alreadyWarned[args[0]] = new Date();
warn(...args);
}
// not needed right now
//
// export function deprecated(...args) {
// if (process && process.env && (!process.env.NODE_ENV || process.env.NODE_ENV === 'development')) {
// if (typeof args[0] === 'string') args[0] = `deprecation warning -> ${args[0]}`;
// warnOnce(...args);
// }
// }
export function loadNamespaces(i18n, ns, cb) {
i18n.loadNamespaces(ns, () => {
// delay ready if not yet initialized i18n instance
if (i18n.isInitialized) {
cb();
} else {
const initialized = () => {
// due to emitter removing issue in i18next we need to delay remove
setImmediate(() => {
i18n.off('initialized', initialized);
});
cb();
};
i18n.on('initialized', initialized);
}
});
}
export function hasLoadedNamespace(ns, i18n) {
if (!i18n.languages || !i18n.languages.length) {
warnOnce('i18n.languages were undefined or empty', i18n.languages);
return true;
}
const lng = i18n.languages[0];
const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
const lastLng = i18n.languages[i18n.languages.length - 1];
// we're in cimode so this shall pass
if (lng.toLowerCase() === 'cimode') return true;
const loadNotPending = (l, n) => {
const loadState = i18n.services.backendConnector.state[`${l}|${n}`];
return loadState === -1 || loadState === 2;
};
// loaded -> SUCCESS
if (i18n.hasResourceBundle(lng, ns)) return true;
// were not loading at all -> SEMI SUCCESS
if (!i18n.services.backendConnector.backend) return true;
// failed loading ns - but at least fallback is not pending -> SEMI SUCCESS
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
return false;
}