Bonfire.Common.Localise.Cldr.Unit (Bonfire v1.0.0-social-rc.3.22)
View SourceSupports the CLDR Units definitions which provide for the localization of many unit types.
Summary
Functions
See Cldr.Unit.Math.add/2.
See Cldr.Unit.Math.add!/2.
Returns the localized display name for a unit.
See Cldr.Unit.Math.div/2.
See Cldr.Unit.Math.div!/2.
Localizes a unit according to the current processes locale and backend.
Localizes a unit according to a territory
See Cldr.Unit.Math.mult/2.
See Cldr.Unit.Math.mult!/2.
Parse a string to create a new unit.
Parse a string to create a new unit or raises an exception.
Parse a string to find a matching unit-atom.
Parse a string to find a matching unit-atom.
Returns a list of the preferred units for a given unit, locale, use case and scope.
Returns a list of the preferred units for a given unit, locale, use case and scope.
See Cldr.Unit.Math.round/1.
See Cldr.Unit.Math.round/2.
See Cldr.Unit.Math.round/3.
See Cldr.Unit.Math.sub/2.
See Cldr.Unit.Math.sub!/2.
Formats a number into an iolist according to a unit definition for a locale.
Formats a unit using to_iolist/3 but raises if there is
an error.
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.
Functions
See Cldr.Unit.Math.add/2.
See Cldr.Unit.Math.add!/2.
@spec display_name(Cldr.Unit.translatable_unit() | Cldr.Unit.t(), Keyword.t()) :: String.t() | {:error, {module(), binary()}}
Returns the localized display name for a unit.
The returned text is generally suitable for including in UI elements such as selection boxes.
Arguments
unitis anyt:Cldr.Unitor any unit name returned byCldr.Unit.known_units/0.optionsis a keyword list of options.
Options
:localeis any valid locale name returned byCldr.known_locale_names/0or aCldr.LanguageTagstruct. The default isCldr.get_locale/0.:styleis one of those returned byCldr.Unit.available_styles. The current styles are:long,:shortand:narrow. The default isstyle: :long.
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.display_name :liter
"liters"
iex> Bonfire.Common.Localise.Cldr.Unit.display_name :liter, locale: "fr"
"litres"
iex> Bonfire.Common.Localise.Cldr.Unit.display_name :liter, locale: "fr", style: :short
"l"
See Cldr.Unit.Math.div/2.
See Cldr.Unit.Math.div!/2.
@spec localize(Cldr.Unit.t()) :: [Cldr.Unit.t(), ...] | {:error, {module(), String.t()}}
Localizes a unit according to the current processes locale and backend.
The current process's locale is set with
Cldr.put_locale/1.
See Cldr.Unit.localize/3 for further
details.
@spec localize(Cldr.Unit.t(), Keyword.t()) :: [Cldr.Unit.t(), ...] | {:error, {module(), String.t()}}
Localizes a unit according to a territory
A territory can be derived from a t:Cldr.Locale.locale_name
or t:Cldr.LangaugeTag.
Use this function if you have a unit which
should be presented in a user interface using
units relevant to the audience. For example, a
unit #Cldr.Unit100, :meter> might be better
presented to a US audience as #Cldr.Unit<328, :foot>.
Arguments
unitis any unit returned byCldr.Unit.new/2optionsis a keyword list of options
Options
:localeis any valid locale name returned byCldr.known_locale_names/0or aCldr.LanguageTagstruct. The default isbackend.get_locale/0:territoryis any valid territory code returned byCldr.known_territories/0. The default is the territory defined as part of the:locale. The option:territoryhas a precedence over the territory in a locale.:usageis the way in which the unit is intended to be used. The availableusagevaryies according to the unit category. SeeCldr.Unit.preferred_units/3.
Examples
iex> unit = Cldr.Unit.new!(1.83, :meter)
iex> Bonfire.Common.Localise.Cldr.Unit.localize(unit, usage: :person_height, territory: :US)
[
Cldr.Unit.new!(:foot, 6, usage: :person_height),
Cldr.Unit.new!(:inch, "0.04724409448818897637795275598", usage: :person_height)
]
See Cldr.Unit.Math.mult/2.
See Cldr.Unit.Math.mult!/2.
See Cldr.Unit.new/2.
See Cldr.Unit.new!/2.
Parse a string to create a new unit.
This function attempts to parse a string
into a number and unit type. If successful
it attempts to create a new unit using
Cldr.Unit.new/3.
The parsed unit type is aliased against all the
known unit names for a give locale (or the current
locale if no locale is specified). The known
aliases for unit types can be returned with
MyApp.Cldr.Unit.unit_strings_for/1 where MyApp.Cldr
is the name of a backend module.
Arguments
unit stringis any string to be parsed and if possible used to create a newt:Cldr.Unitoptionsis a keyword list of options
Options
:localeis any valid locale name returned byCldr.known_locale_names/0or at:Cldr.LanguageTagstruct. The default isCldr.get_locale/0
Returns
{:ok, unit}or{:error, {exception, reason}}
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.parse "1kg"
Cldr.Unit.new(1, :kilogram)
iex> Bonfire.Common.Localise.Cldr.Unit.parse "1 tages", locale: "de"
Cldr.Unit.new(1, :day)
iex> Bonfire.Common.Localise.Cldr.Unit.parse "1 tag", locale: "de"
Cldr.Unit.new(1, :day)
iex> Bonfire.Common.Localise.Cldr.Unit.parse("42 millispangels")
{:error, {Cldr.UnknownUnitError, "Unknown unit was detected at \"spangels\""}}
Parse a string to create a new unit or raises an exception.
This function attempts to parse a string
into a number and unit type. If successful
it attempts to create a new unit using
Cldr.Unit.new/3.
The parsed unit type is un-aliased against all the
known unit names for a give locale (or the current
locale if no locale is specified). The known
aliases for unit types can be returned with
MyApp.Cldr.Unit.unit_strings_for/1 where MyApp.Cldr
is the name of a backend module.
Arguments
unit stringis any string to be parsed and if possible used to create a newt:Cldr.Unitoptionsis a keyword list of options
Options
:localeis any valid locale name returned byCldr.known_locale_names/0or at:Cldr.LanguageTagstruct. The default isCldr.get_locale/0
Returns
unitorraises an exception
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.parse! "1kg"
Cldr.Unit.new!(1, :kilogram)
iex> Bonfire.Common.Localise.Cldr.Unit.parse! "1 tages", locale: "de"
Cldr.Unit.new!(1, :day)
iex> Bonfire.Common.Localise.Cldr.Unit.parse!("42 candela per lux")
Cldr.Unit.new!(42, "candela per lux")
iex> Bonfire.Common.Localise.Cldr.Unit.parse!("42 millispangels")
** (Cldr.UnknownUnitError) Unknown unit was detected at "spangels"
Parse a string to find a matching unit-atom.
This function attempts to parse a string and
extract the unit type.
The parsed unit type is aliased against all the
known unit names for a give locale (or the current
locale if no locale is specified). The known
aliases for unit types can be returned with
MyApp.Cldr.Unit.unit_strings_for/1 where MyApp.Cldr
is the name of a backend module.
Arguments
unit_name_stringis any string to be parsed and converted into aunit typeoptionsis a keyword list of options
Options
:localeis any valid locale name returned byCldr.known_locale_names/0or at:Cldr.LanguageTagstruct. The default isCldr.get_locale/0:backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend!/0.:onlyis a unit category or unit, or a list of unit categories and units. The parsed unit must match one of the categories or units in order to be valid. This is helpful when disambiguating parsed units. For example, parsing "w" could be either:wattor:weeks. Specifyingonly: :durationwould return:weeks. Specifyingonly: :powerwould return:watt:exceptis the oppostte of:only. The parsed unit must not match the specified unit or category, or unit categories and units.
Returns
{:ok, unit_name}or{:error, {exception, reason}}
Notes
When both
:onlyand:exceptoptions are passed, both conditions must be true in order to return a parsed result.Only units returned by
Cldr.Unit.known_units/0can be used in the:onlyand:exceptfilters.
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name "kg"
{:ok, :kilogram}
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name "w"
{:ok, :watt}
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name "w", only: :duration
{:ok, :week}
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name "m", only: [:year, :month, :day]
{:ok, :month}
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name "tages", locale: "de"
{:ok, :day}
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name "tag", locale: "de"
{:ok, :day}
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name("millispangels")
{:error, {Cldr.UnknownUnitError, "Unknown unit was detected at \"spangels\""}}
Parse a string to find a matching unit-atom.
This function attempts to parse a string and
extract the unit type.
The parsed unit type is aliased against all the
known unit names for a give locale (or the current
locale if no locale is specified). The known
aliases for unit types can be returned with
MyApp.Cldr.Unit.unit_strings_for/1 where MyApp.Cldr
is the name of a backend module.
Arguments
unit_name_stringis any string to be parsed and converted into aunit typeoptionsis a keyword list of options
Options
:localeis any valid locale name returned byCldr.known_locale_names/0or at:Cldr.LanguageTagstruct. The default isCldr.get_locale/0:backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend!/0.:onlyis a unit category or unit, or a list of unit categories and units. The parsed unit must match one of the categories or units in order to be valid. This is helpful when disambiguating parsed units. For example, parsing "w" could be eitherwattsor:week. Specifyingonly: :durationwould return:week. Specifyingonly: :powerwould return:watts:exceptis the oppostte of:only. The parsed unit must not match the specified unit or category, or unit categories and units.
Returns
unit_nameorraises an exception
Notes
When both
:onlyand:exceptoptions are passed, both conditions must be true in order to return a parsed result.Only units returned by
Cldr.Unit.known_units/0can be used in the:onlyand:exceptfilters.
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name! "kg"
:kilogram
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name! "w"
:watt
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name! "w", only: :duration
:week
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name! "m", only: [:year, :month, :day]
:month
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name! "tages", locale: "de"
:day
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name! "tag", locale: "de"
:day
iex> Bonfire.Common.Localise.Cldr.Unit.parse_unit_name!("millispangels")
** (Cldr.UnknownUnitError) Unknown unit was detected at "spangels"
@spec preferred_units(Cldr.Unit.t(), Keyword.t()) :: {:ok, [atom(), ...], Keyword.t()} | {:error, {module(), binary()}}
Returns a list of the preferred units for a given unit, locale, use case and scope.
The units used to represent length, volume and so on depend on a given territory, measurement system and usage.
For example, in the US, people height is most commonly
referred to in inches, or informally as feet and inches.
In most of the rest of the world it is centimeters.
Arguments
unitis any unit returned byCldr.Unit.new/2.backendis any Cldr backend module. That is, any module that includesuse Cldr. The default isCldr.default_backend/0optionsis a keyword list of options or aCldr.Unit.Conversion.Optionsstruct. The default is[].
Options
:usageis the unit usage. for example;personfor a unit type of length. The available usage for a given unit category can be seen withCldr.Unit.unit_category_usage/0. The default isnil:scopeis either:smallornil. In some usage, the units used are different when the unit size is small. It is up to the developer to determine whenscope: :smallis appropriate.:altis either:informalornil. Like:scope, the units in use depend on whether they are being used in a formal or informal context.:localeis any locale returned byCldr.validate_locale/2
Returns
{:ok, unit_list, formatting_options}or{:error, {exception, reason}}
Notes
formatting_options is a keyword list of options
that can be passed to Cldr.Unit.to_string/3. Its
primary intended usage is for localizing a unit that
decomposes into more than one unit (for example when
2 meters might become 6 feet 6 inches.) In such
cases, the last unit in the list (in this case the
inches) is formatted with the formatting_options.
Examples
iex> meter = Cldr.Unit.new!(:meter, 1)
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units meter, locale: "en-US", usage: :person_height
{:ok, [:foot, :inch], []}
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units meter, locale: "en-US", usage: :person
{:ok, [:inch], []}
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units meter, locale: "en-AU", usage: :person
{:ok, [:centimeter], []}
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units meter, locale: "en-US", usage: :road
{:ok, [:foot], [round_nearest: 1]}
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units meter, locale: "en-AU", usage: :road
{:ok, [:meter], [round_nearest: 1]}
Returns a list of the preferred units for a given unit, locale, use case and scope.
The units used to represent length, volume and so on depend on a given territory, measurement system and usage.
For example, in the US, people height is most commonly
referred to in inches, or informally as feet and inches.
In most of the rest of the world it is centimeters.
Arguments
unitis any unit returned byCldr.Unit.new/2.backendis any Cldr backend module. That is, any module that includesuse Cldr. The default isCldr.default_backend/0optionsis a keyword list of options or aCldr.Unit.Conversion.Optionsstruct. The default is[].
Options
:usageis the unit usage. for example;personfor a unit type of length. The available usage for a given unit category can be seen withCldr.Unit.unit_category_usage/0. The default isnil:scopeis either:smallornil. In some usage, the units used are different when the unit size is small. It is up to the developer to determine whenscope: :smallis appropriate.:altis either:informalornil. Like:scope, the units in use depend on whether they are being used in a formal or informal context.:localeis any locale returned byCldr.validate_locale/2
Returns
unit_listorraises an exception
Examples
iex> meter = Cldr.Unit.new!(:meter, 2)
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units! meter, locale: "en-US", usage: :person_height
[:foot, :inch]
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units! meter, locale: "en-AU", usage: :person
[:centimeter]
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units! meter, locale: "en-US", usage: :road
[:foot]
iex> Bonfire.Common.Localise.Cldr.Unit.preferred_units! meter, locale: "en-AU", usage: :road
[:meter]
See Cldr.Unit.Math.round/1.
See Cldr.Unit.Math.round/2.
See Cldr.Unit.Math.round/3.
See Cldr.Unit.Math.sub/2.
See Cldr.Unit.Math.sub!/2.
@spec to_iolist(Cldr.Unit.value() | Cldr.Unit.t() | [Cldr.Unit.t(), ...], Keyword.t()) :: {:ok, list()} | {:error, {atom(), binary()}}
Formats a number into an iolist according to a unit definition for a locale.
Arguments
list_or_numberis any number (integer, float or Decimal) or at:Cldr.Unitstruct or a list oft:Cldr.Unitstructsoptionsis a keyword list
Options
:unitis any unit returned byCldr.Unit.known_units/0. Ignored if the number to be formatted is at:Cldr.Unitstruct:localeis any valid locale name returned byCldr.known_locale_names/0or aCldr.LanguageTagstruct. The default isCldr.get_locale/0:styleis one of those returned byCldr.Unit.known_styles. The current styles are:long,:shortand:narrow. The default isstyle: :long:grammatical_caseindicates that a localisation for the given locale and given grammatical case should be used. SeeCldr.Unit.known_grammatical_cases/0for the list of known grammatical cases. Note that not all locales define all cases. However all locales do define the:nominativecase, which is also the default.:genderindicates that a localisation for the given locale and given grammatical gender should be used. SeeCldr.Unit.known_grammatical_genders/0for the list of known grammatical genders. Note that not all locales define all genders. The default gender isBonfire.Common.Localise.Cldr.Unit.default_gender/1for the given locale.:list_optionsis a keyword list of options for formatting a list which is passed through toCldr.List.to_string/3. This is only applicable when formatting a list of units.Any other options are passed to
Cldr.Number.to_string/2which is used to format thenumber
Returns
{:ok, io_list}or{:error, {exception, message}}
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.to_iolist Cldr.Unit.new!(:gallon, 123)
{:ok, ["123", " gallons"]}
@spec to_iolist!( Cldr.Unit.value() | Cldr.Unit.t() | [Cldr.Unit.t(), ...], Keyword.t() ) :: list() | no_return()
Formats a unit using to_iolist/3 but raises if there is
an error.
Arguments
list_or_numberis any number (integer, float or Decimal) or at:Cldr.Unitstruct or a list oft:Cldr.Unitstructsoptionsis a keyword list
Options
:unitis any unit returned byCldr.Unit.known_units/0. Ignored if the number to be formatted is at:Cldr.Unitstruct:localeis any valid locale name returned byCldr.known_locale_names/0or aCldr.LanguageTagstruct. The default isCldr.get_locale/0:styleis one of those returned byCldr.Unit.known_styles/0. The current styles are:long,:shortand:narrow. The default isstyle: :long.:grammatical_caseindicates that a localisation for the given locale and given grammatical case should be used. SeeCldr.Unit.known_grammatical_cases/0for the list of known grammatical cases. Note that not all locales define all cases. However all locales do define the:nominativecase, which is also the default.:genderindicates that a localisation for the given locale and given grammatical gender should be used. SeeCldr.Unit.known_grammatical_genders/0for the list of known grammatical genders. Note that not all locales define all genders. The default gender isBonfire.Common.Localise.Cldr.Unit.default_gender/1for the given locale.:list_optionsis a keyword list of options for formatting a list which is passed through toCldr.List.to_string/3. This is only applicable when formatting a list of units.Any other options are passed to
Cldr.Number.to_string/2which is used to format thenumber
Returns
io_listorraises an exception
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.to_iolist! 123, unit: :gallon
["123", " gallons"]
@spec to_string(Cldr.Unit.value() | Cldr.Unit.t() | [Cldr.Unit.t(), ...], Keyword.t()) :: {:ok, String.t()} | {:error, {atom(), binary()}}
Formats a number into a string according to a unit definition for a locale.
Arguments
list_or_numberis any number (integer, float or Decimal) or at:Cldr.Unitstruct or a list oft:Cldr.Unitstructsoptionsis a keyword list
Options
:unitis any unit returned byCldr.Unit.known_units/0. Ignored if the number to be formatted is at:Cldr.Unitstruct:localeis any valid locale name returned byCldr.known_locale_names/0or aCldr.LanguageTagstruct. The default isCldr.get_locale/0:styleis one of those returned byCldr.Unit.known_styles. The current styles are:long,:shortand:narrow. The default isstyle: :long:grammatical_caseindicates that a localisation for the given locale and given grammatical case should be used. SeeCldr.Unit.known_grammatical_cases/0for the list of known grammatical cases. Note that not all locales define all cases. However all locales do define the:nominativecase, which is also the default.:genderindicates that a localisation for the given locale and given grammatical gender should be used. SeeCldr.Unit.known_grammatical_genders/0for the list of known grammatical genders. Note that not all locales define all genders. The default gender isBonfire.Common.Localise.Cldr.Unit.default_gender/1for the given locale.:list_optionsis a keyword list of options for formatting a list which is passed through toCldr.List.to_string/3. This is only applicable when formatting a list of units.Any other options are passed to
Cldr.Number.to_string/2which is used to format thenumber
Returns
{:ok, formatted_string}or{:error, {exception, message}}
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 123)
{:ok, "123 gallons"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1)
{:ok, "1 gallon"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1), locale: "af"
{:ok, "1 gelling"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1), locale: "af-NA"
{:ok, "1 gelling"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1), locale: "bs"
{:ok, "1 galon"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1234), format: :long
{:ok, "1 thousand gallons"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:gallon, 1234), format: :short
{:ok, "1K gallons"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:megahertz, 1234)
{:ok, "1,234 megahertz"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:megahertz, 1234), style: :narrow
{:ok, "1,234MHz"}
iex> Bonfire.Common.Localise.Cldr.Unit.to_string Cldr.Unit.new!(:megabyte, 1234), locale: "en", style: :unknown
{:error, {Cldr.UnknownFormatError, "The unit style :unknown is not known."}}
@spec to_string!( Cldr.Unit.value() | Cldr.Unit.t() | [Cldr.Unit.t(), ...], Keyword.t() ) :: String.t() | no_return()
Formats a list using to_string/3 but raises if there is
an error.
Arguments
list_or_numberis any number (integer, float or Decimal) or at:Cldr.Unitstruct or a list oft:Cldr.Unitstructsoptionsis a keyword list
Options
:unitis any unit returned byCldr.Unit.known_units/0. Ignored if the number to be formatted is at:Cldr.Unitstruct:localeis any valid locale name returned byCldr.known_locale_names/0or aCldr.LanguageTagstruct. The default isCldr.get_locale/0:styleis one of those returned byCldr.Unit.known_styles. The current styles are:long,:shortand:narrow. The default isstyle: :long:grammatical_caseindicates that a localisation for the given locale and given grammatical case should be used. SeeCldr.Unit.known_grammatical_cases/0for the list of known grammatical cases. Note that not all locales define all cases. However all locales do define the:nominativecase, which is also the default.:genderindicates that a localisation for the given locale and given grammatical gender should be used. SeeCldr.Unit.known_grammatical_genders/0for the list of known grammatical genders. Note that not all locales define all genders. The default gender isBonfire.Common.Localise.Cldr.Unit.default_gender/1for the given locale.:list_optionsis a keyword list of options for formatting a list which is passed through toCldr.List.to_string/3. This is only applicable when formatting a list of units.Any other options are passed to
Cldr.Number.to_string/2which is used to format thenumber
Returns
formatted_stringorraises an exception
Examples
iex> Bonfire.Common.Localise.Cldr.Unit.to_string! 123, unit: :gallon
"123 gallons"
iex> Bonfire.Common.Localise.Cldr.Unit.to_string! 1, unit: :gallon
"1 gallon"
iex> Bonfire.Common.Localise.Cldr.Unit.to_string! 1, unit: :gallon, locale: "af"
"1 gelling"
See Cldr.Unit.value/1.
See Cldr.Unit.zero/1.
See Cldr.Unit.zero?/1.