Issue with react i18next singular and plural, always return plural value

huangapple go评论116阅读模式
英文:

Issue with react i18next singular and plural, always return plural value

问题

I have some confusion with i18next. When I use the English language, it's completely correct. However, when I change the language to Chinese (zh), it always returns a plural value even when the value is singular.

I've tried upgrading dependencies and changing the configuration, but it still doesn't work.

Here's your provided sample code:

i18n
  .use(Backend)
  .use(initReactI18next)
  .init({
    ns: ["common"],
    defaultNS: "common",
    resources,
    fallbackNS: "en",
    supportedLngs: ["en", "zh"],
    interpolation: {
      escapeValue: false
    },
    react: {
      bindI18n: "loaded languageChanged"
    },
    returnEmptyString: false,
    nsSeparator: false
  });

export default i18n;

Dependencies version:

i18next             21.3.1
intl-pluralrules    1.3.1
react               17.0.0
react-i18next       11.12.0
英文:

I have some confuse situation with i18next. when i use english language it totally correct. but when I change language to zh, It always return plural value event the value is singular.

I try to upgrade dependencies, change config, but still not working.

I have provided a sample code in the following link.

https://codesandbox.io/s/react-i18next-plurals-forked-jgulw6?file=/public/locales/zh/translation.json

Config

i18n
  .use(Backend)
  .use(initReactI18next)
  .init({
    ns: ["common"],
    defaultNS: "common",
    resources,
    fallbackNS: "en",
    supportedLngs: ["en", "zh"],
    interpolation: {
      escapeValue: false
    },
    react: {
      bindI18n: "loaded languageChanged"
    },
    returnEmptyString: false,
    nsSeparator: false
  });

export default i18n;

Dependencies version:

i18next             21.3.1
intl-pluralrules    1.3.1
react               17.0.0
react-i18next.      11.12.0

答案1

得分: 1

这不是一个错误,而是一个特性

不同语言(区域)有不同的适用复数后缀。

您可以只使用允许的后缀或i18n回退到另一个(如果存在的话)命名空间。

阅读 https://www.i18next.com/translation-function/plurals
并使用(由i18next提供,您可以在上一页找到链接) https://jsfiddle.net/6bpxsgd4 来检查您所需语言(区域)支持的后缀。

例如:

  • 英语(en):_one,_other;
  • 中文(zh):_other;
  • 俄语(ru):_one,_few,_many。

所以当您提供翻译键时,需要记住这一点。
当我尝试为“en”和“ru”使用“month_one”和“month_other”时,我也遇到了类似的问题:

Issue with react i18next singular and plural, always return plural value

这是因为“ru”不支持“_other”后缀。当我只使用“month”和“month_one”键时,问题得以解决。

对于您的情况 - 中文(zh)仅支持“_other”后缀(或者干脆不使用后缀):

{
  "test": "測試",
  "month_one": "{{ count }} 月" // 不起作用,因为"zh"没有"_one"后缀
  "month_other": "{{ count }} 月s" // 或者"month": "{{ count }} 月s"
}
英文:

It's not a bug, but a feature

Different languages (locales) have different applicable plural suffixes.

You can use only allowed suffixes or i18n fallbacks to another (if existed) namespace.

Read https://www.i18next.com/translation-function/plurals
and use (provided by i18next, you can find the link on previous page) https://jsfiddle.net/6bpxsgd4 to check what suffixes your desired language (locale) supports.

For example:

  • English (en): _one, _other;
  • Chinese (zh): _other;
  • Russian (ru): _one, _few, _many.

So you need to keep it in mind, when you provide translation keys.
I also encountered similar problem when I tried to use "month_one" and "month_other" for "en" and "ru":

Issue with react i18next singular and plural, always return plural value

All because "ru" don't support "_other" suffix. The problem is solved when I used just "month" and "month_one" keys.

For you case - Chinese (zh) only supports "_other" suffix (or just dont use suffix at all):

{
  "test": "測試",
  "month_one": "{{ count }} 月" // won't work, because there is no "_one" for "zh"
  "month_other": "{{ count }} 月s" // or "month": "{{ count }} 月s"
}

huangapple
  • 本文由 发表于 2023年4月13日 18:04:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定