|
| 1 | +# Property Resource Bundle Loader for Webpack |
| 2 | + |
| 3 | +Loads .properties files into JavaScript as precompiled functions using |
| 4 | +[dot-properties] and [messageformat]. |
| 5 | + |
| 6 | +Property values are parsed directly as ICU MessageFormat. With the default |
| 7 | +options, will assume that the filename has `_` separated parts, of which the |
| 8 | +second is the two- or three-letter language code [as in Java Resource Bundles](https://docs.oracle.com/javase/9/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-java.util.Locale-java.lang.ClassLoader-) |
| 9 | + |
| 10 | +[dot-properties]: https://www.npmjs.com/package/dot-properties |
| 11 | +[messageformat]: https://messageformat.github.io/ |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```sh |
| 16 | +npm install messageformat-properties-loader |
| 17 | +``` |
| 18 | +or |
| 19 | +```sh |
| 20 | +yarn add messageformat-properties-loader |
| 21 | +``` |
| 22 | + |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +For a working demo of the following, run `npm install` in the |
| 27 | +[`example/`](./example/) directory, and then open `example/dist/index.html` in |
| 28 | +a browser. |
| 29 | + |
| 30 | + |
| 31 | +#### Webpack configuration |
| 32 | + |
| 33 | +```js |
| 34 | +{ |
| 35 | + test: /\.properties$/, |
| 36 | + loader: require.resolve('messageformat-properties-loader'), |
| 37 | + options: { |
| 38 | + biDiSupport: false, // enables bi-directional text support |
| 39 | + defaultLocale: 'en', // used if resolution from filename fails |
| 40 | + keyPath: false, // if true, dots '.' key names will result |
| 41 | + // in multi-level objects -- use a string |
| 42 | + // value to customize |
| 43 | + pathSep: '_' // separator for parsing locale from filename |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Default option values are shown above, though none is required. |
| 49 | + |
| 50 | + |
| 51 | +#### messages_en.properties |
| 52 | + |
| 53 | +``` |
| 54 | +errors.format: {0} {1} |
| 55 | +errors.messages.confirmation: doesn't match {attribute} |
| 56 | +errors.messages.accepted: must be accepted |
| 57 | +errors.messages.wrong_length: is the wrong length (should be {count, plural, one{1 character} other{# characters}}) |
| 58 | +errors.messages.equal_to: must be equal to {count} |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | +#### example.js |
| 63 | + |
| 64 | +```js |
| 65 | +import messages from './messages_en.properties' |
| 66 | +const { format, messages: errors } = messages.errors |
| 67 | + |
| 68 | +errors.accepted() |
| 69 | +// 'must be accepted' |
| 70 | + |
| 71 | +format([ |
| 72 | + 'Your message', |
| 73 | + errors.wrong_length({ count: 42 }) |
| 74 | +]) |
| 75 | +// 'Your message is the wrong length (should be 42 characters)' |
| 76 | +``` |
0 commit comments