UNPKG

5.15 kBMarkdownView Raw
1Intl MessageFormat Parser
2=========================
3
4Parses [ICU Message strings][ICU] into an AST via JavaScript.
5
6[![npm Version][npm-badge]][npm]
7[![Build Status][travis-badge]][travis]
8[![Dependency Status][david-badge]][david]
9
10
11Overview
12--------
13
14This package implements a parser in JavaScript that parses the industry standard [ICU Message strings][ICU] — used for internationalization — into an AST. The produced AST can then be used by a compiler, like [`intl-messageformat`][intl-mf], to produce localized formatted strings for display to users.
15
16This parser is written in [PEG.js][], a parser generator for JavaScript. This parser's implementation was inspired by and derived from Alex Sexton's [messageformat.js][] project. The differences from Alex's implementation are:
17
18- This project is standalone.
19- It's authored as ES6 modules compiled to CommonJS and the Bundle format for the browser.
20- The produced AST is more descriptive and uses recursive structures.
21- The keywords used in the AST match the ICU Message "spec".
22
23
24Usage
25-----
26
27### Loading in the Browser
28
29The `dist/` folder contains the version of this package for use in the browser, and it can be loaded and used like this:
30
31```html
32<script src="intl-messageformat-parser/dist/parser.min.js"></script>
33<script>
34 IntlMessageFormatParser.parse('...');
35</script>
36```
37
38### Loading in Node.js
39
40This package can also be `require()`-ed in Node.js:
41
42```js
43var parser = require('intl-messageformat-parser');
44parser.parse('...');
45```
46
47### Example
48
49Given an ICU Message string like this:
50
51```
52On {takenDate, date, short} {name} took {numPhotos, plural,
53 =0 {no photos.}
54 =1 {one photo.}
55 other {# photos.}
56}
57```
58
59```js
60// Assume `msg` is the string above.
61parser.parse(msg);
62```
63
64This parser will produce this AST:
65
66```json
67{
68 "type": "messageFormatPattern",
69 "elements": [
70 {
71 "type": "messageTextElement",
72 "value": "On "
73 },
74 {
75 "type": "argumentElement",
76 "id": "takenDate",
77 "format": {
78 "type": "dateFormat",
79 "style": "short"
80 }
81 },
82 {
83 "type": "messageTextElement",
84 "value": " "
85 },
86 {
87 "type": "argumentElement",
88 "id": "name",
89 "format": null
90 },
91 {
92 "type": "messageTextElement",
93 "value": " took "
94 },
95 {
96 "type": "argumentElement",
97 "id": "numPhotos",
98 "format": {
99 "type": "pluralFormat",
100 "offset": 0,
101 "options": [
102 {
103 "type": "optionalFormatPattern",
104 "selector": "=0",
105 "value": {
106 "type": "messageFormatPattern",
107 "elements": [
108 {
109 "type": "messageTextElement",
110 "value": "no photos."
111 }
112 ]
113 }
114 },
115 {
116 "type": "optionalFormatPattern",
117 "selector": "=1",
118 "value": {
119 "type": "messageFormatPattern",
120 "elements": [
121 {
122 "type": "messageTextElement",
123 "value": "one photo."
124 }
125 ]
126 }
127 },
128 {
129 "type": "optionalFormatPattern",
130 "selector": "other",
131 "value": {
132 "type": "messageFormatPattern",
133 "elements": [
134 {
135 "type": "messageTextElement",
136 "value": "# photos."
137 }
138 ]
139 }
140 }
141 ]
142 }
143 }
144 ]
145}
146```
147
148
149License
150-------
151
152This software is free to use under the Yahoo! Inc. BSD license.
153See the [LICENSE file][] for license text and copyright information.
154
155
156[npm]: https://www.npmjs.org/package/intl-messageformat-parser
157[npm-badge]: https://img.shields.io/npm/v/intl-messageformat-parser.svg?style=flat-square
158[david]: https://david-dm.org/formatjs/formatjs
159[david-badge]: https://img.shields.io/david/formatjs/formatjs.svg?style=flat-square
160[travis]: https://travis-ci.org/formatjs/formatjs
161[travis-badge]: https://img.shields.io/travis/formatjs/formatjs.svg?style=flat-square
162[ICU]: http://userguide.icu-project.org/formatparse/messages
163[intl-mf]: https://github.com/yahoo/intl-messageformat
164[PEG.js]: https://pegjs.org/
165[messageformat.js]: https://github.com/SlexAxton/messageformat.js
166[LICENSE file]: https://github.com/formatjs/formatjs/blob/master/LICENSE