Cldr.Unit.Preference.preferred_units
preferred_units, go back to Cldr.Unit.Preference module for more information.
Returns a list of the preferred units for a given unit, locale, territory and use case.
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 at:Cldr.Unit.Conversion.Optionsstruct. The default is[].
Options
:usageis the unit usage. for example;personfor a unit of type: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}or{:error, {exception, reason}}
Examples
iex> meter = Cldr.Unit.new!(:meter, 1)
iex> many_meters = Cldr.Unit.new!(:meter, 10_000)
iex> Cldr.Unit.Preference.preferred_units meter, MyApp.Cldr, locale: "en-US", usage: :person
{:ok, [:inch], []}
iex> Cldr.Unit.Preference.preferred_units meter, MyApp.Cldr, locale: "en-AU", usage: :person
{:ok, [:centimeter], []}
iex> Cldr.Unit.Preference.preferred_units meter, MyApp.Cldr, locale: "en-US", usage: :road
{:ok, [:foot], [round_nearest: 1]}
iex> Cldr.Unit.Preference.preferred_units many_meters, MyApp.Cldr, locale: "en-US", usage: :road
{:ok, [:mile], []}
iex> Cldr.Unit.Preference.preferred_units meter, MyApp.Cldr, locale: "en-AU", usage: :road
{:ok, [:meter], [round_nearest: 1]}
iex> Cldr.Unit.Preference.preferred_units many_meters, MyApp.Cldr, locale: "en-AU", usage: :road
{:ok, [:kilometer], []}Notes
One common pattern is to convert a given unit into the unit
appropriate for a given locale and usage. This can be
accomplished with a combination of Cldr.Unit.Preference.preferred_units/3
and Cldr.Unit.decompose/2. For example:
iex> meter = Cldr.Unit.new!(:meter, 1)
iex> preferred_units = Cldr.Unit.Preference.preferred_units(meter,
...> MyApp.Cldr, locale: "en-US", usage: :person)
iex> with {:ok, preferred_units, _} <- preferred_units do
...> Cldr.Unit.decompose(meter, preferred_units)
...> end
[Cldr.Unit.new!(:inch, Ratio.new(216172782113783808, 5490788665690109))]