Search code examples
javascriptinternationalizationcurrency

Why would Intl.NumberFormat with options be outputting a euro symbol on the left when the docs say it should be on the right?


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat#Using_options

The example in the docs:

var number = 123456.789;

// request a currency format
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// → 123.456,79 €

My output in Node 10:

> new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(123456.789)
'€ 123,456.79' 

Euro symbol on the right in the docs. On the left when I actually run the code. Do I have to set a locale or something? I'm in the US. But I'm actually asking for de-DE format, seems like that should override my locale. Running the exact code from the example made no difference.


Solution

  • Very likely you Node does not have full locale support for Intl, only English (small-icu)

    See https://nodejs.org/api/intl.html#intl_detecting_internationalization_support

    Try the "To check for support for a non-English locale" part.


    Solution (if you have small-icu, the most likely case)

    1. You can (of course) recompile (as the page linked above recommends)

    2. But you can use a shortcut, it might work and save you some time

      • Start node and use process.versions.icu (in my case is '62.1', for node v10.15.3)
      • Download the matching ICU sources (which contain the data file)
        The URL is something like http://download.icu-project.org/files/icu4c/<version>/icu4c-<version>-src.tgz.
        In my case http://download.icu-project.org/files/icu4c/62.1/icu4c-62_1-src.tgz
      • Unpack the ICU sources and find the icu data file. In my case the data file is at icu/source/data/in/icudt62l.dat. But the location might be different in other ICU versions (although I doubt, it is not impossible :-)
      • Set NODE_ICU_DATA to point to the date file. Either export NODE_ICU_DATA=<icu_data_path>/icudt62l.dat (define the variable) or env NODE_ICU_DATA=<icu_data_path>/icudt62l.dat node (define + run node)
        You would probably want to move the data file in a more "stable" place and remove ICU the sources

    It is pretty lame that Node does not provide versions with full ICU support ready to download.
    Or at least archived ICU data files matching the Node releases, so that we don't have to go through all this digging.