Parses an ICU MessageFormat 1 message into the list of parts that
Localize.Message.MF1 formats.
A parsed message is a list whose elements are:
{:literal, string}— text, with quoting resolved.{:named_arg, name}or{:pos_arg, index}— an argument, by name (a string) or by position.{:simple_format, argument, type}and{:simple_format, argument, type, style}— a formatted argument such as{count, number, integer}. The type is one of:number,:date,:time,:datetime,:spellout,:ordinal,:unit,:listor:money. The style is a keyword atom,{:named_format, name}for a:namestyle,{:skeleton, text}for a::skeletonstyle, or pattern text.{:plural, argument, options, clauses}and{:select_ordinal, argument, options, clauses}— the options are[offset: n]when an offset is given, and the clauses map:zero,:one,:two,:few,:many,:otheror an explicit integer value to a message.{:select, argument, clauses}— the clauses map each selector string to a message.:value—#in apluralorselectordinalclause, the number being selected on.
Summary
Functions
Parses a MessageFormat 1 message.
Types
@type t() :: [term()]
A parsed MessageFormat 1 message.
Functions
@spec parse(term(), Keyword.t()) :: {:ok, t()} | {:error, Localize.Message.MF1.ParseError.t() | Localize.Message.MF1.PositionalArgumentError.t()}
Parses a MessageFormat 1 message.
Arguments
messageis a MessageFormat 1 message string.optionsis a keyword list of options.
Options
:allow_positional_argsistrue(the default) to accept positional arguments such as{0}, orfalseto return an error for them.
Returns
{:ok, parsed_message}, or{:error, %Localize.Message.MF1.ParseError{}}when the message cannot be parsed, or is not a string, or{:error, %Localize.Message.MF1.PositionalArgumentError{}}when it uses positional arguments and they are not allowed.
Examples
iex> Localize.Message.MF1.Parser.parse("Hello {name}!")
{:ok, [literal: "Hello ", named_arg: "name", literal: "!"]}
iex> Localize.Message.MF1.Parser.parse("{count, plural, one {# item} other {# items}}")
{:ok,
[
{:plural, {:named_arg, "count"}, [],
%{one: [:value, {:literal, " item"}], other: [:value, {:literal, " items"}]}}
]}