Localize.Message.MF1.Parser (Localize MF1 v0.1.0)

Copy Markdown View Source

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, :list or :money. The style is a keyword atom, {:named_format, name} for a :name style, {:skeleton, text} for a ::skeleton style, 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, :other or an explicit integer value to a message.

  • {:select, argument, clauses} — the clauses map each selector string to a message.

  • :value — # in a plural or selectordinal clause, the number being selected on.

Summary

Types

t()

A parsed MessageFormat 1 message.

Functions

Parses a MessageFormat 1 message.

Types

t()

@type t() :: [term()]

A parsed MessageFormat 1 message.

Functions

parse(message, options \\ [])

Parses a MessageFormat 1 message.

Arguments

  • message is a MessageFormat 1 message string.

  • options is a keyword list of options.

Options

  • :allow_positional_args is true (the default) to accept positional arguments such as {0}, or false to 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"}]}}
 ]}