How to use Cldr.Message.format on a string when there are no bindings set?

Hi,

How do i use Cldr.Message.format on a string when there are no bindings set for an value in the string?

Example:
Cldr.Message.format("{foo}", [])

returns
{:error, {Cldr.Message.BindError, "No binding was found for [\"foo\"]"}}

In my usecase i want the string to just return “{foo}” since there is no binding set.

Thanks in advance!

To escape tokens that are Message Format syntax requires the use of single quote wrapping like '{foo}'. This is documented in the ICU Message Format docs.

Therefore for your example it would be:

Cldr.Message.format("'{foo}'", [])

However there is a parser bug which I need to fix since currently that is erroring:

iex> Cldr.Message.format("'{foo}'", [])
{:error,
 {Cldr.Message.ParseError, "Couldn't parse message. Error detected at \"}'\""}}

I will fix this over the weekend and have opened an issue to track it.

I was able to get this fixed earlier than anticipated. ex_cldr_messages version 1.0.1 is now published to hex with the following changelog entry:

Bug Fixes

Example

iex> Cldr.Message.format("Bob's got an escaped '{foo}' literal", [])
{:ok, "Bob's got an escaped {foo} literal"}
1 Like

Thank you so much for the quick answer and fix!