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
This is the source for [messageformat.github.io](https://messageformat.github.io/), the site for [messageformat.js](https://github.com/messageformat/messageformat.js).
1
+
This is the source for [messageformat.github.io](https://messageformat.github.io/), the site for [messageformat](https://github.com/messageformat/messageformat.js).
Copy file name to clipboardExpand all lines: build.md
+66-27Lines changed: 66 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,36 +3,32 @@ layout: page
3
3
title: Build-time Compilation
4
4
---
5
5
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:
6
+
For a significant decrease in filesize and execution time, you should precompile your messages to JavaScript during your build phase. If you're using [Webpack], we also provide **[messageformat-loader]** to help with that.
7
7
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() +')()');
13
+
With the [loader](https://github.com/messageformat/loader) (see its README for configuration instructions), this will "just work" in your code:
23
14
24
-
efunc();
25
-
// { simple: [Function],
26
-
// var: [Function],
27
-
// plural: [Function],
28
-
// select: [Function],
29
-
// ordinal: [Function] }
15
+
### messages.json
30
16
31
-
efunc().ordinal({ N:2 });
32
-
// "The 2nd message."
17
+
```json
18
+
{
19
+
"ordinal": "The {N, selectordinal, one{1st} two{2nd} few{3rd} other{#th}} message."
20
+
}
21
+
```
22
+
23
+
### example.js
24
+
25
+
```js
26
+
importmessagesfrom'./messages.json'
27
+
messages.ordinal({ N:1 })
28
+
// => 'The 1st message.'
33
29
```
34
30
35
-
Note that as `efunc` is defined as a `new Function()`, it has no access to the surrounding scope. This means that 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 completely independently of messageformat.js, or any other dependencies.
31
+
During the build, the loader will compile your messages into their respective functions, and package only those into the webpack output.
36
32
37
33
38
34
## CLI Compiler
@@ -41,12 +37,18 @@ A [CLI compiler](https://github.com/messageformat/messageformat.js/tree/master/b
Parses the input JSON file(s) of MessageFormat strings into a JS module of
47
43
corresponding hierarchical functions, written to stdout. Directories are
48
44
recursively scanned for all .json files.
49
45
46
+
-i, --enable-intl-support
47
+
Because native or polyfilled support for global Intl object is not
48
+
guaranteed, messageformat will disable Intl formatters by default.
49
+
If you require Intl support, you can use this argument to enable
50
+
Intl formatters for your messages. [default: false]
51
+
50
52
-l lc, --locale=lc
51
53
The locale(s) lc to include; if multiple, selected by matching
52
54
message key. [default: en]
@@ -58,23 +60,60 @@ recursively scanned for all .json files.
58
60
'module.exports' (node.js) are special. [default: module.exports]
59
61
60
62
-p, --disable-plural-key-checks
61
-
By default, messageformat.js throws an error when a statement uses a
63
+
By default, messageformat throws an error when a statement uses a
62
64
non-numerical key that will never be matched as a pluralization
63
65
category for the current locale. Use this argument to disable the
64
66
validation and allow unused plural keys. [default: false]
65
67
```
66
68
67
69
68
-
## Using compiled messageformat.js output
70
+
## Using compiled messageformat output
69
71
70
72
With default options, compiled messageformat functions are available through `module.exports`. However, using e.g. `-n i18n`, an UMD pattern is used, falling back to a global `i18n` object, with each source json having a corresponding subobject. Hence the compiled function corresponding to the `test` message defined in [`example/en/sub/folder/plural.json`](https://github.com/messageformat/messageformat.js/tree/master/example/en/sub/folder/plural.json) is available as [`i18n.sub.folder.plural.test`](https://github.com/messageformat/messageformat.js/tree/master/example/en/i18n.js):
A working example is available [here](/messageformat.js/example/index.html).
83
+
84
+
85
+
## Other JavaSCript Build Environments
86
+
87
+
To precompile messages in other JavaScript environments, you should make use of the object input format of [`MessageFormat#compile()`](http://messageformat.github.io/messageformat.js/doc/MessageFormat.html#compile), the output of which is stringifiable for later execution in other environments.
88
+
89
+
It works like this:
90
+
91
+
```js
92
+
constmf=newMessageFormat('en')
93
+
constmessages= {
94
+
simple:'A simple message.',
95
+
var:'Message with {X}.',
96
+
plural:'You have {N, plural, =0{no messages} one{1 message} other{# messages}}.',
97
+
select:'{GENDER, select, male{He has} female{She has} other{They have}} sent you a message.',
Note that as `efunc` is defined as a `new Function()`, it has no access to the surrounding scope. This means that 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 completely independently of messageformat, or any other dependencies.
0 commit comments