76 lines
No EOL
2.7 KiB
JavaScript
76 lines
No EOL
2.7 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.warnOnce = exports.warn = exports.loadNamespaces = exports.loadLanguages = exports.isString = exports.isObject = exports.hasLoadedNamespace = exports.getDisplayName = void 0;
|
|
const warn = (i18n, code, msg, rest) => {
|
|
const args = [msg, {
|
|
code,
|
|
...(rest || {})
|
|
}];
|
|
if (i18n?.services?.logger?.forward) {
|
|
return i18n.services.logger.forward(args, 'warn', 'react-i18next::', true);
|
|
}
|
|
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
|
|
if (i18n?.services?.logger?.warn) {
|
|
i18n.services.logger.warn(...args);
|
|
} else if (console?.warn) {
|
|
console.warn(...args);
|
|
}
|
|
};
|
|
exports.warn = warn;
|
|
const alreadyWarned = {};
|
|
const warnOnce = (i18n, code, msg, rest) => {
|
|
if (isString(msg) && alreadyWarned[msg]) return;
|
|
if (isString(msg)) alreadyWarned[msg] = new Date();
|
|
warn(i18n, code, msg, rest);
|
|
};
|
|
exports.warnOnce = warnOnce;
|
|
const loadedClb = (i18n, cb) => () => {
|
|
if (i18n.isInitialized) {
|
|
cb();
|
|
} else {
|
|
const initialized = () => {
|
|
setTimeout(() => {
|
|
i18n.off('initialized', initialized);
|
|
}, 0);
|
|
cb();
|
|
};
|
|
i18n.on('initialized', initialized);
|
|
}
|
|
};
|
|
const loadNamespaces = (i18n, ns, cb) => {
|
|
i18n.loadNamespaces(ns, loadedClb(i18n, cb));
|
|
};
|
|
exports.loadNamespaces = loadNamespaces;
|
|
const loadLanguages = (i18n, lng, ns, cb) => {
|
|
if (isString(ns)) ns = [ns];
|
|
if (i18n.options.preload && i18n.options.preload.indexOf(lng) > -1) return loadNamespaces(i18n, ns, cb);
|
|
ns.forEach(n => {
|
|
if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
|
|
});
|
|
i18n.loadLanguages(lng, loadedClb(i18n, cb));
|
|
};
|
|
exports.loadLanguages = loadLanguages;
|
|
const hasLoadedNamespace = (ns, i18n, options = {}) => {
|
|
if (!i18n.languages || !i18n.languages.length) {
|
|
warnOnce(i18n, 'NO_LANGUAGES', 'i18n.languages were undefined or empty', {
|
|
languages: i18n.languages
|
|
});
|
|
return true;
|
|
}
|
|
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;
|
|
}
|
|
});
|
|
};
|
|
exports.hasLoadedNamespace = hasLoadedNamespace;
|
|
const getDisplayName = Component => Component.displayName || Component.name || (isString(Component) && Component.length > 0 ? Component : 'Unknown');
|
|
exports.getDisplayName = getDisplayName;
|
|
const isString = obj => typeof obj === 'string';
|
|
exports.isString = isString;
|
|
const isObject = obj => typeof obj === 'object' && obj !== null;
|
|
exports.isObject = isObject; |