|
1 | | -# A Polyfill for Intl.MessageFormat |
| 1 | +# MessageFormat 2 for JavaScript |
2 | 2 |
|
3 | | -This library provides a runtime for the [ECMA-402 Intl.MessageFormat proposal], |
4 | | -which is built on top of the developing [Unicode MessageFormat 2.0 specification], "MF2". |
| 3 | +This library provides an implementation of the [ECMA-402 Intl.MessageFormat proposal], |
| 4 | +which is built on top of the [Unicode MessageFormat 2.0 specification] (MF2), |
| 5 | +developed by the [MessageFormat Working Group]. |
5 | 6 |
|
6 | | -[ecma-402 intl.messageformat proposal]: https://github.com/dminor/proposal-intl-messageformat/ |
7 | | -[unicode messageformat 2.0 specification]: https://github.com/unicode-org/message-format-wg |
8 | | - |
9 | | -> **NOTE**: This means that the v4 release of the `messageformat` package has |
10 | | -> an entirely different API compared to its earlier major releases, |
11 | | -> which were built on top of ICU MessageFormat, aka "MF1". |
12 | | -> For that, |
13 | | -> please see [`@messageformat/core`](https://www.npmjs.com/package/@messageformat/core) instead. |
| 7 | +The API provided by this library is current as of the [LDML 46.1] (December 2024)G |
| 8 | +version of the MF2 specification. |
14 | 9 |
|
15 | | -## Usage |
| 10 | +[ecma-402 intl.messageformat proposal]: https://github.com/dminor/proposal-intl-messageformat/ |
| 11 | +[unicode messageformat 2.0 specification]: https://unicode.org/reports/tr35/tr35-messageFormat.html |
| 12 | +[messageformat working group]: https://github.com/unicode-org/message-format-wg |
| 13 | +[LDML 46.1]: https://www.unicode.org/reports/tr35/tr35-74/tr35-messageFormat.html |
16 | 14 |
|
17 | 15 | ```sh |
18 | 16 | npm install --save-exact messageformat@next |
19 | 17 | ``` |
20 | 18 |
|
21 | 19 | ```js |
22 | 20 | import { MessageFormat } from 'messageformat'; |
23 | | -Intl.MessageFormat = MessageFormat; |
| 21 | + |
| 22 | +const msg = 'Today is {$today :datetime dateStyle=medium}'; |
| 23 | +const mf = new MessageFormat('en', msg); |
| 24 | + |
| 25 | +mf.format({ today: new Date('2022-02-02') }); |
| 26 | +// 'Today is Feb 2, 2022' |
24 | 27 | ``` |
25 | 28 |
|
26 | | -In addition to supporting MF2 syntax, |
27 | | -compilers and formatting function runtimes are also provided for |
28 | | -ICU MessageFormat and Fluent messages: |
| 29 | +The library also provides a number of other tools and utilities for MF2, such as: |
29 | 30 |
|
30 | | -- [@messageformat/icu-messageformat-1](https://www.npmjs.com/package/@messageformat/icu-messageformat-1) |
31 | | -- [@messageformat/fluent](https://www.npmjs.com/package/@messageformat/fluent) |
| 31 | +- MF2 data model conversion tools |
32 | 32 |
|
33 | | -## API |
| 33 | + ```js |
| 34 | + import { parseMessage, stringifyMessage } from 'messageformat'; |
| 35 | + ``` |
34 | 36 |
|
35 | | -The API provided by this Intl.MessageFormat polyfill is current as of |
36 | | -[2022-07-13](https://github.com/tc39/proposal-intl-messageformat/blob/72eefa5/README.md). |
37 | | -The static `MessageFormat.parseResource()` method is not yet provided, |
38 | | -as the message resource syntax is still under development. |
| 37 | +- MF2 data model validation and transformation tools |
39 | 38 |
|
40 | | -```js |
41 | | -const locale = 'en-US'; |
42 | | -const msg = '{Today is {$today :datetime dateStyle=medium}}'; |
| 39 | + ```js |
| 40 | + import { validate, visit } from 'messageformat'; |
| 41 | + ``` |
43 | 42 |
|
44 | | -const mf = new Intl.MessageFormat(msg, locale); |
| 43 | +- Concreate Syntax Tree (CST) tools for MF2 |
| 44 | + ```js |
| 45 | + import { parseCST, messageFromCST, stringifyCST } from 'messageformat'; |
| 46 | + ``` |
45 | 47 |
|
46 | | -mf.format({ today: new Date('2022-02-02') }); |
47 | | -// 'Today is Feb 2, 2022' |
48 | | -``` |
| 48 | +In addition to supporting MF2 syntax, |
| 49 | +compilers and formatting function runtimes are also provided for |
| 50 | +ICU MessageFormat and Fluent messages: |
| 51 | + |
| 52 | +- [@messageformat/icu-messageformat-1](https://www.npmjs.com/package/@messageformat/icu-messageformat-1) |
| 53 | +- [@messageformat/fluent](https://www.npmjs.com/package/@messageformat/fluent) |
49 | 54 |
|
50 | 55 | For more information on additional types and functions provided by this package, |
51 | 56 | see the [API documentation site](https://messageformat.github.io/messageformat/api/). |
| 57 | + |
| 58 | +> [!IMPORTANT] |
| 59 | +> This means that the v4 release of the `messageformat` package has |
| 60 | +> an entirely different API compared to its earlier major releases, |
| 61 | +> which were built on top of ICU MessageFormat, aka "MF1". |
| 62 | +> For that, |
| 63 | +> please see [`@messageformat/core`](https://www.npmjs.com/package/@messageformat/core) instead. |
0 commit comments