Skip to content

Commit 9fc108c

Browse files
committed
Make example code less node-y
1 parent 0073731 commit 9fc108c

2 files changed

Lines changed: 34 additions & 32 deletions

File tree

build.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@ title: Build-time Compilation
66
For a significant decrease in filesize and execution time, you should precompile your messages to JavaScript during your build phase. It works like this:
77

88
```js
9-
> var mf = new MessageFormat('en');
10-
> var messages = {
11-
simple: 'A simple message.',
12-
var: 'Message with {X}.',
13-
plural: 'You have {N, plural, =0{no messages} one{1 message} other{# messages}}.',
14-
select: '{GENDER, select, male{He has} female{She has} other{They have}} sent you a message.',
15-
ordinal: 'The {N, selectordinal, one{1st} two{2nd} few{3rd} other{#th}} message.' };
16-
17-
> var mfunc = mf.compile(messages);
18-
> mfunc().ordinal({N:1})
19-
'The 1st message.'
20-
21-
> var efunc = new Function('return (' + mfunc.toString() + ')()');
22-
> efunc()
23-
{ simple: [Function],
24-
var: [Function],
25-
plural: [Function],
26-
select: [Function],
27-
ordinal: [Function] }
28-
> efunc().ordinal({N:2})
29-
'The 2nd message.'
9+
var mf = new MessageFormat('en');
10+
var messages = {
11+
simple: 'A simple message.',
12+
var: 'Message with {X}.',
13+
plural: 'You have {N, plural, =0{no messages} one{1 message} other{# messages}}.',
14+
select: '{GENDER, select, male{He has} female{She has} other{They have}} sent you a message.',
15+
ordinal: 'The {N, selectordinal, one{1st} two{2nd} few{3rd} other{#th}} message.'
16+
};
17+
18+
var mfunc = mf.compile(messages);
19+
mfunc().ordinal({ N: 1 })
20+
// "The 1st message."
21+
22+
var efunc = new Function('return (' + mfunc.toString() + ')()');
23+
efunc()
24+
// { simple: [Function],
25+
// var: [Function],
26+
// plural: [Function],
27+
// select: [Function],
28+
// ordinal: [Function] }
29+
30+
efunc().ordinal({N:2})
31+
// "The 2nd message."
3032
```
3133

3234
Note that as `efunc` is defined as a `new Function()`, it has no access to the surrounding scope; the output of `mfunc().toString()` can be saved as a file and later included with `require()` or `<script src=...>`, providing access to the compiled functions that is completely independent of messageformat.js, or any other dependencies.
@@ -36,7 +38,7 @@ Note that as `efunc` is defined as a `new Function()`, it has no access to the s
3638

3739
A [CLI compiler](https://github.com/SlexAxton/messageformat.js/tree/master/bin/messageformat.js) is also included, available as `./node_modules/.bin/messageformat` or just `messageformat` when installed with `npm install -g`.
3840

39-
```
41+
```text
4042
$ messageformat --help
4143
Usage: messageformat -l [locale] [OPTIONS] [INPUT_DIR] [OUTPUT_DIR]
4244

index.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Using messageformat.js, you can separate your code from your text formatting, wh
2424
With this message:
2525

2626
```js
27-
> var msg =
27+
var msg =
2828
'{GENDER, select, male{He} female{She} other{They} }' +
2929
' found ' +
3030
'{RES, plural, =0{no results} one{1 result} other{# results} }' +
@@ -36,19 +36,19 @@ With this message:
3636
You'll get these results:
3737

3838
```js
39-
> var mfunc = new MessageFormat('en').compile(msg);
39+
var mfunc = new MessageFormat('en').compile(msg);
4040

41-
> mfunc({ GENDER: 'male', RES: 1, CAT: 2 })
42-
'He found 1 result in the 2nd category.'
41+
mfunc({ GENDER: 'male', RES: 1, CAT: 2 })
42+
// 'He found 1 result in the 2nd category.'
4343

44-
> mfunc({ GENDER: 'female', RES: 1, CAT: 2 })
45-
'She found 1 result in the 2nd category.'
44+
mfunc({ GENDER: 'female', RES: 1, CAT: 2 })
45+
// 'She found 1 result in the 2nd category.'
4646

47-
> mfunc({ GENDER: 'male', RES: 2, CAT: 1 })
48-
'He found 2 results in the 1st category.'
47+
mfunc({ GENDER: 'male', RES: 2, CAT: 1 })
48+
// 'He found 2 results in the 1st category.'
4949

50-
> mfunc({ RES: 2, CAT: 2 })
51-
'They found 2 results in the 2nd category.'
50+
mfunc({ RES: 2, CAT: 2 })
51+
// 'They found 2 results in the 2nd category.'
5252
```
5353

5454

0 commit comments

Comments
 (0)