package
An AST parser for ICU MessageFormat strings
npm install @messageformat/parserGitHub-flavored package documentation.
Across all versions, last 30 days.
13,235,246 in 30 days
Runtime metadata.
Tagged topics.
Estimated unpacked size across 2 packages.
75.9 kB
Includes dependencies.
Latest published Oct 12, 2026
An AST parser for ICU MessageFormat strings – part of messageformat.
The parse(src, [options]) function takes two parameters, first the
string to be parsed, and a second optional parameter options, an object with
the following possible keys:
cardinal and ordinal – Arrays of valid plural categories for the current
locale, used to validate plural and selectordinal keys. If these are
missing or set to false, the full set of valid Unicode CLDR keys is used:
'zero', 'one', 'two', 'few', 'many', 'other'. To disable this check, pass in
an empty array.
strict – By default, the parsing applies a few relaxations to the ICU
MessageFormat spec. Setting strict: true will disable these relaxations:
argType of simpleArg formatting functions will be restricted to the
set of number, date, time, spellout, ordinal, and duration,
rather than accepting any lower-case identifier that does not start with a
number.argStyle of simpleArg formatting functions will not be
parsed as any other text, but instead as the spec requires: "In
argStyleText, every single ASCII apostrophe begins and ends quoted literal
text, and unquoted {curly braces} must occur in matched pairs."plural or selectordinal statement, a pound symbol (#) is
replaced with the input number. By default, # is also parsed as a special
character in nested statements too, and can be escaped using apostrophes
('#'). In strict mode # will be parsed as a special character only
directly inside a plural or selectordinal statement. Outside those, #
and '#' will be parsed as literal text.The parser only supports the default DOUBLE_OPTIONAL apostrophe mode, in
which a single apostrophe only starts quoted literal text if it immediately
precedes a curly brace {}, or a pound symbol # if inside a plural format. A
literal apostrophe ' is represented by either a single ' or a doubled ''
apostrophe character.
This package was previously named messageformat-parser.
npm install @messageformat/parser> const { parse } = require('@messageformat/parser')
// For clarity, the examples below do not show the ctx object included for each token
> parse('So {wow}.')
[ { type: 'content', value: 'So ' },
{ type: 'argument', arg: 'wow' },
{ type: 'content', value: '.' } ]
> parse('Such { thing }. { count, selectordinal, one {First} two {Second}' +
' few {Third} other {#th} } word.')
[ { type: 'content', value: 'Such ' },
{ type: 'argument', arg: 'thing' },
{ type: 'content', value: '. ' },
{ type:
For more example usage, please take a look at our test suite.
The output of parse() is an array of tokens, Array<Content | PlainArg | FunctionArg | Select>:
interface Content {
type: 'content'
value: string
ctx: Context
}
interface PlainArg {
type: 'argument'
arg: string
ctx: Context
}
interface FunctionArg {
type: 'function'
arg: string
key: string
param?: Array<Content | PlainArg | FunctionArg | Select | Octothorpe>
ctx:
Messageformat is an OpenJS Foundation project, and we follow its Code of Conduct.