如何使用 i18next 在嵌套翻译中应用格式化程序
How to apply a formatter in nested translations with i18next
我想用 i18next 格式化嵌套翻译
鉴于资源:
{
translation: {
en: {
food: 'bread',
food_is_good: "$t(food), that's not bad",
},
},
}
和格式化函数:
function format(value, format, lng) {
if (value == undefined) return value;
switch (format) {
case 'capitalize':
return _.capitalize(value);
default:
return value;
}
}
在i18next的初始化中使用的:
...
interpolation: { format: format },
...
我希望输出是“面包,还不错”。
所以我希望是这样的:
{
...
"food_is_good_1" : "$t(food,capitalize), that's not bad",
"food_is_good_2" : "{{$t(food),capitalize}}, that's not bad",
"food_is_good_3" : "{{food,capitalize}}, that's not bad",
...
}
就可以了。
第一个选项显示错误:"failed parsing options string in nesting"
最后两个选项警告:missed to pass in variable food,capitalize for interpolating {{food,capitalize}}
您的第一个选项适用于最新的 i18next
(v19)。
https://codesandbox.io/s/react-i18next-forked-g1d47?file=/src/i18n.js
我想用 i18next 格式化嵌套翻译
鉴于资源:
{
translation: {
en: {
food: 'bread',
food_is_good: "$t(food), that's not bad",
},
},
}
和格式化函数:
function format(value, format, lng) {
if (value == undefined) return value;
switch (format) {
case 'capitalize':
return _.capitalize(value);
default:
return value;
}
}
在i18next的初始化中使用的:
...
interpolation: { format: format },
...
我希望输出是“面包,还不错”。 所以我希望是这样的:
{
...
"food_is_good_1" : "$t(food,capitalize), that's not bad",
"food_is_good_2" : "{{$t(food),capitalize}}, that's not bad",
"food_is_good_3" : "{{food,capitalize}}, that's not bad",
...
}
就可以了。
第一个选项显示错误:"failed parsing options string in nesting"
最后两个选项警告:missed to pass in variable food,capitalize for interpolating {{food,capitalize}}
您的第一个选项适用于最新的 i18next
(v19)。
https://codesandbox.io/s/react-i18next-forked-g1d47?file=/src/i18n.js