在Node.js中出现了“ReferenceError: Intl未定义”的错误

7
我正在尝试在Node.js中构建一个new Intl.Collator()对象。
有人知道为什么在Node运行时中没有Intl对象吗?
根据MDN的说明,它被ECMAScript指定为命名空间,所以我不明白为什么它不在那里。

1
因为那个模块没有捆绑在Node中 :P - Henrik Andersson
1
为什么不呢?它不是捆绑在V8中吗? - user2958725
1
显然不行。因为 var Intl = require('Intl'); 不起作用,new Intl.Collator() 也不行。 - Henrik Andersson
2
https://github.com/joyent/node/issues/6371 - vkurchatkin
4个回答

6

很遗憾,截至撰写本文时(版本为0.10),Node当前不支持ECMA-402的Intl对象,除非您进行自定义编译,这在node.js Readme中有记录。

With libicu i18n support:

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

如果编译自定义版本的 Node 不可行或让您感到恐惧,可以使用 intl 模块 作为解决方法。这是一个 JavaScript polyfill,涵盖了大部分 EMCAScript 402 标准,但不包括 Intl.Collator。有关此限制的原因,请参阅项目的 Readme 文件。
使用该模块非常简单:
npm install intl --save

然后在你的 Node 代码中:

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

希望这可以帮到你。

是的,并且请参见上面提到的 https://github.com/joyent/node/issues/6371 票。这被认为是一项“少数使用”功能,但我想要努力使其默认启用。然后我们可以修改对这个问题的回答。 - Steven R. Loomis
1
这已经在Node 0.12和io.js 1.0中实现了。https://github.com/joyent/node/issues/7676#issuecomment-68811475 - Andrew Newdigate
另外,请注意Node不建议从Chromium构建ICU:https://github.com/nodejs/node/wiki/Intl 使用Chromium的ICU进行构建注意:不建议!此构建缺少一些区域设置,是旧版本,并且输出大小较大。这里记录仅为完整性考虑。 - noderman

1
Node.js 0.12 版本已经支持 Intl,但仅包含 ICU 区域设置的子集(例如:英语)。您需要使用完整 ICU 标志构建 Node.js(或任何您需要的子集)。有关 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.Collator。 对于这个,你真的需要依赖使用所需语言环境构建的Node 0.12。


更新:full-icu 模块将安装完整的数据,请参见 full-icu#3460 - Steven R. Loomis
添加以下代码是否安全?:Number.prototype.toLocaleString = function(locale) { return Intl.NumberFormat(locale).format(this); }; Date.prototype.toLocaleString = function(locale) { return Intl.DateTimeFormat(locale).format(this); }; - velop
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - noderman

1
自从io.js被合并到Node中,应该可以在新版本的Node中使用Intl(在io.js v3.1.0及以上版本中可用)。
  • intl:现在默认启用了使用small-icu的Intl支持(Steven R. Loomis)#2264
    • String#normalize()现在可用于unicode规范化。
    • Intl对象和各种String和Number方法存在,但仅支持英语区域设置。
    • 为了支持所有区域设置,必须使用full-icu构建节点。

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


0

React / Next JS 的解决方案

引用错误:Intl 未定义

这个错误是由于浏览器兼容性问题引起的。在这里查看相同问题。

错误发现于:移动设备、Android、MiuiBrowser 9.7.3

如果还有其他环境不在上述列表/浏览器兼容性表中,请在评论中添加。

解决方法:

使用 polyfill

import IntlPolyfill from "intl";

// import desired or required locale
import "intl/locale-data/jsonp/en";
import "intl/locale-data/jsonp/en-IN";

如果Intl未定义,则返回Polyfill的函数。
function getIntl() {
  if (typeof Intl !== "undefined") {
    return Intl;
  }

  return IntlPolyfill;
}

const CustomIntl = getIntl();

使用方法:

const number = 123456;

const enFormatted = new CustomIntl.NumberFormat("en").format(number);
console.log("enFormatted: ", enFormatted); // 123,456

const enINFormatted = new CustomIntl.NumberFormat("en-IN").format(number);
console.log("enINFormatted: ", enINFormatted); // 1,23,456

Edit intl-polyfill


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接