View Source Cldr.Message (Cldr Messages v1.0.0)
Implements the ICU Message Format with functions to parse and interpolate messages.
Summary
Functions
Extract the binding names from an ICU message.
Formats a message into a canonical form.
Formats a message into a canonical form or raises if the message cannot be parsed.
Format a message in the ICU Message Format into a string.
Format a message in the ICU Message Format into an iolist.
Returns the Jaro distance between two messages.
Returns the Jaro distance between two messages or raises.
Returns the translation of the given ICU-formatted message string.
Returns the translation of the given ICU-formatted message string.
Types
Functions
Extract the binding names from an ICU message.
Arguments
messageis a CLDR message in binary or parsed form.
Returns
A list of binding names as strings or
{:error, {exception, reason}}
Examples
iex> Cldr.Message.bindings "This {variable} is in the message"
["variable"]
Formats a message into a canonical form.
This allows for messages to be compared
directly, or using Cldr.Message.jaro_distance/3.
Arguments
messageis a CLDR message in binary form.optionsis a keyword list of options. The default is[].
Options
:trimdetermines if the message is trimmed of whitespace before formatting. The default istrue.:prettydetermines if the message if formatted with indentation to aid readability. The default isfalse.
Returns
{ok, canonical_message}wherecanonical_messageis a string or{:error, {exception, reason}}
Examples
iex> Cldr.Message.canonical_message "{greeting } to you!"
{:ok, "{greeting} to you!"}
Formats a message into a canonical form or raises if the message cannot be parsed.
This allows for messages to be compared
directly, or using Cldr.Message.jaro_distance/3.
Arguments
messageis a CLDR message in binary form.optionsis a keyword list of options. The default is[].
Options
:trimdetermines if the message is trimmed of whitespace before formatting. The default istrue.:prettydetermines if the message if formatted with indentation to aid readability. The default isfalse.
Returns
canonical_messageas a string orraises an exception
Examples
iex> Cldr.Message.canonical_message! "{greeting } to you!"
"{greeting} to you!"
@spec format(String.t(), bindings(), options()) :: {:ok, String.t()} | {:error, {module(), String.t()}}
Format a message in the ICU Message Format into a string.
The ICU Message Format uses message "pattern" strings with
variable-element placeholders enclosed in {curly braces}. The
argument syntax can include formatting details, otherwise a
default format is used.
Arguments
bindingsis a list or map of arguments that are used to replace placeholders in the message.optionsis a keyword list of options.
Options
backendis anyCldrbackend. That is, any module that containsuse Cldr.:localeis any valid locale name returned byCldr.known_locale_names/0or at:Cldr.LanguageTagstruct. The default isCldr.get_locale/0.:trimdetermines if the message is trimmed of whitespace before formatting. The default isfalse.:allow_positional_argsdetermines if position arguments are permitted. Positional arguments are in the format{0}in the message. The default istrue.All other aptions are passed to the
to_string/2function of a formatting module.
Returns
{:ok, formatted_mesasge}or{:error, {module, reason}}
Examples
iex> Cldr.Message.format "{greeting} to you!", greeting: "Good morning"
{:ok, "Good morning to you!"}
@spec format_to_iolist(String.t(), bindings(), options()) :: {:ok, list(), list(), list()} | {:error, list(), list(), list()} | {:error, {module(), binary()}}
Format a message in the ICU Message Format into an iolist.
The ICU Message Format uses message "pattern" strings with
variable-element placeholders enclosed in {curly braces}. The
argument syntax can include formatting details, otherwise a
default format is used.
Arguments
bindingsis a list or map of arguments that are used to replace placeholders in the message.optionsis a keyword list of options.
Options
backendis anyCldrbackend. That is, any module that containsuse Cldr.:localeis any valid locale name returned byCldr.known_locale_names/0or at:Cldr.LanguageTagstruct. The default isCldr.get_locale/0.:trimdetermines if the message is trimmed of whitespace before formatting. The default isfalse.:allow_positional_argsdetermines if position arguments are permitted. Positional arguments are in the format{0}in the message. The default istrue.All other aptions are passed to the
to_string/2function of a formatting module.
Returns
{:ok, formatted_mesasge}or{:error, {module, reason}}
Examples
iex> Cldr.Message.format_to_iolist "{greeting} to you!", greeting: "Good morning"
{:ok, ["Good morning", " to you!"], ["greeting"], []}
Returns the Jaro distance between two messages.
This allows for fuzzy matching of message which can be helpful when a message string is changed but the semantics remain the same.
Arguments
message1is a CLDR message in binary form.message2is a CLDR message in binary form.optionsis a keyword list of options. The default is[].
Options
:trimdetermines if the message is trimmed of whitespace before formatting. The default isfalse.
Returns
{ok, distance}wheredistanceis a float value between 0.0 (equates to no similarity) and 1.0 (is an exact match) representing Jaro distance betweenmessage1andmessage2or{:error, {exception, reason}}
Examples
iex> Cldr.Message.jaro_distance "{greetings} to you!", "{greeting} to you!"
{:ok, 0.9824561403508771}
Returns the Jaro distance between two messages or raises.
This allows for fuzzy matching of message which can be helpful when a message string is changed but the semantics remain the same.
Arguments
message1is a CLDR message in binary form.message2is a CLDR message in binary form.optionsis a keyword list of options. The default is[].
Options
:trimdetermines if the message is trimmed of whitespace before formatting. The default isfalse.
Returns
distancewheredistanceis a float value between 0.0 (equates to no similarity) and 1.0 (is an exact match) representing Jaro distance betweenmessage1andmessage2orraises an exception
Examples
iex> Cldr.Message.jaro_distance! "{greetings} to you!", "{greeting} to you!"
0.9824561403508771
Returns the translation of the given ICU-formatted message string.
Any placeholders are replaced with the value of variables already in scope at the time of compilation.
t/1 is a wrapper around the gettext/2 macro
which should therefore be imported from a Gettext
backend prior to calling t/1.
Arguments
messageis an ICU format message string.
Returns
- A translated string.
Returns the translation of the given ICU-formatted message string.
t/2 is a wrapper around the gettext/2 macro
which should therefore be imported from a Gettext
backend prior to calling t/2.
Arguments
messageis an ICU format message string.bindingsis a keyword list or map of bindings used to replace placeholders in the message.
Returns
- A translated string.