Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

feat: use Number.toLocaleString to support localization #216

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

pimlie
Copy link

@pimlie pimlie commented Jan 2, 2019

Resolves: #210

This PR add locale support by using Number.toLocaleString for number formatting.

Btw, as we discussed in the corresponding issue I did some work on adapting a polyfill for Number.toLocaleString but unfortunately I couldnt get it to fully work correctly in the time I had set for this (the polyfill works actually but when used as a dependency it doesnt download the correct cldr-data due to an issue in the cldr-data-npm package). As most (current?) browsers fully support number localization already I think the full-icu solution will work just fine :)

@jdalton
Copy link
Member

jdalton commented Jan 2, 2019

Thank you @pimlie!

It looks like related unit tests are failing. Could you double check them please.

@pimlie
Copy link
Author

pimlie commented Jan 2, 2019

Hi @jdalton,

PhantomJS doesnt seem to support toLocaleString (see their issue 12581, it just uses toString). Also the project is currently archived, so what solution do you prefer? Skip the numberFormat tests? Do a manual comparison on user-agent and use the old formatNumber code for phantomjs (and skip the numberFormat tests with a locale)?
The polyfill I adapted doesnt seem to work either when I hack it, it uses libraries which are not phantomjs compatible it seems.

Also I simplified the travis config a bit (although opinions may vary), it work nicely (except for the phantomjs issues) but before I 'finalize' that config maybe you are opposed against using npm scripts like this and prefer the old config?

@jdalton
Copy link
Member

jdalton commented Jan 2, 2019

Dropping phantom is fine 😄

@pimlie
Copy link
Author

pimlie commented Jan 2, 2019

Great thanks, even better 👍

Let me know if you have any other remarks!

return number[0].replace(/(?=(?:\d{3})+$)(?!\b)/g, ',') +
(number[1] ? '.' + number[1] : '');
function formatNumber(number, locale) {
return number.toLocaleString(locale);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a check during the creation of formatNumber that Number.prototype.toLocaleString is a function and if it's not then make formatNumber fallback to the legacy formatter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is like this ok or did you mean something else with during the creation of formatNumber?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll do!

@prantlf
Copy link

prantlf commented Jan 31, 2020

PhantomJS doesnt seem to support toLocaleString (see their issue 12581, it just uses toString).

You could switch to Puppeteer. With some additional test-refactoring effort, of course...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

Locale support to format numbers
3 participants