i18使用count的next复数返回了一个对象而不是字符串



尝试用i18next-react计算复数。我一定错过了一些非常明显的东西。我得到错误Results in key 'generic.rule-group (en)' returned an object instead of string.

提前感谢

resources.json

{
"en": {
"default": {
"generic.rule-group": {
"one": "Rule group",
"other": "Rule groups"
}
}
}
}

jsx

import React from 'react';
import { useTranslation } from 'react-i18next';

export const Test = () => {
const { t } = useTranslation()
// Results in key 'generic.rule-group (en)' returned an object instead of string.
return <h1>{t('generic.rule-group', { count: 1 })}</h1>
}

i18next-config.js

i18next
.use(initReactI18next)
.init({
ns,
fallbackNS: 'default',
defaultNS: ns,
lng: 'en',
fallbackLng: 'en',
resources,
keySeparator: false,
interpolation: {
escapeValue: false,
},
})
.catch(error => {
console.log(error);
});

根据文档,我认为资源json键应该是:

{
"en": {
"default": {
"generic.rule-group": "Rule group",
"generic.rule-group_plural": "Rule groups"
}
}
}
}

任何遇到此问题的人,请使用otherone而不是plural

{
"en": {
"default": {
"generic.rule-group": "Rule group",
"generic.rule-group_other": "Rule groups"
}
}
}
}

您可以验证您的密钥。如果您有嵌套的关键帧结构,那么传递精确的关键帧以避免此错误。例如,

"parent": {
"child1": "Child 1",
"child2": "Child 2"
}

然后将密钥作为"parent.child1""parent.child2"传递。

相关内容

最新更新