Gettext.Interpolation.Default (gettext v0.19.0) View Source

Default Interpolation Implementation

Replaces %{binding_name} with a string value.

Link to this section Summary

Functions

Compile a static message to interpolate with dynamic bindings.

Interpolate a message or interpolatable with the given bindings.

Link to this section Types

Specs

interpolatable() :: [String.t() | atom()]

Link to this section Functions

Link to this macro

compile_interpolate(translation_type, message, bindings)

View Source (macro)

Compile a static message to interpolate with dynamic bindings.

This macro takes a static message and some dynamic bindings. The generated code will return an {:ok, interpolated_string} tuple if the interpolation is successful. If it encounters a binding in the message that is missing from bindings, it returns {:missing_bindings, incomplete_string, missing_bindings}, where incomplete_string is the string with only the present bindings interpolated and missing_bindings is a list of atoms representing bindings that are in interpolatable but not in bindings.

Link to this function

runtime_interpolate(message, bindings)

View Source

Interpolate a message or interpolatable with the given bindings.

This function takes a message and some bindings and returns an {:ok, interpolated_string} tuple if interpolation is successful. If it encounters a binding in the message that is missing from bindings, it returns {:missing_bindings, incomplete_string, missing_bindings} where incomplete_string is the string with only the present bindings interpolated and missing_bindings is a list of atoms representing bindings that are in interpolatable but not in bindings.

Examples

iex> msgid = "Hello %{name}, you have %{count} unread messages"
iex> good_bindings = %{name: "José", count: 3}
iex> Gettext.Interpolation.Default.runtime_interpolate(msgid, good_bindings)
{:ok, "Hello José, you have 3 unread messages"}
iex> Gettext.Interpolation.Default.runtime_interpolate(msgid, %{name: "José"})
{:missing_bindings, "Hello José, you have %{count} unread messages", [:count]}

iex> msgid = "Hello %{name}, you have %{count} unread messages"
iex> interpolatable = Gettext.Interpolation.Default.to_interpolatable(msgid)
iex> good_bindings = %{name: "José", count: 3}
iex> Gettext.Interpolation.Default.runtime_interpolate(interpolatable, good_bindings)
{:ok, "Hello José, you have 3 unread messages"}
iex> Gettext.Interpolation.Default.runtime_interpolate(interpolatable, %{name: "José"})
{:missing_bindings, "Hello José, you have %{count} unread messages", [:count]}