|
118 | 118 | } |
119 | 119 | }; |
120 | 120 |
|
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 | + }]; |
125 | 126 | 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) { |
129 | 131 | i18n.services.logger.warn(...args); |
130 | 132 | } else if (console?.warn) { |
131 | | - if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`; |
132 | 133 | console.warn(...args); |
133 | 134 | } |
134 | 135 | }; |
135 | 136 | 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); |
143 | 141 | }; |
144 | 142 | const loadedClb = (i18n, cb) => () => { |
145 | 143 | if (i18n.isInitialized) { |
|
168 | 166 | const hasLoadedNamespace = function (ns, i18n) { |
169 | 167 | let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
170 | 168 | 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 | + }); |
172 | 172 | return true; |
173 | 173 | } |
174 | 174 | return i18n.hasLoadedNamespace(ns, { |
|
261 | 261 | childrenArray.forEach((child, childIndex) => { |
262 | 262 | if (isString(child)) { |
263 | 263 | stringNode += `${child}`; |
264 | | - } else if (react.isValidElement(child)) { |
| 264 | + return; |
| 265 | + } |
| 266 | + if (react.isValidElement(child)) { |
265 | 267 | const { |
266 | 268 | props, |
267 | 269 | type |
|
271 | 273 | const childChildren = props.children; |
272 | 274 | if (!childChildren && shouldKeepChild && !childPropsCount) { |
273 | 275 | stringNode += `<${type}/>`; |
274 | | - } else if (!childChildren && (!shouldKeepChild || childPropsCount) || props.i18nIsDynamicList) { |
| 276 | + return; |
| 277 | + } |
| 278 | + if (!childChildren && (!shouldKeepChild || childPropsCount) || props.i18nIsDynamicList) { |
275 | 279 | stringNode += `<${childIndex}></${childIndex}>`; |
276 | | - } else if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) { |
| 280 | + return; |
| 281 | + } |
| 282 | + if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) { |
277 | 283 | stringNode += `<${type}>${childChildren}</${type}>`; |
278 | | - } else { |
279 | | - const content = nodesToString(childChildren, i18nOptions, i18n, i18nKey); |
280 | | - stringNode += `<${childIndex}>${content}</${childIndex}>`; |
| 284 | + return; |
281 | 285 | } |
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)) { |
285 | 297 | const { |
286 | 298 | format, |
287 | 299 | ...clone |
|
290 | 302 | if (keys.length === 1) { |
291 | 303 | const value = format ? `${keys[0]}, ${format}` : keys[0]; |
292 | 304 | 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; |
295 | 306 | } |
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; |
298 | 312 | } |
| 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 | + }); |
299 | 317 | }); |
300 | 318 | return stringNode; |
301 | 319 | }; |
|
438 | 456 | }); |
439 | 457 | return componentMap; |
440 | 458 | }; |
441 | | - const generateComponents = (components, translation, i18n) => { |
| 459 | + const generateComponents = (components, translation, i18n, i18nKey) => { |
442 | 460 | if (!components) return null; |
443 | 461 | if (Array.isArray(components)) { |
444 | 462 | return generateArrayComponents(components, translation); |
445 | 463 | } |
446 | 464 | if (isObject(components)) { |
447 | 465 | return generateObjectComponents(components, translation); |
448 | 466 | } |
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 | + }); |
450 | 470 | return null; |
451 | 471 | }; |
452 | 472 | function Trans$1(_ref) { |
|
468 | 488 | } = _ref; |
469 | 489 | const i18n = i18nFromProps || getI18n(); |
470 | 490 | 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 | + }); |
472 | 494 | return children; |
473 | 495 | } |
474 | 496 | const t = tFromProps || i18n.t.bind(i18n) || (k => k); |
|
509 | 531 | ns: namespaces |
510 | 532 | }; |
511 | 533 | const translation = key ? t(key, combinedTOpts) : defaultValue; |
512 | | - const generatedComponents = generateComponents(components, translation, i18n); |
| 534 | + const generatedComponents = generateComponents(components, translation, i18n, i18nKey); |
513 | 535 | const content = renderNodes(generatedComponents || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape); |
514 | 536 | const useAsParent = parent ?? reactI18nextOptions.defaultTransParent; |
515 | 537 | return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content; |
|
623 | 645 | const i18n = i18nFromProps || i18nFromContext || getI18n(); |
624 | 646 | if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces(); |
625 | 647 | 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'); |
627 | 649 | const notReadyT = (k, optsOrDefaultValue) => { |
628 | 650 | if (isString(optsOrDefaultValue)) return optsOrDefaultValue; |
629 | 651 | if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue)) return optsOrDefaultValue.defaultValue; |
|
635 | 657 | retNotReady.ready = false; |
636 | 658 | return retNotReady; |
637 | 659 | } |
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.'); |
639 | 661 | const i18nOptions = { |
640 | 662 | ...getDefaults(), |
641 | 663 | ...i18n.options.react, |
|
0 commit comments