Skip to content

Commit 26ab5a3

Browse files
committed
release
1 parent 3cd025f commit 26ab5a3

3 files changed

Lines changed: 62 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 15.4.0
2+
3+
feat: add meta with codes on warnings to allow conditional logging [1826](https://github.com/i18next/react-i18next/pull/1826)
4+
15
### 15.3.0
26

37
Uses the i18next logger instead of the default console logger, if there is a valid i18next instance. Now the debug i18next option is respected, and you can also inject your own logger module: https://www.i18next.com/misc/creating-own-plugins#logger

react-i18next.js

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,26 @@
118118
}
119119
};
120120

121-
const warn = function (i18n) {
122-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
123-
args[_key - 1] = arguments[_key];
124-
}
121+
const warn = (i18n, code, msg, rest) => {
122+
const args = [msg, {
123+
code,
124+
...(rest || {})
125+
}];
125126
if (i18n?.services?.logger?.forward) {
126-
i18n.services.logger.forward(args, 'warn', 'react-i18next::', true);
127-
} else if (i18n?.services?.logger?.warn) {
128-
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
127+
return i18n.services.logger.forward(args, 'warn', 'react-i18next::', true);
128+
}
129+
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
130+
if (i18n?.services?.logger?.warn) {
129131
i18n.services.logger.warn(...args);
130132
} else if (console?.warn) {
131-
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
132133
console.warn(...args);
133134
}
134135
};
135136
const alreadyWarned = {};
136-
const warnOnce = function (i18n) {
137-
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
138-
args[_key2 - 1] = arguments[_key2];
139-
}
140-
if (isString(args[0]) && alreadyWarned[args[0]]) return;
141-
if (isString(args[0])) alreadyWarned[args[0]] = new Date();
142-
warn(i18n, ...args);
137+
const warnOnce = (i18n, code, msg, rest) => {
138+
if (isString(msg) && alreadyWarned[msg]) return;
139+
if (isString(msg)) alreadyWarned[msg] = new Date();
140+
warn(i18n, code, msg, rest);
143141
};
144142
const loadedClb = (i18n, cb) => () => {
145143
if (i18n.isInitialized) {
@@ -168,7 +166,9 @@
168166
const hasLoadedNamespace = function (ns, i18n) {
169167
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
170168
if (!i18n.languages || !i18n.languages.length) {
171-
warnOnce(i18n, 'i18n.languages were undefined or empty', i18n.languages);
169+
warnOnce(i18n, 'NO_LANGUAGES', 'i18n.languages were undefined or empty', {
170+
languages: i18n.languages
171+
});
172172
return true;
173173
}
174174
return i18n.hasLoadedNamespace(ns, {
@@ -261,7 +261,9 @@
261261
childrenArray.forEach((child, childIndex) => {
262262
if (isString(child)) {
263263
stringNode += `${child}`;
264-
} else if (react.isValidElement(child)) {
264+
return;
265+
}
266+
if (react.isValidElement(child)) {
265267
const {
266268
props,
267269
type
@@ -271,17 +273,27 @@
271273
const childChildren = props.children;
272274
if (!childChildren && shouldKeepChild && !childPropsCount) {
273275
stringNode += `<${type}/>`;
274-
} else if (!childChildren && (!shouldKeepChild || childPropsCount) || props.i18nIsDynamicList) {
276+
return;
277+
}
278+
if (!childChildren && (!shouldKeepChild || childPropsCount) || props.i18nIsDynamicList) {
275279
stringNode += `<${childIndex}></${childIndex}>`;
276-
} else if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) {
280+
return;
281+
}
282+
if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) {
277283
stringNode += `<${type}>${childChildren}</${type}>`;
278-
} else {
279-
const content = nodesToString(childChildren, i18nOptions, i18n, i18nKey);
280-
stringNode += `<${childIndex}>${content}</${childIndex}>`;
284+
return;
281285
}
282-
} else if (child === null) {
283-
warn(i18n, `Trans: the passed in value is invalid - seems you passed in a null child.`);
284-
} else if (isObject(child)) {
286+
const content = nodesToString(childChildren, i18nOptions, i18n, i18nKey);
287+
stringNode += `<${childIndex}>${content}</${childIndex}>`;
288+
return;
289+
}
290+
if (child === null) {
291+
warn(i18n, 'TRANS_NULL_VALUE', `Passed in a null value as child`, {
292+
i18nKey
293+
});
294+
return;
295+
}
296+
if (isObject(child)) {
285297
const {
286298
format,
287299
...clone
@@ -290,12 +302,18 @@
290302
if (keys.length === 1) {
291303
const value = format ? `${keys[0]}, ${format}` : keys[0];
292304
stringNode += `{{${value}}}`;
293-
} else {
294-
warn(i18n, `react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.`, child, i18nKey);
305+
return;
295306
}
296-
} else {
297-
warn(i18n, `Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.`, child, i18nKey);
307+
warn(i18n, 'TRANS_INVALID_OBJ', `Invalid child - Object should only have keys {{ value, format }} (format is optional).`, {
308+
i18nKey,
309+
child
310+
});
311+
return;
298312
}
313+
warn(i18n, 'TRANS_INVALID_VAR', `Passed in a variable like {number} - pass variables for interpolation as full objects like {{number}}.`, {
314+
i18nKey,
315+
child
316+
});
299317
});
300318
return stringNode;
301319
};
@@ -438,15 +456,17 @@
438456
});
439457
return componentMap;
440458
};
441-
const generateComponents = (components, translation, i18n) => {
459+
const generateComponents = (components, translation, i18n, i18nKey) => {
442460
if (!components) return null;
443461
if (Array.isArray(components)) {
444462
return generateArrayComponents(components, translation);
445463
}
446464
if (isObject(components)) {
447465
return generateObjectComponents(components, translation);
448466
}
449-
warnOnce(i18n, '<Trans /> component prop expects an object or an array');
467+
warnOnce(i18n, 'TRANS_INVALID_COMPONENTS', `<Trans /> "components" prop expects an object or array`, {
468+
i18nKey
469+
});
450470
return null;
451471
};
452472
function Trans$1(_ref) {
@@ -468,7 +488,9 @@
468488
} = _ref;
469489
const i18n = i18nFromProps || getI18n();
470490
if (!i18n) {
471-
warnOnce(i18n, 'You will need to pass in an i18next instance by using i18nextReactModule');
491+
warnOnce(i18n, 'NO_I18NEXT_INSTANCE', `Trans: You need to pass in an i18next instance using i18nextReactModule`, {
492+
i18nKey
493+
});
472494
return children;
473495
}
474496
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
@@ -509,7 +531,7 @@
509531
ns: namespaces
510532
};
511533
const translation = key ? t(key, combinedTOpts) : defaultValue;
512-
const generatedComponents = generateComponents(components, translation, i18n);
534+
const generatedComponents = generateComponents(components, translation, i18n, i18nKey);
513535
const content = renderNodes(generatedComponents || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
514536
const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
515537
return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
@@ -623,7 +645,7 @@
623645
const i18n = i18nFromProps || i18nFromContext || getI18n();
624646
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
625647
if (!i18n) {
626-
warnOnce(i18n, 'You will need to pass in an i18next instance by using initReactI18next');
648+
warnOnce(i18n, 'NO_I18NEXT_INSTANCE', 'useTranslation: You will need to pass in an i18next instance by using initReactI18next');
627649
const notReadyT = (k, optsOrDefaultValue) => {
628650
if (isString(optsOrDefaultValue)) return optsOrDefaultValue;
629651
if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue)) return optsOrDefaultValue.defaultValue;
@@ -635,7 +657,7 @@
635657
retNotReady.ready = false;
636658
return retNotReady;
637659
}
638-
if (i18n.options.react?.wait) warnOnce(i18n, 'It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
660+
if (i18n.options.react?.wait) warnOnce(i18n, 'DEPRECATED_OPTION', 'useTranslation: It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
639661
const i18nOptions = {
640662
...getDefaults(),
641663
...i18n.options.react,

0 commit comments

Comments
 (0)