Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
messageformat-parser
Advanced tools
A PEG.js parser for ICU MessageFormat strings -- part of messageformat.js
The messageformat-parser npm package is a tool for parsing ICU MessageFormat strings. It is used to handle internationalization (i18n) by parsing messages that can include variables, plurals, and select statements. This package is particularly useful for applications that need to support multiple languages and complex message formatting.
Parsing Simple Messages
This feature allows you to parse simple messages with placeholders. The `parse` function takes a message string and returns an abstract syntax tree (AST) representing the structure of the message.
const { parse } = require('messageformat-parser');
const message = 'Hello, {name}!';
const parsed = parse(message);
console.log(JSON.stringify(parsed, null, 2));
Handling Plurals
This feature allows you to parse messages that include pluralization. The `parse` function can handle ICU MessageFormat plural syntax, making it easier to manage messages that need to change based on quantity.
const { parse } = require('messageformat-parser');
const message = '{count, plural, one {# item} other {# items}}';
const parsed = parse(message);
console.log(JSON.stringify(parsed, null, 2));
Handling Select Statements
This feature allows you to parse messages that include select statements. The `parse` function can handle ICU MessageFormat select syntax, which is useful for gender-specific or other conditional messages.
const { parse } = require('messageformat-parser');
const message = '{gender, select, male {He} female {She} other {They}} will respond soon.';
const parsed = parse(message);
console.log(JSON.stringify(parsed, null, 2));
The intl-messageformat package provides similar functionality for parsing and formatting ICU MessageFormat strings. It also includes additional features for formatting dates, times, and numbers, making it a more comprehensive solution for internationalization.
The format-message package offers a similar capability to parse and format ICU MessageFormat strings. It also includes utilities for message extraction and translation, making it a good choice for projects that require a full i18n workflow.
The messageformat package is another alternative that not only parses ICU MessageFormat strings but also compiles them into JavaScript functions for runtime evaluation. This can be useful for performance optimization in large applications.
A PEG.js parser for ICU MessageFormat strings – part of messageformat.js. Outputs an AST defined by parser.pegjs.
The generated parser 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.
strictFunctionParams
– By default, function parameters are split on commas
and trimmed, so the parameters in {x,fn, a, b }
are parsed as
['a','b']
. Setting strictFunctionParams
to true will make the parser
follow the ICU MessageFormat spec more closely, and result in a params array
with a single element: [' a, b ']
.
npm install messageformat-parser
> var parse = require('messageformat-parser').parse;
> parse('So {wow}.')
[ 'So ', { type: 'argument', arg: 'wow' }, '.' ]
> parse('Such { thing }. { count, selectordinal, one {First} two {Second}' +
' few {Third} other {#th} } word.')
[ 'Such ',
{ type: 'argument', arg: 'thing' },
'. ',
{ type: 'selectordinal',
arg: 'count',
offset: 0,
cases:
[ { key: 'one', tokens: [ 'First' ] },
{ key: 'two', tokens: [ 'Second' ] },
{ key: 'few', tokens: [ 'Third' ] },
{ key: 'other', tokens: [ { type: 'octothorpe' }, 'th' ] } ] },
' word.' ]
> parse('Many{type,select,plural{ numbers}selectordinal{ counting}' +
'select{ choices}other{ some {type}}}.')
[ 'Many',
{ type: 'select',
arg: 'type',
cases:
[ { key: 'plural', tokens: [ ' numbers' ] },
{ key: 'selectordinal', tokens: [ ' counting' ] },
{ key: 'select', tokens: [ ' choices' ] },
{ key: 'other', tokens: [ ' some',
{ type: 'argument', arg: 'type' } ] } ] },
'.' ]
> parse('{Such compliance')
// SyntaxError: Expected ",", "}" or [ \t\n\r] but "c" found.
> var msg = '{words, plural, zero{No words} one{One word} other{# words}}';
> var englishKeys = { cardinal: [ 'one', 'other' ],
ordinal: [ 'one', 'two', 'few', 'other' ] };
> parse(msg)
[ { type: 'plural',
arg: 'words',
offset: 0,
cases:
[ { key: 'zero', tokens: [ 'No words' ] },
{ key: 'one', tokens: [ 'One word' ] },
{ key: 'other', tokens: [ { type: 'octothorpe' }, ' words' ] } ] } ]
> parse(msg, englishKeys)
// Error: Invalid key `zero` for argument `words`. Valid plural keys for this
// locale are `one`, `other`, and explicit keys like `=0`.
See the messageformat.js README for details.
FAQs
A PEG.js parser for ICU MessageFormat strings
The npm package messageformat-parser receives a total of 405,649 weekly downloads. As such, messageformat-parser popularity was classified as popular.
We found that messageformat-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.