Localize.List.Pattern (Localize v0.6.0)

Copy Markdown View Source

Defines the structure for list formatting patterns.

Each pattern has four components used for different list sizes:

  • :two — used when the list has exactly two elements.

  • :start — used for the first pair of elements in lists with three or more elements.

  • :middle — used for pairs in the middle of lists with four or more elements.

  • :end — used for the last pair of elements in lists with three or more elements.

Each component is a pre-parsed token list from Localize.Substitution.parse/1.

Summary

Functions

Creates a pattern from locale data.

Creates a new pattern from a map of template strings.

Types

t()

@type t() :: %Localize.List.Pattern{
  end: token_list(),
  middle: token_list(),
  start: token_list(),
  two: token_list()
}

token_list()

@type token_list() :: [String.t() | integer()]

Functions

from_locale_data(data)

@spec from_locale_data(map()) :: t()

Creates a pattern from locale data.

Locale data uses integer key 2 for the two-element pattern. This function normalizes that into the standard struct form.

Arguments

  • data is a map from locale data with keys :start, :middle, :end, and 2.

Returns

new(options)

@spec new(Keyword.t() | map()) :: {:ok, t()} | {:error, Exception.t()}

Creates a new pattern from a map of template strings.

Arguments

  • options is a keyword list or map with the keys :two, :start, :middle, and :end. Each value is either a template string like "{0}, and {1}" or a pre-parsed token list.

Returns

  • {:ok, pattern} where pattern is a t/0.

  • {:error, reason} if any required key is missing.

Examples

iex> Localize.List.Pattern.new(
...>   two: "{0} and {1}",
...>   start: "{0}, {1}",
...>   middle: "{0}, {1}",
...>   end: "{0}, and {1}"
...> )
{:ok,
 %Localize.List.Pattern{
   two: [0, " and ", 1],
   start: [0, ", ", 1],
   middle: [0, ", ", 1],
   end: [0, ", and ", 1]
 }}