icu-messageformat-parser is a JavaScript library that provides a PEG.js-based parser for ICU MessageFormat strings. It transforms a given MessageFormat string into an Abstract Syntax Tree (AST), enabling programmatic manipulation or interpretation of localized messages. The current stable version is 4.0.0. New major versions are released periodically to introduce breaking changes, often related to stricter conformance with the ICU MessageFormat specification, and to expand parsing capabilities. Key differentiators include its robust AST output, configurable strictness options (e.g., for number signs and function parameters), and its role as a fundamental parsing component for internationalization workflows involving MessageFormat, prioritizing accurate parsing according to Unicode CLDR and ICU standards.
npm install icu-messageformat-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic parsing, complex selectordinal statements, nested selects, and usage of options for plural key validation and strict mode for '#' parsing.
Replace backslash escapes with single quotes for literal text, e.g., `\{` should become `'{'`. A codemod was provided with v3.0.0 to assist with this migration.Replace `{ strictNumberSign: true }` with `{ strict: true }` in your parser options. The `strict` option now encompasses more strict parsing behaviors.Review code that processes function parameters from the AST, as their structure might have changed to accommodate nested MessageFormat elements. If you relied on parameters being simple strings, adjust your logic accordingly.
Ensure that the `options` object, specifically `options.cardinal` and `options.ordinal`, contains all plural and selectordinal keys expected for your target locales. To disable this validation, pass an empty array for these options.
Be explicit about the `strictFunctionParams` option. If you expect an array of trimmed strings, ensure this option is `false` (default). If you need the raw parameter string, set it to `true` and handle parsing manually.
Always use double apostrophes (`''`) for a literal apostrophe within quoted text. For literal text that contains curly braces or `#`, enclose it in single quotes, e.g., `'{literal text}'`.Review the MessageFormat string for common syntax errors like `{Such compliance` missing a closing brace or incorrect separator characters. Ensure all arguments and elements are correctly formatted according to the ICU MessageFormat specification.Check the `cardinal` and `ordinal` arrays in the parser `options` object. Ensure they contain all the plural rule keys used in your MessageFormat string for the target locale. If 'zero' is used but not listed in `options.cardinal`, it will cause this error.
For versions 3.0.0 and above, use ES Module `import` syntax: `import { parse } from 'icu-messageformat-parser';`. If you are in a CommonJS environment, ensure your `package.json` does not have `"type": "module"` or configure your build system to correctly transpile.Ensure you are using named import syntax: `import { parse } from 'icu-messageformat-parser';`. If using a bundler like Webpack or Rollup, verify its configuration for ES Module resolution and tree-shaking.No dependency data recorded yet.