vue-i18n icon indicating copy to clipboard operation
vue-i18n copied to clipboard

Default $tc plurialization in different language

Open mrleblanc101 opened this issue 3 years ago • 2 comments
trafficstars

Clear and concise description of the problem

Consider this translation:

"en": {
    "total": "0 results | {n} result | {n} results",
}

Here is the output: $tc('total', 0) => 0 results $tc('total', 1) => 1 result $tc('total', 2) => 2 results

If the count in $tc is undefined $tc('total', someVariable) => 1 result

Suggested solution

Undefined should be the same as 0.

Alternative

I could put a fallback value for every $tc, but this can be hard for nested object as Vue 2 doesn't accept optional chaining in template. {{ $tc('total', items.meta.total || 0) }}

Additional context

No response

Validations

mrleblanc101 avatar May 11 '22 21:05 mrleblanc101

There seem to be a pluralizationRules function which i could duplicate and override, but it seem like there should be a way to set the default index without overrider the function because I'm not trying to implement custom pluralization behavior.

Also the doc is not clear on how to override this function.

var defaultImpl = function (_choice, _choicesLength) {
    _choice = Math.abs(_choice);

    if (_choicesLength === 2) {
      return _choice
        ? _choice > 1
          ? 1
          : 0
        : 1
    }

    return _choice ? Math.min(_choice, 2) : 0
  };

mrleblanc101 avatar May 11 '22 21:05 mrleblanc101

From the docs:

The value of plural is handled with default 1.

But it doesn't make sense. Following this exemple from the docs.

  en: {
    apple: 'no apples | one apple | {count} apples'
  }

$tc("apple", undefined) => one apple but it would make much more sense with no apples as the default

The issue isn't just that it choose the second string, but also that it pass a value of 1 to {n} so following this exemple:

  en: {
    apple: '{n} apples | {n} apple | {n}  apples'
  }

$tc("apple", undefined) => 1 apple

mrleblanc101 avatar May 11 '22 21:05 mrleblanc101