Cldr v2.20.0 Cldr.Substitution View Source

Compiles substituation formats that are of the form "{0} something {1}" into a token list that allows for more efficient parameter substituation at runtime.

Link to this section Summary

Functions

Parses a substitution template into a list of tokens to allow efficient parameter substitution at runtime.

Substitutes a list of values into a template token list that is created by Cldr.Substitution.parse/1.

Link to this section Functions

Specs

parse(String.t()) :: [String.t() | integer(), ...]

Parses a substitution template into a list of tokens to allow efficient parameter substitution at runtime.

  • template is a binary that may include parameter markers that are substituded for values at runtime.

Returns:

  • A list of tokens where any substitution is marked as an integer any any binary tokens are passed through as is.

Examples

iex> Cldr.Substitution.parse "{0}, {1}"
[0, ", ", 1]

iex> Cldr.Substitution.parse "{0} This is something {1} or another {2}"
[0, " This is something ", 1, " or another ", 2]

This function is primarily intended to support compile-time generation of templates that simplify and speed up parameter substitution at runtime.

Specs

substitute(term() | [term(), ...], [String.t() | integer(), ...]) :: [
  term(),
  ...
]

Substitutes a list of values into a template token list that is created by Cldr.Substitution.parse/1.

Returns:

  • A list with values substituted for parameters in the list1 template

Examples:

iex> template = Cldr.Substitution.parse "{0} This is something {1}"
[0, " This is something ", 1]
iex> Cldr.Substitution.substitute ["a", "b"], template
["a", " This is something ", "b"]