i18next 复数使用计数返回一个对象而不是字符串

问题描述 投票:0回答:2

试图用

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);
  });
reactjs i18next react-i18next
2个回答
1
投票

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

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

0
投票

遇到这个问题的任何人,请使用

other
one
而不是
plural

  {
    "en": {
       "default": {
       "generic.rule-group": "Rule group",
       "generic.rule-group_other": "Rule groups"
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.