为什么 Intl.NumberFormat with options 会在左边输出一个欧元符号,而文档说它应该在右边?

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

文档中的示例:

var number = 123456.789;

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

我在节点 10 中的输出:

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

文档右侧的欧元符号。左边的时候我居然是运行的代码。我必须设置语言环境还是什么?我在美国。但我实际上要求的是 de-DE 格式,这似乎应该覆盖我的语言环境。 运行 示例中的确切代码没有区别。

很可能您的 Node 没有对 Intl 的完整语言环境支持,只有英语 (small-icu)

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

尝试 "To check for support for a non-English locale" 部分。


解决方案(如果你有small-icu,最有可能的情况)

  1. 您可以(当然)重新编译(如上面链接的页面所推荐)

  2. 但您可以使用 快捷方式,它可能会工作并节省您一些时间

    • 启动节点并使用process.versions.icu (在我的例子中是 '62.1',对于节点 v10.15.3
    • 下载匹配的 ICU 源(包含数据文件)
      URL 类似于 http://download.icu-project.org/files/icu4c/<version>/icu4c-<version>-src.tgz.
      就我而言 http://download.icu-project.org/files/icu4c/62.1/icu4c-62_1-src.tgz
    • 解压 ICU 源并找到 icu 数据文件。在我的例子中,数据文件位于 icu/source/data/in/icudt62l.dat。但在其他 ICU 版本中位置可能不同(虽然我怀疑,但并非不可能:-)
    • 设置NODE_ICU_DATA指向日期文件。 export NODE_ICU_DATA=<icu_data_path>/icudt62l.dat(定义变量)或env NODE_ICU_DATA=<icu_data_path>/icudt62l.dat node(定义+运行节点)
      您可能希望将数据文件移动到更 "stable" 的位置并删除 ICU 源

Node 不提供完整的 ICU 支持版本可供下载,这很蹩脚。
或者至少存档与 Node 版本匹配的 ICU 数据文件,这样我们就不必进行所有这些挖掘。