Support plurals with floating point numbers
See original GitHub issueI want to translate messages such as “3.5 stars”. But __n converts every number to an integer using parseInt, so regardless of whether I use %d or %f, I get “3 stars” instead of “3.5 stars”.
The make-plurals
module supports floats just fine, e.g., it returns “one” in French and “other” in English for the number 1.5.
If we change parseInt(count, 10) to Number(count) in these two locations:
https://github.com/mashpie/i18n-node/blob/b9fb21a3e17081e3a1519ded5ae43e49f72e1758/i18n.js#L358 https://github.com/mashpie/i18n-node/blob/b9fb21a3e17081e3a1519ded5ae43e49f72e1758/i18n.js#L538
it solves the problem, but note that the second conversion may not be necessary at all since we’re already checking for the number
type in the if clause.
parseInt is a bit more lenient than Number, e.g., it would be fine with “50px” whereas Number would convert that to NaN
. If you need that leniency, as far as I can tell, Number(count) || parseInt(count, 10) should work (“0” will just fall back to parseInt).
Let me know what you think & how you’d like to proceed; happy to submit a PR.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top GitHub Comments
Here you go - released as
0.9.0
to npm. @eloquence thanks for pointing out, @einfallstoll thanks for testing 😃stay safe!
Updated my dependency and reverted my workaround. Nice!