You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

93 lines
2.9 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.warn = warn;
  6. exports.warnOnce = warnOnce;
  7. exports.loadNamespaces = loadNamespaces;
  8. exports.hasLoadedNamespace = hasLoadedNamespace;
  9. exports.getDisplayName = getDisplayName;
  10. function warn() {
  11. if (console && console.warn) {
  12. var _console;
  13. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  14. args[_key] = arguments[_key];
  15. }
  16. if (typeof args[0] === 'string') args[0] = "react-i18next:: ".concat(args[0]);
  17. (_console = console).warn.apply(_console, args);
  18. }
  19. }
  20. var alreadyWarned = {};
  21. function warnOnce() {
  22. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  23. args[_key2] = arguments[_key2];
  24. }
  25. if (typeof args[0] === 'string' && alreadyWarned[args[0]]) return;
  26. if (typeof args[0] === 'string') alreadyWarned[args[0]] = new Date();
  27. warn.apply(void 0, args);
  28. } // not needed right now
  29. //
  30. // export function deprecated(...args) {
  31. // if (process && process.env && (!process.env.NODE_ENV || process.env.NODE_ENV === 'development')) {
  32. // if (typeof args[0] === 'string') args[0] = `deprecation warning -> ${args[0]}`;
  33. // warnOnce(...args);
  34. // }
  35. // }
  36. function loadNamespaces(i18n, ns, cb) {
  37. i18n.loadNamespaces(ns, function () {
  38. // delay ready if not yet initialized i18n instance
  39. if (i18n.isInitialized) {
  40. cb();
  41. } else {
  42. var initialized = function initialized() {
  43. // due to emitter removing issue in i18next we need to delay remove
  44. setTimeout(function () {
  45. i18n.off('initialized', initialized);
  46. }, 0);
  47. cb();
  48. };
  49. i18n.on('initialized', initialized);
  50. }
  51. });
  52. }
  53. function hasLoadedNamespace(ns, i18n) {
  54. if (!i18n.languages || !i18n.languages.length) {
  55. warnOnce('i18n.languages were undefined or empty', i18n.languages);
  56. return true;
  57. }
  58. var lng = i18n.languages[0];
  59. var fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
  60. var lastLng = i18n.languages[i18n.languages.length - 1]; // we're in cimode so this shall pass
  61. if (lng.toLowerCase() === 'cimode') return true;
  62. var loadNotPending = function loadNotPending(l, n) {
  63. var loadState = i18n.services.backendConnector.state["".concat(l, "|").concat(n)];
  64. return loadState === -1 || loadState === 2;
  65. }; // loaded -> SUCCESS
  66. if (i18n.hasResourceBundle(lng, ns)) return true; // were not loading at all -> SEMI SUCCESS
  67. if (!i18n.services.backendConnector.backend) return true; // failed loading ns - but at least fallback is not pending -> SEMI SUCCESS
  68. if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
  69. return false;
  70. }
  71. function getDisplayName(Component) {
  72. return Component.displayName || Component.name || (typeof Component === 'string' && Component.length > 0 ? Component : 'Unknown');
  73. }