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

Intl.js on Node 4.2 #140

Closed
killercup opened this issue Oct 25, 2015 · 7 comments
Closed

Intl.js on Node 4.2 #140

killercup opened this issue Oct 25, 2015 · 7 comments

Comments

@killercup
Copy link

On one machine, I've recently updated from node 0.10 to node 4.2.1. Now, calling toLocaleString does not use the correct locale anymore. Is this a known issue with a newer node version?

You can find a demo of the issue here: https://gist.github.com/killercup/9880e1af193311cb4734

@caridy
Copy link
Collaborator

caridy commented Oct 26, 2015

node 4 supports Intl, which means the polyfill will opt-out automatically. The catch is that it comes with english locale only, and you need to setup node to enable the right locales for your app. Here is the doc: https://github.com/nodejs/node/wiki/Intl

@caridy caridy closed this as completed Oct 26, 2015
@killercup
Copy link
Author

So, that means I'll have to compile node myself instead of using the version apt/brew contains.

Intl.js' Readme currently says:

Node.js versions prior to 0.12 and ≥v3.1 don't provide the Intl APIs, so they require that the runtime is polyfilled.

You might want to update that 😄

@caridy
Copy link
Collaborator

caridy commented Oct 26, 2015

Anything related to Intl in node should be discussed here: https://github.com/nodejs/Intl/issues, I don't think you need to compile node, in most cases it is just a configuration option, plus we are working on using npm install <something> to enable other locales.

As for the readme, that is correct, node 0.12 also supports Intl, but that one will require you to compile node with extra options. The polyfill knows how to deal with 0.12 if you haven't compiled node.

@redbugz
Copy link

redbugz commented Apr 9, 2016

I'm having a similar issue, and I don't understand the response. The gist shows that that polyfill would be active because it is using a non-English locale.

Can you help me understand why this is? It's just doing the language polyfills, not the entire Intl object. The README doesn't really make it clear that additional steps are necessary for this to work.

Using the same code from the gist in my app:
Node 0.10.43, Intl 1.0.1:

421 337,66
421,337.66
421.337,66
2016-04-09 6:58:23 vm.
4/9/2016, 6:58:23 AM
9.4.2016, 6:58:23

Node 4.4.0, Intl 1.0.1:

421,337.66
421,337.66
421,337.66
4/9/2016, 6:59:39 AM
4/9/2016, 6:59:39 AM
4/9/2016, 6:59:39 AM

I also see that with Intl 1.0.1, the default locale appears to be "af-NA" instead of "en-US" like it is in Intl 0.1.4. Is that expected?

@twalker
Copy link

twalker commented Apr 9, 2016

I'm also not seeing localized output form the Date/Number prototypes (toLocaleString) when applying the polyfill. An unpleasant workaround, seems to be to polyfill the prototypes manually by calling IntlPolyfill.__applyLocaleSensitivePrototypes();

As illustrated here:
https://gist.github.com/twalker/d33941123b89fe28c059931679994673

It seems like a bug in Node.js. If Intl is implemented, I'd expect the Date/Number toLocaleString methods to also have been implemented. Based on this issue in node, if Node.js is compiled --with-intl=none, the prototypes are omitted.

@caridy
Copy link
Collaborator

caridy commented Apr 11, 2016

that gist is not polyfilling Data/Number in node because of line 4 (https://gist.github.com/twalker/d33941123b89fe28c059931679994673#file-intl-node-protoype-example-js-L4), that condition will be false since Intl is in fact available. You can relax that condition to not only check if Intl is available or not, but also testing if the default Intl supports all locales that your app should support, and you go from there. Alternative, if you know that the default Intl is not what you need, replace it entirely with the polyfill by doing global.Intl = global.IntlPolyfill, although that's not recommended.

@redbugz
Copy link

redbugz commented Apr 12, 2016

I'm talking about the original gist at the top of this issue, which follows the guidelines of the README and loads the polyfills for non-English locales.
The implication of the response to killercup above is that for Node 4+ we should compile our own Node and not use the polyfills, but the README suggests you can use the polyfills for missing locale data, but it doesn't work correctly.
I deploy my Node to a cloud provider and compiling my own Node is not possible, and deploying the additional ICU data might be possible but I'm not quite sure how to do that yet, so I was hoping to use the polyfills.
I can create a new issue if it's useful, to make sure the polyfills work as expected on Node 4+ if it's something that you feel should work. Otherwise the README should be updated with additional information if that the polyfills won't handle certain scenarios. I don't understand how they work well enough to know, so I'm trying to get some guidance on how to proceed.

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

No branches or pull requests

4 participants