Cldr.Unit.validate_unit
validate_unit
, go back to Cldr.Unit module for more information.
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 directlyor 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.
Arguments
unit_name
is anatom()
ort:String
, supplied as is or as part of ant:Cldr.Unit
struct.
Returns
{:ok, canonical_unit_name, conversion}
wherecanonical_unit_name
is the normalized unit name andconversion
is an opaque structure used to convert this this unit into its base unit or{:error, {exception, reason}}
Notes
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.
Examples
iex> Cldr.Unit.validate_unit :meter
{
:ok,
: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",
{[
mile:
%Cldr.Unit.Conversion{
base_unit: [:meter],
factor: Ratio.new(905980129838867985, 562949953421312),
offset: 0
}
],
[
liter:
%Cldr.Unit.Conversion{
base_unit: [:cubic_meter],
factor: Ratio.new(1152921504606847, 1152921504606846976),
offset: 0
}
]}}