transCore.js 6.9 KB

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