Cldr_Units v0.4.4 Cldr.Unit View Source
Supports the CLDR Units definitions which provide for the localization of many unit types.
The public API defines two primary functions:
Cldr.Unit.to_string/3
which, given a number and a unit name will output a localized stringCldr.Unit.available_units/0
identifies the available units for localization
Link to this section Summary
Functions
Returns the available styles for a unit localiation
Returns the available unit types for a given locale and style
Returns the available units for a given locale and style
Formats a number into a string according to a unit definition for a locale
Formats a list using to_string/3
but raises if there is
an error
Link to this section Functions
Returns the available styles for a unit localiation.
Example
iex> Cldr.Unit.available_styles
[:long, :short, :narrow]
Returns the available unit types for a given locale and style.
locale
is any configured locale. SeeCldr.known_locales()
. The default islocale: Cldr.get_current_locale()
style
is one of those returned byCldr.Unit.available_styles
. The current styles are:long
,:short
and:narrow
. The default isstyle: :long
Example
iex> Cldr.Unit.available_unit_types
[:acceleration, :angle, :area, :concentr, :consumption, :coordinate, :digital,
:duration, :electric, :energy, :frequency, :length, :light, :mass, :power,
:pressure, :speed, :temperature, :volume]
Returns the available units for a given locale and style.
locale
is any valid locale name returned byCldr.known_locale_names/0
or aCldr.LanguageTag
structstyle
is one of those returned byCldr.Unit.available_styles
. The current styles are:long
,:short
and:narrow
. The default isstyle: :long
Example
Cldr.Unit.available_units
[:acre, :acre_foot, :ampere, :arc_minute, :arc_second, :astronomical_unit, :bit,
:bushel, :byte, :calorie, :carat, :celsius, :centiliter, :centimeter, :century,
:cubic_centimeter, :cubic_foot, :cubic_inch, :cubic_kilometer, :cubic_meter,
:cubic_mile, :cubic_yard, :cup, :cup_metric, :day, :deciliter, :decimeter,
:degree, :fahrenheit, :fathom, :fluid_ounce, :foodcalorie, :foot, :furlong,
:g_force, :gallon, :gallon_imperial, :generic, :gigabit, :gigabyte, :gigahertz,
:gigawatt, :gram, :hectare, :hectoliter, :hectopascal, :hertz, :horsepower,
:hour, :inch, ...]
to_string(Cldr.Math.number_or_decimal(), atom(), Keyword.t()) :: {:ok, String.t()} | {:error, {atom(), binary()}}
Formats a number into a string according to a unit definition for a locale.
number
is any number (integer, float or Decimal)unit
is any unit returned byCldr.Unit.available_units/2
options
are:locale
is any valid locale name returned byCldr.known_locale_names/0
or aCldr.LanguageTag
structstyle
is one of those returned byCldr.Unit.available_styles
. The current styles are:long
,:short
and:narrow
. The default isstyle: :long
Any other options are passed to
Cldr.Number.to_string/2
which is used to format thenumber
Examples
iex> Cldr.Unit.to_string 123, :gallon
{:ok, "123 gallons"}
iex> Cldr.Unit.to_string 1, :gallon
{:ok, "1 gallon"}
iex> Cldr.Unit.to_string 1, :gallon, locale: "af"
{:ok, "1 gelling"}
iex> Cldr.Unit.to_string 1, :gallon, locale: "af-NA"
{:ok, "1 gelling"}
iex> Cldr.Unit.to_string 1, :gallon, locale: "bs"
{:ok, "1 galona"}
iex> Cldr.Unit.to_string 1234, :gallon, format: :long
{:ok, "1 thousand gallons"}
iex> Cldr.Unit.to_string 1234, :gallon, format: :short
{:ok, "1K gallons"}
iex> Cldr.Unit.to_string 1234, :megahertz
{:ok, "1,234 megahertz"}
iex> Cldr.Unit.to_string 1234, :megahertz, style: :narrow
{:ok, "1,234MHz"}
iex> Cldr.Unit.to_string 123, :megabyte, locale: "en", style: :unknown
{:error, {Cldr.UnknownFormatError, "The unit style :unknown is not known."}}
iex> Cldr.Unit.to_string 123, :blabber, locale: "en"
{:error, {Cldr.UnknownUnitError, "The unit :blabber is not known."}}
to_string!(List.t(), atom(), Keyword.t()) :: String.t() | Exception.t()
Formats a list using to_string/3
but raises if there is
an error.
Examples
iex> Cldr.Unit.to_string! 123, :gallon
"123 gallons"
iex> Cldr.Unit.to_string! 1, :gallon
"1 gallon"
iex> Cldr.Unit.to_string! 1, :gallon, locale: "af"
"1 gelling"