首页 > 解决方案 > 如何声明自定义复数规则 vue-i18n Vue 3?

问题描述

我有一条来自 https://kazupon.github.io/vue-i18n/guide/pluralization.html#accessing-the-number-via-the-pre-defined-argument的复数规则, 但以形式声明

setup() {
    const { t, locale } = useI18n({
      pluralizationRules: {
        ru: function (choice, choicesLength) {
          if (choice === 0) {
            return 0;
          }

          const teen = choice > 10 && choice < 20;
          const endsWithOne = choice % 10 === 1;

          if (choicesLength < 4) {
            return !teen && endsWithOne ? 1 : 2;
          }
          if (!teen && endsWithOne) {
            return 1;
          }
          if (!teen && choice % 10 >= 2 && choice % 10 <= 4) {
            return 2;
          }

          return choicesLength < 4 ? 2 : 3;
        },
      },
    });
    return { t, locale };
  },

不会改变任何东西(即,0 - секунд,1 - секунда,其余为 секунд)

我需要

...1 секунда

...2-...3-...4 секунды

...0-...5-...6-...7-...8-...9 секунд

<i18n>
{
  "en": {
    "seconds":"{count} seconds | {count} second | {count} seconds"
  },
  "ru":{
    "seconds":"{count} секунд | {count} секунда | {count} секунд"
  }
}
</i18n>

标签: vue.jsinternationalizationvue-i18n

解决方案


我很快查看了 vue-i18n 的来源,并注意到两者都有pluralRules并且pluralizationRules具有相同的类型。对我来说,它使用pluralRules而不是pluralizationRules.


推荐阅读