You're seeing just the function validate_unit, go back to Cldr.Unit module for more information.
Link to this function


View Source

Validates a unit name and normalizes it,

A unit name can be expressed as:

  • an atom() in which case the unit must be localizable in CLDR directly

  • or a t:String in which case it is parsed into a list of composable subunits that can be converted but are not guaranteed to be output as a localized string.


  • unit_name is an atom() or t:String, supplied as is or as part of an t:Cldr.Unit struct.


  • {:ok, canonical_unit_name, conversion} where canonical_unit_name is the normalized unit name and conversion is an opaque structure used to convert this this unit into its base unit or

  • {:error, {exception, reason}}


A returned unit_name that is an atom is directly localisable (CLDR has translation data for the unit).

A unit_name that is a t:String is composed of one or more unit names that need to be resolved in order for the unit_name to be localised.

The difference is an implementation detail and should not be of concern to the user of this library.


iex> Cldr.Unit.validate_unit :meter
  [meter: %Cldr.Unit.Conversion{base_unit: [:meter], factor: 1, offset: 0}]

iex> Cldr.Unit.validate_unit "meter"
{:ok, :meter,
 [meter: %Cldr.Unit.Conversion{base_unit: [:meter], factor: 1, offset: 0}]}

iex> Cldr.Unit.validate_unit "miles_per_liter"
{:error, {Cldr.UnknownUnitError, "Unknown unit was detected at \"s\""}}

iex> Cldr.Unit.validate_unit "mile_per_liter"
{:ok, "mile_per_liter",
       base_unit: [:meter],
       factor:, 562949953421312),
       offset: 0
       base_unit: [:cubic_meter],
       factor:, 1152921504606846976),
       offset: 0