Skip to content
This repository was archived by the owner on Jan 3, 2019. It is now read-only.

Commit 2093ecb

Browse files
authored
Add example & update readme (#3)
1 parent 0b49ad5 commit 2093ecb

File tree

7 files changed

+130
-10
lines changed

7 files changed

+130
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ node_modules
1111

1212
# Optional REPL history
1313
.node_repl_history
14+
15+
example/dist/bundle.js
16+
example/package-lock.json

README.md

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
# messageformat.js loader for webpack
1+
# ICU MessageFormat loader for webpack
22

3-
## Dependencies
43

5-
* Requires [messageformat.js](https://github.com/messageformat/messageformat.js) 1.0.0 or higher
6-
7-
## Install
4+
## Installation
85

96
```
10-
npm install messageformat-loader
7+
npm install messageformat messageformat-loader
118
```
129

10+
1311
## Usage
1412

15-
[Documentation: Using loaders](https://webpack.js.org/concepts/loaders/#using-loaders)
13+
For a working demo of the loader, run `npm install` in the `example/` directory, and then open `example/dist/index.html` in a browser.
14+
15+
### webpack.config.js
16+
17+
```js
18+
{
19+
test: /\bmessages\.json$/,
20+
loader: require.resolve('messageformat-loader'),
21+
options: {
22+
biDiSupport: false,
23+
disablePluralKeyChecks: false,
24+
formatters: null,
25+
intlSupport: false,
26+
locale: ['en'],
27+
strictNumberSign: false
28+
}
29+
}
30+
```
1631

17-
[Documentation: messageformat.js](https://messageformat.github.io/)
32+
The default option values are shown, and are not required. [See below](#options) for more information on them. As Webpack v1 does not support loader options, you should instead pass the options as query parameters in that environment; `locale` will accept a comma-delimited set of values.
1833

1934
### messages.json
2035

@@ -30,22 +45,39 @@ npm install messageformat-loader
3045

3146
### example.js
3247

33-
``` javascript
48+
ES6, with configuration:
49+
```js
50+
import messages from './messages.json'
51+
messages['ordinal-example']({ N: 1 })
52+
// => 'The 1st message.'
53+
```
54+
55+
ES5, without configuration:
56+
```js
3457
var messages = require('messageformat-loader?locale=en!./messages.json');
3558
messages['ordinal-example']({ N: 1 });
3659
// => 'The 1st message.'
3760
```
3861

62+
3963
## Options
4064

41-
* [`locale`](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html#MessageFormat) The [CLDR language code](http://www.unicode.org/cldr/charts/29/supplemental/language_territory_information.html) to pass to [messageformat.js](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html). Defaults to `en`.
65+
* [`locale`](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html#MessageFormat) The [CLDR language code](http://www.unicode.org/cldr/charts/29/supplemental/language_territory_information.html) or codes to pass to [messageformat.js](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html). If using multiple locales at the same time, exact matches to a locale code in the data structure keys will select that locale within it (as in [`example/src/messages.json`](example/src/messages.json)). Defaults to `en`.
4266
* [`disablePluralKeyChecks`](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html#disablePluralKeyChecks) By default, messageformat.js throws an error when a statement uses a non-numerical key that will never be matched as a pluralization category for the current locale. Use this argument to disable the validation and allow unused plural keys. Defaults to `false`.
4367
* [`intlSupport`](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html#setIntlSupport) Enable or disable support for the default formatters, which require the Intl object. Defaults to `false`.
4468
* [`biDiSupport`](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html#setBiDiSupport) Enable or disable the addition of Unicode control characters to all input to preserve the integrity of the output when mixing LTR and RTL text. Defaults to `false`.
4569
* [`formatters`](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html#addFormatters) Add custom formatter functions to this MessageFormat instance.
4670
* [`strictNumberSign`](https://messageformat.github.io/messageformat.js/doc/MessageFormat.html#setStrictNumberSign) Follow the stricter ICU MessageFormat spec and throw a runtime error if # is used with non-numeric input. Defaults to `false`.
4771

4872

73+
## Links
74+
75+
- [messageformat](https://messageformat.github.io/)
76+
- Loaders:
77+
- [Webpack v1](https://webpack.github.io/docs/using-loaders.html)
78+
- [Webpack v2/3](https://webpack.js.org/concepts/loaders/)
79+
80+
4981
## License
5082

5183
MIT

example/dist/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>Getting Started</title>
6+
</head>
7+
<body>
8+
<script src="bundle.js"></script>
9+
</body>
10+
</html>

example/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "messageformat-loader-example",
3+
"version": "0.1.0",
4+
"description": "Small example for messageformat-loader",
5+
"scripts": {
6+
"build": "webpack",
7+
"postinstall": "cd .. && npm install --no-save messageformat",
8+
"prepare": "npm run build"
9+
},
10+
"main": "src/index.js",
11+
"repository": "https://github.com/eemeli/messageformat-loader",
12+
"author": "Eemeli Aro <[email protected]>",
13+
"license": "MIT",
14+
"dependencies": {
15+
"messageformat": "^1.0.2",
16+
"messageformat-loader": "file:../",
17+
"webpack": "^3.8.1"
18+
}
19+
}

example/src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import messages from './messages.json'
2+
3+
function component() {
4+
const element = document.createElement('div')
5+
element.innerHTML = [
6+
'In English: ' + messages.en.select({ GENDER: 'female' }),
7+
'Suomeksi: ' + messages.fi.select({ GENDER: 'female' })
8+
].join('<br/>')
9+
return element
10+
}
11+
12+
console.log('messages', messages)
13+
document.body.appendChild(component())

example/src/messages.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"en": {
3+
"simple": "A simple message.",
4+
"var": "Message with {X}.",
5+
"plural": "You have {N, plural, =0{no messages} one{1 message} other{# messages}}.",
6+
"select": "{GENDER, select, male{He has} female{She has} other{They have}} sent you a message.",
7+
"ordinal": "The {N, selectordinal, one{1st} two{2nd} few{3rd} other{#th}} message."
8+
},
9+
"fi": {
10+
"simple": "Yksinkertainen viesti.",
11+
"var": "Viesti jossa {X}.",
12+
"plural": "Sinulla {N, plural, =0{ei ole viestejä} one{on 1 viesti} other{on # viestiä}}.",
13+
"select": "{GENDER, select, other{Hän}} lähetti sinulle viestin.",
14+
"ordinal": "{N, selectordinal, other{#.}} viesti."
15+
}
16+
}

example/webpack.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const path = require('path')
2+
3+
const locale = ['en', 'fi']
4+
5+
module.exports = {
6+
entry: './src/index.js',
7+
output: {
8+
filename: 'bundle.js',
9+
path: path.resolve(__dirname, 'dist')
10+
},
11+
module: {
12+
rules: [
13+
{
14+
test: /\bmessages\.json$/,
15+
loader: require.resolve('messageformat-loader'),
16+
options: {
17+
biDiSupport: false,
18+
disablePluralKeyChecks: false,
19+
formatters: null,
20+
intlSupport: false,
21+
locale,
22+
strictNumberSign: false
23+
}
24+
}
25+
]
26+
}
27+
}

0 commit comments

Comments
 (0)