i18next在缺少某种语言的键时发出警告或棉绒(不是fallbackLng)

问题描述 投票:2回答:1
到目前为止,[[我的项目正在使用

i18next和react-i18next)。不涉及本地化或服务器端。

当前设置:-默认和后备语言:en-其他语言:fr

我想自动化一些与错误检查有关的东西:1.检查是否在每个定义的语言文件中翻译了所有键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中删除了一个密钥(以测试它是否有效),它将被记录下来,但是只有当我删除两个密钥时,它才会被记录。 

[尝试其他解决方案:1. "eslint-plugin-i18n-json",但未检查我需要的内容,没有找到正确的选项/配置2.选项2.

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

reactjs i18next
1个回答
0
投票
您可以使用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.jsontranslations/de.json中的值进行播放以检查其工作方式。

© www.soinside.com 2019 - 2024. All rights reserved.