首页 > 解决方案 > i18next:自定义 json 格式,带有翻译局的注释

问题描述

我们为包含翻译局注释的 i18n 资源使用自定义 json 格式,以便他们更好地理解要翻译的字符串的上下文:

示例en.json

{
  "headerbar": {
    "search": {
      "placeholder": {
        "value": "Enter your search here...",
        "comment": "This string will be shown in the search input if empty. Truncated after 100 characters."
    }
  },
  "welcome": {
    "heading": {
      "value": "Welcome, {{name}}!",
      "comment": "This string should not be longer than 50 characters."
    }
  }
}

如何配置 i18next(或 react-i18next),以便始终从value属性中检索翻译?无需{returnObjects}在每个t().

t('headerbar.search.placeholder')  // === 'Enter your search here...'
t('welcome.header', {name: 'Bob'}) // === 'Welcome, Bob!'

标签: i18nextreact-i18next

解决方案


我也有这个要求,但似乎 i18next 没有定义注释或描述的能力,因为 1)API 没有定义这些的方法,以及 2)最流行的提取器i18next-parser没有t 支持生成包含注释的文件。

或者,您可以考虑具有此功能的 Format.JS: https ://formatjs.io/docs/getting-started/message-declaration


推荐阅读