Nesting
Nesting allows you to reference other keys in a translation. Could be useful to build glossary terms.

Basic

keys
1
{
2
"nesting1": "1 $t(nesting2)",
3
"nesting2": "2 $t(nesting3)",
4
"nesting3": "3",
5
}
Copied!
sample
1
i18next.t('nesting1'); // -> "1 2 3"
Copied!
You can reference keys from other namespaces by prepending the namespace: "nesting1": "1 $t(common:nesting2)",

Passing options to nestings

You can pass entire data models in options.
keys
1
{
2
"girlsAndBoys": "They have $t(girls, {\"count\": {{girls}} }) and $t(boys, {\"count\": {{boys}} })",
3
"boys": "{{count}} boy",
4
"boys_other": "{{count}} boys",
5
"girls": "{{count}} girl",
6
"girls_other": "{{count}} girls",
7
}
Copied!
sample
1
i18next.t('girlsAndBoys', {girls: 3, boys: 2});
2
// -> "They have 3 girls and 2 boys"
Copied!
Make sure the options string is valid JSON and can be parsed using JSON.parse
'sampleKey': 'test $t(nest2, { "changedVarName": "{{var}}" })'

Passing nesting to interpolated

keys
1
{
2
"key1": "hello world",
3
"key2": "say: {{val}}"
4
}
Copied!
sample
1
i18next.t('key2', {val: '$t(key1)'});
2
// -> "say: hello world"
Copied!

Additional options

Prefix/Suffix for nesting and other options can be overridden in init interpolation options or by passing additional options to t function:
sample
1
i18next.init({
2
interpolation: { ... }
3
});
4
5
i18next.t('key', {
6
interpolation: { ... }
7
});
Copied!
option
default
description
nestingPrefixEscaped
undefined
escaped prefix for nesting (regexSafe)
nestingSuffixEscaped
undefined
escaped suffix for nesting (regexSafe)
While there are a lot of options going with the defaults should get you covered.
Last modified 25d ago