如何在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 },
...

我希望输出是"Bread, that is not bad"。所以我希望像这样:

{
  ...
  "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

最新更新