i18next复数键语法疑问及i18next-parser配置适配咨询
React项目中i18next复数翻译与i18next-parser配置问题
问题详情
我在React项目中集成i18next,使用react-i18next及i18next-parser生成包含当前翻译字符串的JSON文件,采用自然语言键。
源码写法
<p>{t('{{count}} Sensors', { count: getSensorCount() })}</p>
其中getSensorCount()返回数字。
i18next-parser配置
{ "locales": ["en", "fr", "es"], "output": "src/locales/$LOCALE/$NAMESPACE.json", "defaultValue": (locale, namespace, key, value) => value || key, "keySeparator": false, "namespaceSeparator": false, "verbose": true, "createOldCatalogs": false }
解析器生成的JSON键
运行解析器后,生成的JSON键如下:
{ ... "{{count}} Sensors_one": "{{count}} Sensors", "{{count}} Sensors_other": "{{count}} Sensors", ... }
我将{{count}} Sensors_one的值改为{{count}} Sensor,期望当count为1时返回"1 Sensor"——这和i18next文档描述的复数用法一致,但实际返回的是键名,似乎无法找到对应翻译。
只有当修改键为以下形式时,才能正常返回对应值:
"{{count}} Sensors": "{{count}} Sensor", "{{count}} Sensors_plural": "{{count}} Sensors",
疑问
- 复数键的正确语法是什么?
- 若正确语法为
key_one和key_other,如何让项目识别该格式? - 若正确语法为
key和key_plural,如何配置i18next-parser生成对应键?
包版本
"i18next": "^20.6.1", "i18next-parser": "^8.0.0", "react-i18next": "^13.0.0", "react": "^18.2.0"
内容的提问来源于stack exchange,提问作者sshakya
相关产品推荐
相关产品推荐

