@@ -45,7 +45,9 @@ export const nodesToString = (children, i18nOptions, i18n, i18nKey) => {
45
45
// actual e.g. lorem
46
46
// expected e.g. lorem
47
47
stringNode += `${ child } ` ;
48
- } else if ( isValidElement ( child ) ) {
48
+ return ;
49
+ }
50
+ if ( isValidElement ( child ) ) {
49
51
const { props, type } = child ;
50
52
const childPropsCount = Object . keys ( props ) . length ;
51
53
const shouldKeepChild = keepArray . indexOf ( type ) > - 1 ;
@@ -55,53 +57,57 @@ export const nodesToString = (children, i18nOptions, i18n, i18nKey) => {
55
57
// actual e.g. lorem <br/> ipsum
56
58
// expected e.g. lorem <br/> ipsum
57
59
stringNode += `<${ type } />` ;
58
- } else if (
59
- ( ! childChildren && ( ! shouldKeepChild || childPropsCount ) ) ||
60
- props . i18nIsDynamicList
61
- ) {
60
+ return ;
61
+ }
62
+ if ( ( ! childChildren && ( ! shouldKeepChild || childPropsCount ) ) || props . i18nIsDynamicList ) {
62
63
// actual e.g. lorem <hr className="test" /> ipsum
63
64
// expected e.g. lorem <0></0> ipsum
64
65
// or
65
66
// we got a dynamic list like
66
67
// e.g. <ul i18nIsDynamicList>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>
67
68
// expected e.g. "<0></0>", not e.g. "<0><0>a</0><1>b</1></0>"
68
69
stringNode += `<${ childIndex } ></${ childIndex } >` ;
69
- } else if ( shouldKeepChild && childPropsCount === 1 && isString ( childChildren ) ) {
70
+ return ;
71
+ }
72
+ if ( shouldKeepChild && childPropsCount === 1 && isString ( childChildren ) ) {
70
73
// actual e.g. dolor <strong>bold</strong> amet
71
74
// expected e.g. dolor <strong>bold</strong> amet
72
75
stringNode += `<${ type } >${ childChildren } </${ type } >` ;
73
- } else {
74
- // regular case mapping the inner children
75
- const content = nodesToString ( childChildren , i18nOptions , i18n , i18nKey ) ;
76
- stringNode += `<${ childIndex } >${ content } </${ childIndex } >` ;
76
+ return ;
77
77
}
78
- } else if ( child === null ) {
79
- warn ( i18n , `Trans: the passed in value is invalid - seems you passed in a null child.` ) ;
80
- } else if ( isObject ( child ) ) {
78
+ // regular case mapping the inner children
79
+ const content = nodesToString ( childChildren , i18nOptions , i18n , i18nKey ) ;
80
+ stringNode += `<${ childIndex } >${ content } </${ childIndex } >` ;
81
+ return ;
82
+ }
83
+ if ( child === null ) {
84
+ warn ( i18n , 'TRANS_NULL_VALUE' , `Passed in a null value as child` , { i18nKey } ) ;
85
+ return ;
86
+ }
87
+ if ( isObject ( child ) ) {
81
88
// e.g. lorem {{ value, format }} ipsum
82
89
const { format, ...clone } = child ;
83
90
const keys = Object . keys ( clone ) ;
84
91
85
92
if ( keys . length === 1 ) {
86
93
const value = format ? `${ keys [ 0 ] } , ${ format } ` : keys [ 0 ] ;
87
94
stringNode += `{{${ value } }}` ;
88
- } else {
89
- // not a valid interpolation object (can only contain one value plus format)
90
- warn (
91
- i18n ,
92
- `react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.` ,
93
- child ,
94
- i18nKey ,
95
- ) ;
95
+ return ;
96
96
}
97
- } else {
98
97
warn (
99
98
i18n ,
100
- `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}}.` ,
101
- child ,
102
- i18nKey ,
99
+ 'TRANS_INVALID_OBJ' ,
100
+ `Invalid child - Object should only have keys {{ value, format }} (format is optional).` ,
101
+ { i18nKey, child } ,
103
102
) ;
103
+ return ;
104
104
}
105
+ warn (
106
+ i18n ,
107
+ 'TRANS_INVALID_VAR' ,
108
+ `Passed in a variable like {number} - pass variables for interpolation as full objects like {{number}}.` ,
109
+ { i18nKey, child } ,
110
+ ) ;
105
111
} ) ;
106
112
107
113
return stringNode ;
@@ -336,7 +342,7 @@ const generateObjectComponents = (components, translation) => {
336
342
return componentMap ;
337
343
} ;
338
344
339
- const generateComponents = ( components , translation , i18n ) => {
345
+ const generateComponents = ( components , translation , i18n , i18nKey ) => {
340
346
if ( ! components ) return null ;
341
347
342
348
// components could be either an array or an object
@@ -351,7 +357,12 @@ const generateComponents = (components, translation, i18n) => {
351
357
352
358
// if components is not an array or an object, warn the user
353
359
// and return null
354
- warnOnce ( i18n , '<Trans /> component prop expects an object or an array' ) ;
360
+ warnOnce (
361
+ i18n ,
362
+ 'TRANS_INVALID_COMPONENTS' ,
363
+ `<Trans /> "components" prop expects an object or array` ,
364
+ { i18nKey } ,
365
+ ) ;
355
366
return null ;
356
367
} ;
357
368
@@ -374,7 +385,12 @@ export function Trans({
374
385
const i18n = i18nFromProps || getI18n ( ) ;
375
386
376
387
if ( ! i18n ) {
377
- warnOnce ( i18n , 'You will need to pass in an i18next instance by using i18nextReactModule' ) ;
388
+ warnOnce (
389
+ i18n ,
390
+ 'NO_I18NEXT_INSTANCE' ,
391
+ `Trans: You need to pass in an i18next instance using i18nextReactModule` ,
392
+ { i18nKey } ,
393
+ ) ;
378
394
return children ;
379
395
}
380
396
@@ -417,7 +433,7 @@ export function Trans({
417
433
} ;
418
434
const translation = key ? t ( key , combinedTOpts ) : defaultValue ;
419
435
420
- const generatedComponents = generateComponents ( components , translation , i18n ) ;
436
+ const generatedComponents = generateComponents ( components , translation , i18n , i18nKey ) ;
421
437
422
438
const content = renderNodes (
423
439
generatedComponents || children ,
0 commit comments