You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: build.md
+24-22Lines changed: 24 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,27 +6,29 @@ title: Build-time Compilation
6
6
For a significant decrease in filesize and execution time, you should precompile your messages to JavaScript during your build phase. It works like this:
7
7
8
8
```js
9
-
>var mf =newMessageFormat('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.',
var efunc =newFunction('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."
30
32
```
31
33
32
34
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
36
38
37
39
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`.
0 commit comments