reactjs - i18next 警告或检查缺少语言的键(不是 fallbackLng)

标签 reactjs i18next

我的项目正在使用 i18next 到目前为止,react-i18next 取得了成功。不涉及本地化或服务器端。

当前设置:
- 默认和后备语言:en
- 附加语言:fr

我想自动化一些与错误检查相关的事情:
1. 检查所有 key 是否在每个定义的语言文件中被翻译
2. 在 dev env 或 lint 中为 ci/cd 发出警告。


我试过做 1. 使用以下选项:


    saveMissing: true,
    saveMissingTo:"all",
    missingKeyHandler: (ng, ns, key, fallbackValue) => {
        console.log(ng, ns, key, fallbackValue)
    },

    // other options
    resources: {
        en: {
            translations: enTranslation,
        },
        fr: {
            translations: frTranslation,
        },
    },
    fallbackLng: 'en',
    ns: ['translations'],
    defaultNS: 'translations',

    interpolation: {
        formatSeparator: ',',
    },

    react: {
        wait: true,
    }


我想如果我从我的法语 .json 中删除了一个 key (以测试它是否有效),它会被记录下来,但它没有,只有当我删除这两个 key 时。

尝试过的其他解决方案:
1. "eslint-plugin-i18n-json"但它没有检查我需要什么,还没有找到正确的选项/配置
2. 选项 2。

您有任何链接或解决方案可以提供帮助吗? (涉及saas的除外)

最佳答案

您可以使用 https://github.com/godaddy/eslint-plugin-i18n-json#i18n-jsonidentical-keys .就是这样:

在你的 eslintrc 中添加:

module.exports = {
  extends: ["plugin:i18n-json/recommended"],
  rules: {
    "i18n-json/identical-keys": [
      2,
      {
        filePath: path.resolve("translations/en.json")
      }
    ]
  }
};

然后使用以下选项运行 eslint:
eslint --fix --ext .json --format node_modules/eslint-plugin-i18n-json/formatter.js translations/

这假设带有翻译的文件夹名为 translations (您应该调整路径)。

这是一个代码沙盒,证明它有效:https://codesandbox.io/s/friendly-minsky-9siu4

您可以使用终端运行 npm run lint您可以使用 translations/en.json 中的值和 translations/de.json检查它是如何工作的。

关于reactjs - i18next 警告或检查缺少语言的键(不是 fallbackLng),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60652848/

相关文章:

javascript - React-i18next,从 props 添加资源

android - 如何改变 native 布局方向?

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

android - 当软键盘出现时,如何防止 Android webview 中的固定页脚覆盖输入字段?

javascript - 值更改时触发服务

reactjs - 用 Jsx 放入 Textarea 按钮( react )

javascript - 不需要的 React 组件重新渲染?

javascript - 语法错误 : Cannot use import statement outside a module when start NodeJS from IntellijIDEA

Angular 国际化 ( ngx-translate/i18n OR i18next )

javascript - 与 i18next 相关的间歇性单元测试失败