ReferenceError:Intl没有在Node.js中定义

我试图在Node.js中构造一个new Intl.Collator()对象。

有谁知道为什么Intl对象不会出现在节点运行时?

根据MDN ,它被ECMAScript指定为命名空间,所以我不明白为什么它不会在那里。

不幸的是,除非您执行node.js 自述文件中logging的节点的自定义编译,否则当前节点(截止于版本0.10时)不支持ECMA-402 Intl对象。

利用libicu i18n支持:

 svn checkout --force --revision 214189 \ http://src.chromium.org/svn/trunk/deps/third_party/icu46 \ deps/v8/third_party/icu46 ./configure --with-icu-path=deps/v8/third_party/icu46/icu.gyp make 

进行安装

如果编译自定义的节点构build不是一个选项,或者这个想法让你满意,一个解决方法就是使用intl模块 ,一个覆盖EMCA-402标准的Intl.Collator ,除了Intl.Collator ,原因项目自述文件中涵盖了这些内容。

使用模块非常简单:

 npm install intl --save 

然后在你的节点代码中:

 var Intl = require('intl'); console.log(new Intl.NumberFormat("de-DE").format(12345678)); 

希望这可以帮助。

由于io.js被合并到Node中,所以应该可以在更新版本的Node中使用Intl(可从v3.1.0的io.js中获得)。

  • intl:使用small-icu的Intl支持现在默认在构build中启用(Steven R. Loomis) #2264 。
    • string#normalize()现在可以用于unicode标准化。
    • Intl对象和各种String和Number方法都存在,但只支持英文语言环境。
    • 为了支持所有的语言环境,必须使用full-icu来构build节点。

https://github.com/nodejs/node/blob/master/CHANGELOG.md#2015-08-18-version-310-fishrock123

节点0.12已经包含了对Intl的支持,但它只带有ICU区域设置的一个子集(即:英文)。 您需要为完整的ICU(或您需要的任何子集)构build带有标志的节点。 ICU的详细说明请参阅https://github.com/nodejs/node/wiki/Intl

我build议阅读FormatJS文档: http ://formatjs.io/

特别是国际Polyfill

https://github.com/andyearnshaw/Intl.js

 var areIntlLocalesSupported = require('intl-locales-supported'); var localesMyAppSupports = [ /* list locales here */ ]; if (global.Intl) { // Determine if the built-in `Intl` has the locale data we need. if (!areIntlLocalesSupported(localesMyAppSupports)) { // `Intl` exists, but it doesn't have the data we need, so load the // polyfill and replace the constructors we need with the polyfill's. require('intl'); Intl.NumberFormat = IntlPolyfill.NumberFormat; Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat; } } else { // No `Intl`, so use and load the polyfill. global.Intl = require('intl'); } 

Intl.js不会(也不会)实现Intl.Collat​​or。 对于这一个,你真的需要依靠你的所需的语言环境build立的节点0.12。