|
| 1 | +# yaml-to-messageformat |
| 2 | + |
| 3 | +Converts yaml input (e.g. as used by Rails i18n) into [messageformat]-compatible |
| 4 | +JSON. |
| 5 | + |
| 6 | +### Installation |
| 7 | + |
| 8 | +```sh |
| 9 | +npm install yaml-to-messageformat |
| 10 | +``` |
| 11 | +or |
| 12 | +```sh |
| 13 | +yarn add yaml-to-messageformat |
| 14 | +``` |
| 15 | + |
| 16 | +If using in an environment that does not natively support ES6 features such as |
| 17 | +object destructuring and arrow functions, you'll want to use a transpiler for this. |
| 18 | + |
| 19 | + |
| 20 | +### Usage |
| 21 | + |
| 22 | +```js |
| 23 | +const convert = require('yaml-to-messageformat') |
| 24 | +const { locales, translations } = convert(` |
| 25 | +en: |
| 26 | + format: "%{attribute} %{message}" |
| 27 | + errors: |
| 28 | + confirmation: "doesn't match %{attribute}" |
| 29 | + accepted: "must be accepted" |
| 30 | + wrong_length: |
| 31 | + one: "is the wrong length (should be 1 character)" |
| 32 | + other: "is the wrong length (should be %{count} characters)" |
| 33 | + equal_to: "must be equal to %{count}" |
| 34 | +`) |
| 35 | + |
| 36 | +const MessageFormat = require('messageformat') |
| 37 | +const mf = new MessageFormat(locales) |
| 38 | +const messages = mf.compile(translations) |
| 39 | + |
| 40 | +messages.en.errors.accepted() |
| 41 | +// 'must be accepted' |
| 42 | + |
| 43 | +messages.en.format({ |
| 44 | + attribute: 'Problem', |
| 45 | + message: messages.en.errors.confirmation({ attribute: 'your style' }) |
| 46 | +}) |
| 47 | +// 'Problem doesn\'t match your style' |
| 48 | + |
| 49 | +messages.en.errors.wrong_length({ count: 42 }) |
| 50 | +// 'is the wrong length (should be 42 characters)' |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +### API: `convert(input, options)` |
| 55 | + |
| 56 | +`input` should be a string; `options` is an optional set of configuration for |
| 57 | +the [js-yaml] parser, as well as yaml-to-messageformat itself: |
| 58 | + |
| 59 | +- `filename`, `json`, `onWarning`, `schema` – See the [js-yaml documentation] |
| 60 | + |
| 61 | +- `defaultLocale` (string, default `'en'`) – Sets the initial locale. |
| 62 | + |
| 63 | +- `includeLocales` (array of strings, default `null`) – By default any key in the |
| 64 | + input data that matches the two-letter code of a [CLDR pluralisation language] |
| 65 | + switches the language used for determining the pluralisation rules. Set this to |
| 66 | + some limited set of languages (or even an empty array) to limit that. |
| 67 | + |
| 68 | +- `verbose` (boolean, default `false`) – If set to `true`, some logging and |
| 69 | + warnings will be printed to the console. |
| 70 | + |
| 71 | +For more options, take a look at the [source](./index.js). |
| 72 | + |
| 73 | +The object returned by the function contains the following fields: |
| 74 | + |
| 75 | +- `locales` (array of strings) – The actual locales encountered in the data |
| 76 | + |
| 77 | +- `translations` (object) – An object containing the MessageFormat strings, |
| 78 | + matching the shape of the input data |
| 79 | + |
| 80 | +[CLDR pluralisation language]: http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html |
| 81 | +[messageformat]: https://messageformat.github.io/ |
| 82 | +[js-yaml]: https://github.com/nodeca/js-yaml |
| 83 | +[js-yaml documentation]: https://github.com/nodeca/js-yaml#safeload-string---options- |
0 commit comments