View Source Bonfire.Common.Localise.Cldr.List (Bonfire v0.9.10-classic-beta.169)

Cldr backend module that formats lists.

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

iex> Bonfire.Common.Localise.Cldr.List.to_string(["Monday", "Tuesday", "Wednesday"], locale: "en")
{:ok, "Monday, Tuesday, and Wednesday"}

Summary

Functions

Intersperces a list elements into a list format according to the list pattern rules for a locale.

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

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.

Functions

Link to this function

intersperse(list, options \\ [])

View Source
@spec intersperse([term()], Keyword.t()) ::
  {:ok, list()} | {:error, {module(), String.t()}}

Intersperces a list elements into a list format according to the list pattern rules for a locale.

This function can be helpful when creating a list from Phoenix safe strings which are of the format {:safe, "some string"}

Arguments

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

  • options is a keyword list

Options

Examples

iex> Bonfire.Common.Localise.Cldr.List.intersperse(["a", "b", "c"], locale: "en")
{:ok, ["a", ", ", "b", ", and ", "c"]}

iex> Bonfire.Common.Localise.Cldr.List.intersperse(["a", "b", "c"], locale: "en", format: :unit_narrow)
{:ok, ["a", " ", "b", " ", "c"]}

iex> Bonfire.Common.Localise.Cldr.List.intersperse(["a", "b", "c"], locale: "fr")
{:ok, ["a", ", ", "b", " et ", "c"]}

iex> Bonfire.Common.Localise.Cldr.List.intersperse([1,2,3,4,5,6])
{:ok, [1, ", ", 2, ", ", 3, ", ", 4, ", ", 5, ", and ", 6]}

iex> Bonfire.Common.Localise.Cldr.List.intersperse(["a"])
{:ok, ["a"]}

iex> Bonfire.Common.Localise.Cldr.List.intersperse([1,2])
{:ok, [1, " and ", 2]}
Link to this function

intersperse(list, locale, pattern)

View Source
Link to this function

intersperse!(list, options \\ [])

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

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

Examples

iex> Bonfire.Common.Localise.Cldr.List.intersperse!(["a", "b", "c"], locale: "en")
["a", ", ", "b", ", and ", "c"]

iex> Bonfire.Common.Localise.Cldr.List.intersperse!(["a", "b", "c"], locale: "en", format: :unit_narrow)
["a", " ", "b", " ", "c"]
Link to this function

list_formats_for(locale_name)

View Source
@spec list_formats_for(Cldr.Locale.locale_name()) ::
  [atom()] | {:error, {module(), String.t()}}

Returns the styles of list patterns available for a locale.

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

Example

iex> Bonfire.Common.Localise.Cldr.List.list_formats_for(:en)
[:or, :or_narrow, :or_short, :standard, :standard_narrow, :standard_short,
 :unit, :unit_narrow, :unit_short]
Link to this function

list_patterns_for(locale_name)

View Source
@spec list_patterns_for(Cldr.Locale.locale_name()) ::
  map() | {:error, {module(), String.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> Bonfire.Common.Localise.Cldr.List.list_patterns_for(:en)
%{
  or: %Cldr.List.Pattern{
    two: [0, " or ", 1],
    end: [0, ", or ", 1],
    middle: [0, ", ", 1],
    start: [0, ", ", 1]
  },
  or_narrow: %Cldr.List.Pattern{
    two: [0, " or ", 1],
    end: [0, ", or ", 1],
    middle: [0, ", ", 1],
    start: [0, ", ", 1]
  },
  or_short: %Cldr.List.Pattern{
    two: [0, " or ", 1],
    end: [0, ", or ", 1],
    middle: [0, ", ", 1],
    start: [0, ", ", 1]
  },
  standard: %Cldr.List.Pattern{
    two: [0, " and ", 1],
    end: [0, ", and ", 1],
    middle: [0, ", ", 1],
    start: [0, ", ", 1]
  },
  standard_narrow: %Cldr.List.Pattern{
    two: [0, ", ", 1],
    end: [0, ", ", 1],
    middle: [0, ", ", 1],
    start: [0, ", ", 1]
  },
  standard_short: %Cldr.List.Pattern{
    two: [0, " & ", 1],
    end: [0, ", & ", 1],
    middle: [0, ", ", 1],
    start: [0, ", ", 1]
  },
  unit: %Cldr.List.Pattern{
    two: [0, ", ", 1],
    end: [0, ", ", 1],
    middle: [0, ", ", 1],
    start: [0, ", ", 1]
  },
  unit_narrow: %Cldr.List.Pattern{
    two: [0, " ", 1],
    end: [0, " ", 1],
    middle: [0, " ", 1],
    start: [0, " ", 1]
  },
  unit_short: %Cldr.List.Pattern{
    two: [0, ", ", 1],
    end: [0, ", ", 1],
    middle: [0, ", ", 1],
    start: [0, ", ", 1]
  }
}
Link to this function

to_string(list, options \\ [])

View Source
@spec to_string([term()], Keyword.t()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}

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

Arguments

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

  • options is a keyword list

Options

Examples

iex> Bonfire.Common.Localise.Cldr.List.to_string(["a", "b", "c"], locale: "en")
{:ok, "a, b, and c"}

iex> Bonfire.Common.Localise.Cldr.List.to_string(["a", "b", "c"], locale: "en", format: :unit_narrow)
{:ok, "a b c"}

iex> Bonfire.Common.Localise.Cldr.List.to_string(["a", "b", "c"], locale: "fr")
{:ok, "a, b et c"}

iex> Bonfire.Common.Localise.Cldr.List.to_string([1,2,3,4,5,6])
{:ok, "1, 2, 3, 4, 5, and 6"}

iex> Bonfire.Common.Localise.Cldr.List.to_string(["a"])
{:ok, "a"}

iex> Bonfire.Common.Localise.Cldr.List.to_string([1,2])
{:ok, "1 and 2"}
Link to this function

to_string!(list, options \\ [])

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

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

Examples

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

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