如何查看给定的Node.js版本支持哪些语言环境,以及如何启用缺少的语言环境? [英] How to see what locales a given Node.js version supports and how to enable missing locales?
问题描述
我在Windows 8.1上的Node.js版本是:
My Node.js version on Windows 8.1 is:
$ node -v
v5.3.0
但是似乎它不支持语言环境识别和协商.我指的是 ECMAScript国际化API 的支持.仅支持en
语言环境.这是浏览器和Node.js中的示例.在浏览器中,可以确定一个区域设置就可以了:
But it seems it doesn't support locale identification and negotiation. I mean the support of ECMAScript Internationalization API. Only en
locale is supported. Here is an example in a browser and in Node.js. In a browser a locale is identified just fine:
// en
> Intl.NumberFormat('en', {currency: 'USD', style:"currency"}).format(300)
> "$300.00"
// ru
> Intl.NumberFormat('ru', {currency: 'USD', style:"currency"}).format(300)
> "300,00 $"
但是在Node.js中,它不起作用. Node.js为en
和ru
返回相同的en
格式:
But in Node.js it doesn't work. Node.js returns the same en
format for both en
and ru
:
// en
> Intl.NumberFormat('en', {currency: 'USD', style:"currency"}).format(300)
'$300.00'
// ru
> Intl.NumberFormat('ru', {currency: 'USD', style:"currency"}).format(300)
'$300.00'
是否可以查看给定的Node.js支持哪些语言环境,以及如何启用所需的语言环境?
Is there a way to see what locales does a given Node.js support and how can I enable desired locales?
推荐答案
根据 https://github.com/andyearnshaw/Intl.js/是一个nodejs模块,称为
According to https://github.com/andyearnshaw/Intl.js/ there is a nodejs module, called
intl-locales-supported
intl-locales-supported
显示是否支持语言环境.
which shows if a locale is supported.
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 patch the constructors we need with the polyfill's.
var IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
}
} else {
// No `Intl`, so use and load the polyfill.
global.Intl = require('intl');
}
这篇关于如何查看给定的Node.js版本支持哪些语言环境,以及如何启用缺少的语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!