如何获得国家特定的格式规则?
How to get country specific formatting rules?
我目前正在为 vega lite API 构建一个工作流,并希望参数化语言环境的选择。
所以我希望用户通过提供 IETF 语言标签来指定她的国家代码,例如“us-En”或“fr-FR[=22” =]”,我想从中获取特定于国家/地区的格式设置规则(有关我需要的格式,请参见下文)。
例如,我正在寻找一个函数,它为不同的国家代码生成以下输出:
function getLocaleFormats(countryCode) {
if (countryCode === "de-DE") {
return {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["€", ""],
"dateTime": "%a %b %e %X %Y",
"date": "%d.%m.%Y",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
"shortDays": ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
"months": ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
"shortMonths": ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
};
}
}
如有任何帮助,我们将不胜感激。
您正在寻找内置的 Intl
对象:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/NumberFormat
在这些对象的实例上,您可以调用 resolvedOptions()
以获取有关在该语言环境中使用的格式的详细信息。
示例:
const nf = new Intl.NumberFormat('en-US');
const df = new Intl.DateTimeFormat('en-US');
const nfOptions = nf.resolvedOptions();
const dfOptions = df.resolvedOptions();
console.dir(nfOptions);
console.dir(dfOptions);
我找到了我需要的参考文献,它们在:
https://unpkg.com/d3-format@1/locale/${countryCode}.json
https://unpkg.com/d3-time-format@1/locale/${countryCode}.json
国家代码是 IETF 语言标签,如“us-En”。
p.s。我注意到这些格式规则在 d3.js 中指定,因为 vega lite 是建立在 d3.js 之上的,所以我在问题中添加了 d3.js 和 vega lite 标签。
我目前正在为 vega lite API 构建一个工作流,并希望参数化语言环境的选择。
所以我希望用户通过提供 IETF 语言标签来指定她的国家代码,例如“us-En”或“fr-FR[=22” =]”,我想从中获取特定于国家/地区的格式设置规则(有关我需要的格式,请参见下文)。
例如,我正在寻找一个函数,它为不同的国家代码生成以下输出:
function getLocaleFormats(countryCode) {
if (countryCode === "de-DE") {
return {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["€", ""],
"dateTime": "%a %b %e %X %Y",
"date": "%d.%m.%Y",
"time": "%H:%M:%S",
"periods": ["AM", "PM"],
"days": ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
"shortDays": ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
"months": ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
"shortMonths": ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
};
}
}
如有任何帮助,我们将不胜感激。
您正在寻找内置的 Intl
对象:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/NumberFormat
在这些对象的实例上,您可以调用 resolvedOptions()
以获取有关在该语言环境中使用的格式的详细信息。
示例:
const nf = new Intl.NumberFormat('en-US');
const df = new Intl.DateTimeFormat('en-US');
const nfOptions = nf.resolvedOptions();
const dfOptions = df.resolvedOptions();
console.dir(nfOptions);
console.dir(dfOptions);
我找到了我需要的参考文献,它们在:
https://unpkg.com/d3-format@1/locale/${countryCode}.json
https://unpkg.com/d3-time-format@1/locale/${countryCode}.json
国家代码是 IETF 语言标签,如“us-En”。
p.s。我注意到这些格式规则在 d3.js 中指定,因为 vega lite 是建立在 d3.js 之上的,所以我在问题中添加了 d3.js 和 vega lite 标签。