loadNamespaces.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  13. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (_) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. import getConfig from './getConfig';
  49. import getPageNamespaces from './getPageNamespaces';
  50. var colorEnabled = process.env.NODE_DISABLE_COLORS == null &&
  51. process.env.NO_COLOR == null &&
  52. process.env.TERM !== 'dumb' &&
  53. process.env.FORCE_COLOR !== '0';
  54. export default function loadNamespaces(config) {
  55. var _a, _b;
  56. if (config === void 0) { config = {}; }
  57. return __awaiter(this, void 0, void 0, function () {
  58. var conf, localesToIgnore, __lang, page, namespaces, defaultLoader, pageNamespaces, color;
  59. return __generator(this, function (_c) {
  60. switch (_c.label) {
  61. case 0:
  62. conf = __assign(__assign({}, getConfig()), config);
  63. localesToIgnore = conf.localesToIgnore || ['default'];
  64. __lang = ((_a = conf.req) === null || _a === void 0 ? void 0 : _a.locale) ||
  65. conf.locale ||
  66. ((_b = conf.router) === null || _b === void 0 ? void 0 : _b.locale) ||
  67. conf.defaultLocale ||
  68. '';
  69. if (!conf.pathname) {
  70. console.warn('🚨 [next-translate] You forgot to pass the "pathname" inside "loadNamespaces" configuration');
  71. return [2, { __lang: __lang }];
  72. }
  73. if (localesToIgnore.includes(__lang))
  74. return [2, { __lang: __lang }];
  75. if (!conf.loaderName && conf.loader !== false) {
  76. console.warn('🚨 [next-translate] You can remove the "loadNamespaces" helper, unless you set "loader: false" in your i18n config file.');
  77. }
  78. page = removeTrailingSlash(conf.pathname.replace(/\/index$/, '')) || '/';
  79. return [4, getPageNamespaces(conf, page, conf)];
  80. case 1:
  81. namespaces = _c.sent();
  82. defaultLoader = function () { return Promise.resolve({}); };
  83. return [4, Promise.all(namespaces.map(function (ns) {
  84. return typeof conf.loadLocaleFrom === 'function'
  85. ? conf.loadLocaleFrom(__lang, ns).catch(function () { return ({}); })
  86. : defaultLoader(__lang, ns);
  87. }))];
  88. case 2:
  89. pageNamespaces = (_c.sent()) || [];
  90. if (conf.logBuild !== false && typeof window === 'undefined') {
  91. color = function (c) { return (colorEnabled ? "\u001B[36m".concat(c, "\u001B[0m") : c); };
  92. console.log(color('next-translate'), "- compiled page:", color(page), '- locale:', color(__lang), '- namespaces:', color(namespaces.join(', ')), '- used loader:', color(conf.loaderName || '-'));
  93. }
  94. return [2, {
  95. __lang: __lang,
  96. __namespaces: namespaces.reduce(function (obj, ns, i) {
  97. obj[ns] = pageNamespaces[i] || null;
  98. return obj;
  99. }, {}),
  100. }];
  101. }
  102. });
  103. });
  104. }
  105. function removeTrailingSlash(path) {
  106. if (path === void 0) { path = ''; }
  107. return path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path;
  108. }