77 lines
3.3 KiB
JavaScript
77 lines
3.3 KiB
JavaScript
export function warn() {
|
|
if (console && console.warn) {
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
|
|
console.warn(...args);
|
|
}
|
|
}
|
|
const alreadyWarned = {};
|
|
export function warnOnce() {
|
|
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
args[_key2] = arguments[_key2];
|
|
}
|
|
if (isString(args[0]) && alreadyWarned[args[0]]) return;
|
|
if (isString(args[0])) alreadyWarned[args[0]] = new Date();
|
|
warn(...args);
|
|
}
|
|
const loadedClb = (i18n, cb) => () => {
|
|
if (i18n.isInitialized) {
|
|
cb();
|
|
} else {
|
|
const initialized = () => {
|
|
setTimeout(() => {
|
|
i18n.off('initialized', initialized);
|
|
}, 0);
|
|
cb();
|
|
};
|
|
i18n.on('initialized', initialized);
|
|
}
|
|
};
|
|
export const loadNamespaces = (i18n, ns, cb) => {
|
|
i18n.loadNamespaces(ns, loadedClb(i18n, cb));
|
|
};
|
|
export const loadLanguages = (i18n, lng, ns, cb) => {
|
|
if (isString(ns)) ns = [ns];
|
|
ns.forEach(n => {
|
|
if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
|
|
});
|
|
i18n.loadLanguages(lng, loadedClb(i18n, cb));
|
|
};
|
|
const oldI18nextHasLoadedNamespace = function (ns, i18n) {
|
|
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
const lng = i18n.languages[0];
|
|
const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
|
|
const lastLng = i18n.languages[i18n.languages.length - 1];
|
|
if (lng.toLowerCase() === 'cimode') return true;
|
|
const loadNotPending = (l, n) => {
|
|
const loadState = i18n.services.backendConnector.state[`${l}|${n}`];
|
|
return loadState === -1 || loadState === 2;
|
|
};
|
|
if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
|
|
if (i18n.hasResourceBundle(lng, ns)) return true;
|
|
if (!i18n.services.backendConnector.backend || i18n.options.resources && !i18n.options.partialBundledLanguages) return true;
|
|
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
|
|
return false;
|
|
};
|
|
export const hasLoadedNamespace = function (ns, i18n) {
|
|
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
if (!i18n.languages || !i18n.languages.length) {
|
|
warnOnce('i18n.languages were undefined or empty', i18n.languages);
|
|
return true;
|
|
}
|
|
const isNewerI18next = i18n.options.ignoreJSONStructure !== undefined;
|
|
if (!isNewerI18next) {
|
|
return oldI18nextHasLoadedNamespace(ns, i18n, options);
|
|
}
|
|
return i18n.hasLoadedNamespace(ns, {
|
|
lng: options.lng,
|
|
precheck: (i18nInstance, loadNotPending) => {
|
|
if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
|
|
}
|
|
});
|
|
};
|
|
export const getDisplayName = Component => Component.displayName || Component.name || (isString(Component) && Component.length > 0 ? Component : 'Unknown');
|
|
export const isString = obj => typeof obj === 'string';
|
|
export const isObject = obj => typeof obj === 'object' && obj !== null; |