包详细信息

format-message-parse

format-message97kMIT6.2.4

Parse ICU MessageFormat pattern strings to a compact ast

format-message, message-format, internationalization, i18n

自述文件

format-message-parse

Parse ICU MessageFormat pattern strings to a compact ast

npm Version JS Standard Style MIT License

Turns a ICU Message Format string:

`You have {
  numBananas, plural,
     =0 {no bananas}
    one {a banana}
  other {# bananas}
} for sale`

into a compact format-message ast:

[ "You have ", [ "numBananas", "plural", 0, {
     "=0": [ "no bananas" ],
    "one": [ "a banana" ],
  "other": [ [ '#' ], " bananas" ]
} ], " for sale." ]

Quick Examples

npm install format-message-parse --save

import parse from 'format-message-parse'
import interpret from 'format-message-interpret'

interpret('en', parse('Hello, {name}!'))({ name: 'Jane' })

API

parse(pattern: string, tokens?: ?Token[]): AST

Generate a compact array-based AST from an ICU MessageFormat string pattern. If an empty tokens array is passed in, it will be filled with found tokens.

This can throw a SyntaxError if the pattern is not valid. The offset property of the error lets you know how far into the pattern tokenization was able to go before the error. The tokens array will have all the found tokens up until the bad syntax.

Note that the only semantic validation done in parsing is ensuring that select, selectordinal, and plural include an other sub-message. It does not validate that a plural keyword applies to the locale, or that a style is supported by the type, or even that the type will be supported by the interpreter. Successful parsing is not a guarantee the final message will format as expected.

SyntaxError

class SyntaxError extends Error {
  name: 'SyntaxError';
  message: string;
  expected: ?string;
  found: ?string;
  offset: number;
  line: number;
  column: number;
}

License

This software is free to use under the MIT license. See the LICENSE-MIT file for license text and copyright information.

更新日志

Changelog

6.2.0

Add TypeScript type definitions.

6.0.3

Use var declarations for wider compatibility.

6.0.0

Breaking Change You can no longer require('format-message-parse/tokens'). Instead, you can pass an empty array in the options argument to parse(pattern, { tokens }). Even when the method throws, the tokens array will have been filled with all found tokens prior to the bad syntax. Token types have also changed.

Validation of placeholder types is much looser, so additional validation of the type (is there a builtin or custom formatter for it?), as well as validation of sub-message keywords (does this locale have a "two" plural rule?) is recommended after parsing.

New Feature parse now allows any argument type within a placeholder, as long as the name does not include any syntax characters ({},#') or whitespace. This is to support custom placeholder types that are set up with the interpreter.

parse now handles very simple xml/html-like tags to support rich formatting.

Polish Better code reuse internally, let and const declarations instead of var.

5.0.0

This has been versioned only to match versions with related libraries. There are no changes to the functionality.

4.1.0

  • New Feature
    • Add format-message-parse/tokens to get a list of tokens in a message
    • tokens still returns a partial list when an error is encountered
  • Bug Fix
    • Improve whitespace character check