View Source Cldr.Substitution (Cldr v2.40.1)

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

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.

Functions

@spec parse(String.t()) :: [String.t() | integer(), ...] | {:error, String.t()}

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 substituted 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.

@spec 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"]