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

[feature request] Handle booleans like pluralization

Open Soviut opened this issue 6 years ago • 4 comments

Pluralization allows simple shortcut syntax like

{
  car: 'car | cars'
}

Which can be called with

<p>0: {{ $tc('car', 0) }}</p>
<p>1: {{ $tc('car', 1) }}</p>
<p>2: {{ $tc('car', 2) }}</p>

Which produces

0: cars
1: car
2: cars

I would like a similar shorthand for booleans where the first item is true and the second item is false

{
  validity: 'valid | invalid'
}
<p>true: {{ $tc('validity', true) }}</p>
<p>false: {{ $tc('validity', false) }}</p>
true: valid
false: invalid

Soviut avatar Oct 03 '19 20:10 Soviut

I think $tc should focus for pluralization purpose. Overloading for boolean complicates $tc in terms of both usage and implementation.

Switching translation by boolean seems to have many use case (e.g. valid/invalid, enabled/disabled, full/vacant). So I think adding new method for boolean , like $tb or something, might be handy.

exoego avatar Oct 03 '19 21:10 exoego

Is this feature still wanted? I would like to try to submit a PR for this feature issue. It looks useful. This feature would only works with true or false, right? What should be the behavior when null or undefined is passed? Should we support a fallback message (third value option) for that case?

robinsondotnet avatar Jan 13 '20 21:01 robinsondotnet

@robinsondotnet I'd definitely still like this feature and can help you with the review if you want.

The basic concept is that it would work for things like (on, off), (yes, no), (valid, invalid), (visible, hidden), etc.

Regarding null, I'd say that null, false, undefined and any other falsey values should yield the "false" value.

Similarly, I would consider the false condition to be the fallback.

Soviut avatar Jan 13 '20 22:01 Soviut

my workaround for this situation is

{
  "validity": "invalid | valid"  // false text first
}
t('validity', +isValid + 1) 

the unary + operator turns false to 0 and true to 1. by adding one the result is either 1 or 2

Topograph avatar May 24 '23 08:05 Topograph