translation - 如何使用 i18next 在嵌套翻译中应用格式化程序

标签 translation 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

关于translation - 如何使用 i18next 在嵌套翻译中应用格式化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38867564/

相关文章:

c++ - 将C++编写的程序逐渐翻译成Python可行吗?

javascript - React 中常量文件的翻译

php - 开发多语言系统

reactjs - 如何在使用 react-testing-library 和 jest 完成的单元测试中启用 react-i18n 翻译文件?

javascript - 使用 onClick 链接到 React/Semantic UI 中的组件

javascript - React 将函数组件转换为类组件

laravel - 我想在 Laravel 5.3 上使用带变量的复数翻译

python - pyqt5 应用程序中对话框和主窗口的国际化(翻译)

internationalization - i18n 想要加载一个未指定的翻译

html - 在网络移动应用程序中使用 i18next 本地化和 jquerymobile v1.3.2 NavBar 样式丢失