Locale-sensitive list formatting, modelled on
Intl.ListFormat.
Joins a list of items into a human-readable string using locale-appropriate conjunctions ("and"), disjunctions ("or"), or unit-style separators.
Delegates to Localize.List for the underlying formatting.
Summary
Functions
Formats a list into a locale-aware string.
Formats a list into a locale-aware string, raising on error.
Formats a list into typed parts.
Formats a list into typed parts, raising on error.
Functions
Formats a list into a locale-aware string.
Arguments
listis a list of terms that implementString.Chars.optionsis a keyword list of options.
Options
:localeis a locale identifier string or atom. The default is the current process locale.:typeis:conjunction,:disjunction, or:unit. The default is:conjunction.:styleis:long,:short, or:narrow. The default is:long.
Returns
{:ok, formatted_string}on success.{:error, reason}if the locale or options are invalid.
Examples
iex> Intl.ListFormat.format(["a", "b", "c"], locale: :en)
{:ok, "a, b, and c"}
iex> Intl.ListFormat.format(["a", "b", "c"], locale: :en, type: :disjunction)
{:ok, "a, b, or c"}
iex> Intl.ListFormat.format(["a", "b"], locale: :en, type: :unit, style: :narrow)
{:ok, "a b"}
Formats a list into a locale-aware string, raising on error.
Same as format/2 but returns the string directly or raises.
Arguments
listis a list of terms that implementString.Chars.optionsis a keyword list of options.
Returns
- A formatted string.
Examples
iex> Intl.ListFormat.format!(["a", "b", "c"], locale: :en)
"a, b, and c"
@spec format_to_parts([term()], Keyword.t()) :: {:ok, [%{type: atom(), value: String.t()}]} | {:error, term()}
Formats a list into typed parts.
Modelled on the JS Intl.ListFormat.formatToParts(). Each list
element becomes an :element part and each separator becomes a
:literal part.
Arguments
listis a list of terms that implementString.Chars.optionsis a keyword list of options. Accepts the same options asformat/2.
Returns
{:ok, parts}wherepartsis a list of%{type: atom, value: String.t()}maps.{:error, reason}if the locale or options are invalid.
Examples
iex> Intl.ListFormat.format_to_parts(["a", "b", "c"], locale: :en)
{:ok, [
%{type: :element, value: "a"},
%{type: :literal, value: ", "},
%{type: :element, value: "b"},
%{type: :literal, value: ", and "},
%{type: :element, value: "c"}
]}
Formats a list into typed parts, raising on error.
Same as format_to_parts/2 but returns the parts directly or raises.
Arguments
listis a list of terms that implementString.Chars.optionsis a keyword list of options.
Returns
- A list of
%{type: atom, value: String.t()}maps.
Examples
iex> Intl.ListFormat.format_to_parts!(["a", "b"], locale: :en) |> length()
3