Skip to content
This repository was archived by the owner on Dec 17, 2018. It is now read-only.

Commit a45538e

Browse files
committed
Add README.md
1 parent bcb7577 commit a45538e

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# messageformat-parser
2+
3+
A PEG.js parser for [ICU MessageFormat](https://messageformat.github.io/guide/))
4+
strings -- part of [messageformat.js](https://messageformat.github.io/). Outputs
5+
an AST defined by [parser.pegjs](./parser.pegjs).
6+
7+
The generated parser function takes two parameters, first the string to be
8+
parsed, and a second optional parameter `options`, an object containing arrays
9+
of keywords for `cardinal` and `ordinal` rules for the current locale -- these
10+
are used to validate plural and selectordinal keys. If `options` or its fields
11+
are missing or set to false, the full set of valid Unicode CLDR keys is used:
12+
`'zero', 'one', 'two', 'few', 'many', 'other'`. To disable this check, pass in
13+
an empty array.
14+
15+
16+
#### Installation
17+
18+
```sh
19+
npm install messageformat-parser
20+
```
21+
22+
23+
#### Usage
24+
25+
```js
26+
> var parse = require('messageformat-parser').parse;
27+
28+
> parse('So {wow}.')
29+
[ 'So ', { type: 'argument', arg: 'wow' }, '.' ]
30+
31+
> parse('Such { thing }. { count, selectordinal, one {First} two {Second}' +
32+
' few {Third} other {#th} } word.')
33+
[ 'Such ',
34+
{ type: 'argument', arg: 'thing' },
35+
'. ',
36+
{ type: 'selectordinal',
37+
arg: 'count',
38+
offset: 0,
39+
cases:
40+
[ { key: 'one', tokens: [ 'First' ] },
41+
{ key: 'two', tokens: [ 'Second' ] },
42+
{ key: 'few', tokens: [ 'Third' ] },
43+
{ key: 'other', tokens: [ { type: 'octothorpe' }, 'th' ] } ] },
44+
' word.' ]
45+
46+
> parse('Many{type,select,plural{ numbers}selectordinal{ counting}' +
47+
'select{ choices}other{ some {type}}}.')
48+
[ 'Many',
49+
{ type: 'select',
50+
arg: 'type',
51+
cases:
52+
[ { key: 'plural', tokens: [ ' numbers' ] },
53+
{ key: 'selectordinal', tokens: [ ' counting' ] },
54+
{ key: 'select', tokens: [ ' choices' ] },
55+
{ key: 'other', tokens: [ ' some',
56+
{ type: 'argument', arg: 'type' } ] } ] },
57+
'.' ]
58+
59+
> parse('{Such compliance')
60+
// SyntaxError: Expected ",", "}" or [ \t\n\r] but "c" found.
61+
62+
> var msg = '{words, plural, zero{No words} one{One word} other{# words}}';
63+
> var englishKeys = { cardinal: [ 'one', 'other' ],
64+
ordinal: [ 'one', 'two', 'few', 'other' ] };
65+
> parse(msg)
66+
[ { type: 'plural',
67+
arg: 'words',
68+
offset: 0,
69+
cases:
70+
[ { key: 'zero', tokens: [ 'No words' ] },
71+
{ key: 'one', tokens: [ 'One word' ] },
72+
{ key: 'other', tokens: [ { type: 'octothorpe' }, ' words' ] } ] } ]
73+
74+
> parse(msg, englishKeys)
75+
// Error: Invalid key `zero` for argument `words`. Valid plural keys for this
76+
// locale are `one`, `other`, and explicit keys like `=0`.
77+
```

0 commit comments

Comments
 (0)