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

Support formatted numbers as strings for pluralization #59

Closed SebastianTimofte closed 8 years ago

SebastianTimofte commented 8 years ago

Hi guys,

First of all let me thank you for polyglot.js. It is a tool that we are using in our current applications are and quite fond of. So thank you for all your hard work.

However, in the last few days we've been encountering an error that we can't seem to go around without touching polyglot, meaning the ability to use formatted numbers as a smart_count for pluralization. We need this because we have to display the number in the correct user setup format, but in several cases still determine the plural form of the word based on it.

We would like to ask you if you guys are willing to include this feature or if you'd be ok with us sending a small pull request that would fix this.

Thank you for your answer, Sebastian

HamletDRC commented 8 years ago

Would it work to pass a parameter like this?

polyglot.t("num_cars", {smart_count: { count: 12000, value: '12k' }});
>> 12k Cars
polyglot.t("num_cars", {smart_count: { count: 1, value: '1' }});
>> 1 Car
ljharb commented 8 years ago

This seems like something we could support - basically if smart_count wasn't typeof number, use the object form.

SebastianTimofte commented 8 years ago

Epiphany moment... this change makes absolutely no sense since you can do

polyglot.extend({ "num_cars": "%{count} car |||| %{count} cars", });

And then

`polyglot.t("num_cars", {smart_count: 0, count: "Zero"}); => "Zero cars"

polyglot.t("num_cars", {smart_count: 1, count: "One"}); => "One car"

polyglot.t("num_cars", {smart_count: 2, count: "Two"}); => "Two cars"`

Nowhere does the plugin enforce using smart_count for interpolation as well...

Thanks Jordan for your help.

gregorybellencontre commented 4 years ago

I know it's closed for a while but I have a question relative to the value shortcut.

t('search:resultsCount', { smart_count: 12 }) Results to : %{count} results found.

t('search:resultsCount', 12) Results to same : %{count} results found

I need to pass both values (smart_count and count) explicitely to get the correct result. The documentation says it should work with the shortcut, so I don't understand...

The translation string is : %{count} result found |||| %{count} results found

Am I doing something wrong ?

ljharb commented 4 years ago

Based on the documentation, the phrase should be %{smart_count} result found |||| %{smart_count} results found for the shortcut to work. There's no magic mixing of count and smart_count.