带有电单位(伏特、焦耳...)的 Intl.NumberFormat() 的单位参数无效

问题描述 投票:0回答:1

我正在尝试本地化我的 Web 应用程序,但无法使 Intl.NumberFormat 使用电力单位(安培、欧姆、伏特、焦耳...)。

文档中,他们提供了一些示例和可用单位列表

虽然我无法让它与电动装置一起工作:

// Working
console.log(new Intl.NumberFormat('fr', { style: 'unit', unit: 'second' }).format(1000));

// Failing with Invalid unit argument for Intl.NumberFormat() 'volt'
console.log(new Intl.NumberFormat('fr', { style: 'unit', unit: 'volt' }).format(1000));

有人知道为什么吗?

javascript localization
1个回答
4
投票

来自 MDN INTL

从完整列表中选择了单位的子集用于 ECMAScript。

Simple Unit
-----------
acre
bit
byte
celsius
centimeter
day
degree
fahrenheit
fluid-ounce
foot
gallon
gigabit
gigabyte
gram
hectare
hour
inch
kilobit
kilobyte
kilogram
kilometer
liter
megabit
megabyte
meter
mile
mile-scandinavian
milliliter
millimeter
millisecond
minute
month
ounce
percent
petabyte
pound
second
stone
terabit
terabyte
week
yard
year

成对的简单单位可以用“-per-”连接起来形成一个复合单位。没有默认值;如果样式为“unit”,则必须提供unit属性。

很酷:每秒兆字节在法语中变成了mégaoctets par Seconde

console.log(
  new Intl.NumberFormat('fr', 
    { style: 'unit', unit: 'megabyte-per-second', 'unitDisplay': 'long' }
).format(1000)
);

© www.soinside.com 2019 - 2024. All rights reserved.