繁体   English   中英

ReferenceError:Intl未在Node.js中定义

[英]ReferenceError: Intl is not defined in Node.js

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

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

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

不幸的是,节点当前(从版本0.10开始,在编写时)不支持ECMA-402 Intl对象,除非您执行node.js 自述文件中记录的节点的自定义编译。

有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 

make install

如果编译节点的自定义构建不是一个选项或者这个想法充满了恐惧,解决方法是使用intl模块 ,一个Javascript polyfill,涵盖了大部分的EMCA-402标准,除了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:现在默认情况下在build(Steven R. Loomis) #2264中启用使用small-icu的Intl支持。
    • String#normalize()现在可用于unicode规范化。
    • Intl对象和各种String和Number方法存在,但仅支持英语区域设置。
    • 要支持所有语言环境,必须使用full-icu构建节点。

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

节点0.12包括对Intl的支持,但它只带有一部分ICU语言环境(即:英语)。 您需要为完整ICU(或您需要的任何子集)构建带有标志的Node。 ICU构建的详细说明如下: https//github.com/nodejs/node/wiki/Intl

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

尤其是Intl 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。 对于这个,你真的需要依赖于使用你所需的语言环境构建的Node 0.12。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM