Cldr v0.1.0 Cldr.List

Cldr incudes patterns that enable list to be catenated together to form a grammatically correct language construct for a given locale.

If we have a list of days like ["Monday", "Tuesday", "Wednesday"] then we can format that list for a given locale by:

iex> Cldr.List.to_string(["Monday", "Tuesday", "Wednesday"], locale: "en")
"Monday, Tuesday, and Wednesday"

Summary

Functions

Returns the styles of list patterns available for a locale

Returns the list patterns for a locale

Formats a list into a string according to the list pattern rules for a locale

Formats a list using to_string/2 but raises if there is an error

Types

pattern_type()
pattern_type() :: :standard | :unit | :unit_narrow | :unit_short

Functions

list_pattern_styles_for(binary)
list_pattern_styles_for(Cldr.locale) :: [atom]

Returns the styles of list patterns available for a locale.

Returns a list of atoms of of the list format styles that are available in CLDR for a locale.

Example

iex> Cldr.List.list_pattern_styles_for("en")
[:standard, :standard_short, :unit, :unit_narrow, :unit_short]
list_patterns_for(binary)
list_patterns_for(Cldr.locale) :: Map.t

Returns the list patterns for a locale.

List patterns provide rules for combining multiple items into a language format appropriate for a locale.

Example

iex> Cldr.List.list_patterns_for "en"
%{standard: %{"2": [0, " and ", 1], end: [0, ", and ", 1],
   middle: [0, ", ", 1], start: [0, ", ", 1]},
 standard_short: %{"2": [0, " and ", 1], end: [0, ", and ", 1],
   middle: [0, ", ", 1], start: [0, ", ", 1]},
 unit: %{"2": [0, ", ", 1], end: [0, ", ", 1], middle: [0, ", ", 1],
   start: [0, ", ", 1]},
 unit_narrow: %{"2": [0, " ", 1], end: [0, " ", 1],
   middle: [0, " ", 1], start: [0, " ", 1]},
 unit_short: %{"2": [0, ", ", 1], end: [0, ", ", 1],
   middle: [0, ", ", 1], start: [0, ", ", 1]}}
to_string(list, options \\ [])
to_string(List.t, Keyword.t) :: String.t

Formats a list into a string according to the list pattern rules for a locale.

  • list is any list of of terms that can be passed through Kernel.to_string/1

  • options are:

  • locale is any configured locale. See Cldr.known_locales(). The default is locale: Cldr.get_locale()

  • format is one of those returned by Cldr.List.list_pattern_types_for/1. The default is format: :standard

Examples

iex> Cldr.List.to_string(["a", "b", "c"], locale: "en")
"a, b, and c"

iex> Cldr.List.to_string(["a", "b", "c"], locale: "en", format: :unit_narrow)
"a b c"

iex> Cldr.List.to_string(["a", "b", "c"], locale: "fr")
"a, b et c"

iex> Cldr.List.to_string([1,2,3,4,5,6])
"1, 2, 3, 4, 5, and 6"

iex> Cldr.List.to_string(["a"])
"a"

iex> Cldr.List.to_string([1,2])
"1 and 2"
to_string!(list, options \\ [])
to_string!(List.t, Keyword.t) :: String.t | Exception.t

Formats a list using to_string/2 but raises if there is an error.