transCore.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. function splitNsKey(key, nsSeparator) {
  15. if (!nsSeparator)
  16. return { i18nKey: key };
  17. var i = key.indexOf(nsSeparator);
  18. if (i < 0)
  19. return { i18nKey: key };
  20. return {
  21. namespace: key.slice(0, i),
  22. i18nKey: key.slice(i + nsSeparator.length),
  23. };
  24. }
  25. function transCore(_a) {
  26. var config = _a.config, allNamespaces = _a.allNamespaces, pluralRules = _a.pluralRules, lang = _a.lang;
  27. var _b = config.logger, logger = _b === void 0 ? missingKeyLogger : _b;
  28. var t = function (key, query, options) {
  29. var _a;
  30. if (key === void 0) { key = ''; }
  31. var k = Array.isArray(key) ? key[0] : key;
  32. var _b = config.nsSeparator, nsSeparator = _b === void 0 ? ':' : _b, _c = config.loggerEnvironment, loggerEnvironment = _c === void 0 ? 'browser' : _c;
  33. var _d = splitNsKey(k, nsSeparator), i18nKey = _d.i18nKey, _e = _d.namespace, namespace = _e === void 0 ? (_a = options === null || options === void 0 ? void 0 : options.ns) !== null && _a !== void 0 ? _a : config.defaultNS : _e;
  34. var dic = (namespace && allNamespaces[namespace]) || {};
  35. var keyWithPlural = plural(pluralRules, dic, i18nKey, config, query);
  36. var value = getDicValue(dic, keyWithPlural, config, options);
  37. var empty = typeof value === 'undefined' ||
  38. (typeof value === 'object' && !Object.keys(value).length);
  39. var fallbacks = typeof (options === null || options === void 0 ? void 0 : options.fallback) === 'string'
  40. ? [options.fallback]
  41. : (options === null || options === void 0 ? void 0 : options.fallback) || [];
  42. if (empty &&
  43. (loggerEnvironment === 'both' ||
  44. loggerEnvironment ===
  45. (typeof window === 'undefined' ? 'node' : 'browser'))) {
  46. logger({ namespace: namespace, i18nKey: i18nKey });
  47. }
  48. if (empty && Array.isArray(fallbacks) && fallbacks.length) {
  49. var firstFallback = fallbacks[0], restFallbacks = fallbacks.slice(1);
  50. if (typeof firstFallback === 'string') {
  51. return t(firstFallback, query, __assign(__assign({}, options), { fallback: restFallbacks }));
  52. }
  53. }
  54. if (empty && (options === null || options === void 0 ? void 0 : options.default) && (fallbacks === null || fallbacks === void 0 ? void 0 : fallbacks.length) == 0) {
  55. return interpolation({ text: options === null || options === void 0 ? void 0 : options.default, query: query, config: config, lang: lang });
  56. }
  57. if (empty) {
  58. return k;
  59. }
  60. if (value instanceof Object) {
  61. return objectInterpolation({
  62. obj: value,
  63. query: query,
  64. config: config,
  65. lang: lang,
  66. });
  67. }
  68. return interpolation({ text: value, query: query, config: config, lang: lang });
  69. };
  70. return t;
  71. }
  72. exports.default = transCore;
  73. function getDicValue(dic, key, config, options) {
  74. if (key === void 0) { key = ''; }
  75. if (options === void 0) { options = {
  76. returnObjects: false,
  77. }; }
  78. var _a = (config || {}).keySeparator, keySeparator = _a === void 0 ? '.' : _a;
  79. var keyParts = keySeparator ? key.split(keySeparator) : [key];
  80. if (key === keySeparator && options.returnObjects)
  81. return dic;
  82. var value = keyParts.reduce(function (val, key) {
  83. if (typeof val === 'string') {
  84. return {};
  85. }
  86. var res = val[key];
  87. return res || (typeof res === 'string' ? res : {});
  88. }, dic);
  89. if (typeof value === 'string' ||
  90. (value instanceof Object && options.returnObjects)) {
  91. return value;
  92. }
  93. return undefined;
  94. }
  95. function plural(pluralRules, dic, key, config, query) {
  96. if (!query || typeof query.count !== 'number')
  97. return key;
  98. var numKey = "".concat(key, "_").concat(query.count);
  99. if (getDicValue(dic, numKey, config) !== undefined)
  100. return numKey;
  101. var pluralKey = "".concat(key, "_").concat(pluralRules.select(query.count));
  102. if (getDicValue(dic, pluralKey, config) !== undefined) {
  103. return pluralKey;
  104. }
  105. var nestedNumKey = "".concat(key, ".").concat(query.count);
  106. if (getDicValue(dic, nestedNumKey, config) !== undefined)
  107. return nestedNumKey;
  108. var nestedKey = "".concat(key, ".").concat(pluralRules.select(query.count));
  109. if (getDicValue(dic, nestedKey, config) !== undefined)
  110. return nestedKey;
  111. return key;
  112. }
  113. function interpolation(_a) {
  114. var text = _a.text, query = _a.query, config = _a.config, lang = _a.lang;
  115. if (!text || !query)
  116. return text || '';
  117. var escapeRegex = function (str) {
  118. return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  119. };
  120. var _b = config.interpolation || {}, _c = _b.format, format = _c === void 0 ? null : _c, _d = _b.prefix, prefix = _d === void 0 ? '{{' : _d, _e = _b.suffix, suffix = _e === void 0 ? '}}' : _e;
  121. var regexEnd = suffix === '' ? '' : "(?:[\\s,]+([\\w-]*))?\\s*".concat(escapeRegex(suffix));
  122. return Object.keys(query).reduce(function (all, varKey) {
  123. if (typeof all !== 'string')
  124. return all;
  125. var regex = new RegExp("".concat(escapeRegex(prefix), "\\s*").concat(varKey).concat(regexEnd), 'gm');
  126. return all.replace(regex, function (_match, $1) {
  127. return $1 && format
  128. ? format(query[varKey], $1, lang)
  129. : query[varKey];
  130. });
  131. }, text);
  132. }
  133. function objectInterpolation(_a) {
  134. var obj = _a.obj, query = _a.query, config = _a.config, lang = _a.lang;
  135. if (!query || Object.keys(query).length === 0)
  136. return obj;
  137. Object.keys(obj).forEach(function (key) {
  138. if (obj[key] instanceof Object)
  139. objectInterpolation({
  140. obj: obj[key],
  141. query: query,
  142. config: config,
  143. lang: lang,
  144. });
  145. if (typeof obj[key] === 'string')
  146. obj[key] = interpolation({
  147. text: obj[key],
  148. query: query,
  149. config: config,
  150. lang: lang,
  151. });
  152. });
  153. return obj;
  154. }
  155. function missingKeyLogger(_a) {
  156. var namespace = _a.namespace, i18nKey = _a.i18nKey;
  157. if (process.env.NODE_ENV === 'production')
  158. return;
  159. if (!namespace) {
  160. console.warn("[next-translate] The text \"".concat(i18nKey, "\" has no namespace in front of it."));
  161. return;
  162. }
  163. console.warn("[next-translate] \"".concat(namespace, ":").concat(i18nKey, "\" is missing in current namespace configuration. Try adding \"").concat(i18nKey, "\" to the namespace \"").concat(namespace, "\"."));
  164. }