question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support plurals with floating point numbers

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mashpiecommented, Apr 17, 2020

Here you go - released as 0.9.0 to npm. @eloquence thanks for pointing out, @einfallstoll thanks for testing 😃

stay safe!

0reactions
einfallstollcommented, Apr 17, 2020

Updated my dependency and reverted my workaround. Nice!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Plurals for float values - string.format - Stack Overflow
The general suggestion seems to be rounding / processing the float values manually (e.g checking whether the float value matches 1.0) and then ......
Read more >
PluralRules - Android Developers
Parses a plural rules description and returns a PluralRules. String · select(double number). Given a floating-point number, returns the keyword of the first ......
Read more >
Plural forms (GNU gettext utilities)
But when the number is not explicit, the distinction between singular and plural exists: “the apple” is “az alma”, and “the apples” is...
Read more >
plural - Simple Go API for Pluralisation. - Go Packages
Package plural provides simple support for localising plurals in a ... It can handle integers and floating point numbers equally and this ...
Read more >
What numbers (e.g. 0, -1, or 1.0) are plurals in Latin?
In some programming languages there is a type difference so that 1 is an integer and 1.0 is a floating point number, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found