Search code examples
javascriptlocalization

Invalid unit argument for Intl.NumberFormat() with electric units (volt, joule...)


I am trying to localize my web application and I cannot manage to make Intl.NumberFormat work with electric units (ampere, ohm, volt, joule...).

In the documentation, they provide some examples and the list of units available.

Though I cannot manage to make it work with the electric units:

// 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));

Does someone have an idea why?


Solution

  • From MDN INTL

    A subset of units from the full list was selected for use in 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
    

    Pairs of simple units can be concatenated with "-per-" to make a compound unit. There is no default value; if the style is "unit", the unit property must be provided.

    Cool: Megabytes per second becomes mégaoctets par seconde in French

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