neherlab / covid19_scenarios

Models of COVID-19 outbreak trajectories and hospital demand
https://covid19-scenarios.org
MIT License
1.36k stars 352 forks source link

Internationalize and humanize numbers (at the same time) #198

Closed ivan-aksamentov closed 4 years ago

ivan-aksamentov commented 4 years ago

๐Ÿ™‹ Feature Request

Now that the numbers are displayed in a human-readable form, would be nice to also make them internationalized.

๐Ÿ”ฆ Context

๐Ÿ˜ฏ Describe the feature

It is hard to describe all the requirements specifically, without knowing the languages. This should be covered by a library.

๐Ÿ’ป Examples

Here is an non-exhaustive list of requirements:

๐Ÿ’ Possible Solution

A mature library should be used. Either D3.js currently used or number formatting, or react-i18next used for i18n or both.

Related:

gj262 commented 4 years ago

Web standard Intl.NumberFormat would be good. Probably { notation: "compact" , compactDisplay: "short" }. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

const number = 12345678910;

console.log(new Intl.NumberFormat('en-GB', { notation: "compact" , compactDisplay: "short" }).format(number));
console.log(new Intl.NumberFormat('en-GB', { notation: "compact" , compactDisplay: "long" }).format(number));

console.log(new Intl.NumberFormat('de-DE', { notation: "compact" , compactDisplay: "short" }).format(number));
console.log(new Intl.NumberFormat('de-DE', { notation: "compact" , compactDisplay: "long" }).format(number));

console.log(new Intl.NumberFormat('ru-RU', { notation: "compact" , compactDisplay: "short" }).format(number));
console.log(new Intl.NumberFormat('ru-RU', { notation: "compact" , compactDisplay: "long" }).format(number));

Gives:

> "12B"
> "12 billion"
> "12 Mrd."
> "12 Milliarden"
> "12 ะผะปั€ะด"
> "12 ะผะธะปะปะธะฐั€ะดะพะฒ"
gj262 commented 4 years ago

As @rcbevans points out in #210, this issue is not fixed and the 'humanized' totals are overly rounded!