Intl.ListFormat (Intl v0.2.0)

Copy Markdown View Source

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.

Functions

format(list, options \\ [])

@spec format([term()], Keyword.t()) :: {:ok, String.t()} | {:error, term()}

Formats a list into a locale-aware string.

Arguments

  • list is a list of terms that implement String.Chars.

  • options is a keyword list of options.

Options

  • :locale is a locale identifier string or atom. The default is the current process locale.

  • :type is :conjunction, :disjunction, or :unit. The default is :conjunction.

  • :style is :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"}

format!(list, options \\ [])

@spec format!([term()], Keyword.t()) :: String.t() | no_return()

Formats a list into a locale-aware string, raising on error.

Same as format/2 but returns the string directly or raises.

Arguments

  • list is a list of terms that implement String.Chars.

  • options is a keyword list of options.

Returns

  • A formatted string.

Examples

iex> Intl.ListFormat.format!(["a", "b", "c"], locale: :en)
"a, b, and c"