|
| 1 | +--- |
| 2 | +og:title: ICU MessageFormat v1 Plugin |
| 3 | +og:description: Storage plugin for inlang that reads and writes ICU MessageFormat v1 JSON files. Supports selects, plurals, selectordinal, offsets, and formatter styles. |
| 4 | +--- |
| 5 | + |
| 6 | +# ICU MessageFormat v1 Plugin |
| 7 | + |
| 8 | +The ICU MessageFormat v1 plugin is a storage plugin for inlang. It reads and writes ICU1 strings in JSON files per locale. |
| 9 | +It targets ICU MessageFormat v1 (a.k.a. ICU MessageFormat) and maps it onto inlang's data model so selectors and variants are preserved. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- Parses ICU1 messages using `@messageformat/parser`. |
| 14 | +- Supports `select`, `plural`, and `selectordinal`, including exact matches (`=n`) and `offset`. |
| 15 | +- Supports `#` (octothorpe) inside plural/selectordinal cases. |
| 16 | +- Supports formatter functions (e.g. `number`, `date`, `time`, `spellout`, `ordinal`, `duration`) with style parameters. |
| 17 | +- Exports ICU1 strings back from inlang data. |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Install the plugin in your Inlang Project by adding it to your `modules` in `project.inlang/settings.json` and configure `pathPattern`. |
| 22 | + |
| 23 | +```diff |
| 24 | +// project.inlang/settings.json |
| 25 | +{ |
| 26 | + "modules": [ |
| 27 | ++ "https://cdn.jsdelivr.net/npm/@inlang/plugin-icu1@latest/dist/index.js" |
| 28 | + ], |
| 29 | ++ "plugin.inlang.icu-messageformat-1": { |
| 30 | ++ "pathPattern": "./messages/{locale}.json" |
| 31 | ++ } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## Configuration |
| 36 | + |
| 37 | +Configuration happens in `project.inlang/settings.json` under `"plugin.inlang.icu-messageformat-1"`. |
| 38 | + |
| 39 | +### `pathPattern` |
| 40 | + |
| 41 | +You can define a single `pathPattern` or provide an array of patterns. The placeholder should be `{locale}`. |
| 42 | + |
| 43 | +#### Single path pattern example |
| 44 | + |
| 45 | +```json |
| 46 | +{ |
| 47 | + "plugin.inlang.icu-messageformat-1": { |
| 48 | + "pathPattern": "./messages/{locale}.json" |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +#### Multiple path patterns example |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "plugin.inlang.icu-messageformat-1": { |
| 58 | + "pathPattern": ["./defaults/{locale}.json", "./product/{locale}.json"] |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +> [!NOTE] |
| 64 | +> When exporting, all messages are written to every path pattern in the array (one file per pattern and locale). Multiple patterns are a one-way merge for import. |
| 65 | +
|
| 66 | +## Messages |
| 67 | + |
| 68 | +ICU1 files contain key-value pairs with ICU MessageFormat strings. |
| 69 | + |
| 70 | +```json |
| 71 | +// messages/en.json |
| 72 | +{ |
| 73 | + "hello_world": "Hello World!", |
| 74 | + "greeting": "Good morning {name}!", |
| 75 | + "likes": "You have {count, plural, one {# like} other {# likes}}" |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Plurals and selectordinal |
| 80 | + |
| 81 | +```json |
| 82 | +{ |
| 83 | + "items": "{count, plural, offset:1 =0 {no items} one {# item} other {# items}}", |
| 84 | + "rank": "{place, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}" |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### Select |
| 89 | + |
| 90 | +```json |
| 91 | +{ |
| 92 | + "gender": "{gender, select, male {He} female {She} other {They}}" |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Escaping |
| 97 | + |
| 98 | +ICU1 uses apostrophes to escape literals. To include literal braces, wrap them in apostrophes: |
| 99 | + |
| 100 | +- `'{' |
| 101 | +- `'}'` |
| 102 | + |
| 103 | +If you need a literal apostrophe, use `''` (two single quotes). |
| 104 | + |
| 105 | +## Caveats |
| 106 | + |
| 107 | +- Export is canonicalized. Output is semantically equivalent, but whitespace and formatting may differ from the original source. |
| 108 | +- Tags/markup are treated as plain text by the parser. |
0 commit comments