airbnb / polyglot.js

Give your JavaScript the ability to speak many languages.
http://airbnb.github.io/polyglot.js
BSD 2-Clause "Simplified" License
3.71k stars 208 forks source link

Pluralization - Support for multiple interpolation options #9

Closed kmudrick closed 8 years ago

kmudrick commented 10 years ago

The pluralization support seems really limited if it only supports a single "smart_count" interpolated value. The only other option as currently implemented seems to be to string multiple phrases together and concatenate them, which seems tedious and messy.

spikebrehm commented 10 years ago

Good point. I wonder what the best approach would be here. Maybe smart_count, smart_count_2, ..., smart_count_N?

ryanzec commented 10 years ago

Its been a while, any update on this issue as it would be better than doing multiple statements?

jasonkb commented 10 years ago

Then you'd need 9 translations for a Russian sentence with two counts in it =P

vwasteels commented 9 years ago

in french, 0 is singular, ex. "0 car", is there a way to achieve this with polyglot ?

adamhooper commented 8 years ago

This request, er, is not what anybody wants, ever :).

Let's say you're trying to translate this:

I bought 1 banana and 2 carrots

Then you should use phrases like this:

{
  "nCarrots": "%{smart_count} carrot |||| %{smart_count} carrots",
  "nBananas": "%{smart_count} banana |||| %{smart_count} bananas",
  "sentence": "I bought %{bananas} and %{carrots}"
}

And you'd call it like this:

var carrots = polyglot.t('nCarrots', 2);
var bananas = polyglot.t('nBananas', 1);
var sentence = polyglot.t('sentence', { bananas: bananas, carrots: carrots });

This is easier to translate, and it makes more sense than combinations. Think of your poor Arabic translator: would you honestly expect him/her to translate all 36 possible combinations of smart_count and smart_count_2?

spikebrehm commented 8 years ago

@adamhooper great approach! Closing this issue as that is the accepted way of handling this.