kazupon / vue-i18n

:globe_with_meridians: Internationalization plugin for Vue.js
https://kazupon.github.io/vue-i18n/
MIT License
7.25k stars 859 forks source link

[feature request] Handle booleans like pluralization #722

Open Soviut opened 4 years ago

Soviut commented 4 years ago

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
exoego commented 4 years ago

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.

robinsondotnet commented 4 years ago

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?

Soviut commented 4 years ago

@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.

Topograph commented 1 year ago

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