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: guide.md
+44-45Lines changed: 44 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,10 +16,10 @@ title: Format Guide
16
16
The simplest case of MessageFormat involves no formatting, just a string passthrough. This may sound silly, but often it's nice to always use the same message formatting system when doing translations, and not everything requires variables.
17
17
18
18
```javascript
19
-
var mf =newMessageFormat('en');
20
-
var message =mf.compile('This is a message.');
19
+
constmf=newMessageFormat('en')
20
+
constmessage=mf.compile('This is a message.')
21
21
22
-
message();
22
+
message()
23
23
// "This is a message."
24
24
```
25
25
@@ -35,10 +35,10 @@ By default (like in the previous example), you are just writing a literal. Then
35
35
Simply putting a variable name in between `{` and `}` will place that variable there in the output.
36
36
37
37
```javascript
38
-
var mf =newMessageFormat('en');
39
-
var varMessage =mf.compile('His name is {NAME}.');
38
+
constmf=newMessageFormat('en')
39
+
constvarMessage=mf.compile('His name is {NAME}.')
40
40
41
-
varMessage({ NAME:"Jed" });
41
+
varMessage({ NAME:'Jed' })
42
42
// "His name is Jed."
43
43
```
44
44
@@ -50,18 +50,18 @@ varMessage({ NAME : "Jed" });
50
50
Note that comparison is made using the JavaScript `==` operator, so if a key is left out of the input data, the case `undefined{...}` would match that.
To generate sentences such as "You and 4 others added this to their profiles.", PluralFormat supports adding an `offset` to the variable value before determining its plural category. Literal/exact matches are tested before applying the offset.
101
101
102
102
```javascript
103
-
var mf =newMessageFormat('en');
104
-
105
-
var offsetMessage =mf.compile(
106
-
'You {NUM_ADDS, plural, offset:1'+
107
-
'=0{did not add this}'+
108
-
'=1{added this}'+
109
-
'one{and one other person added this}'+
110
-
'other{and # others added this}'+
111
-
'}.'
112
-
);
113
-
114
-
offsetMessage({ NUM_ADDS:0 });
103
+
constmf=newMessageFormat('en')
104
+
constoffsetMessage=mf.compile(
105
+
`You {NUM_ADDS, plural, offset:1
106
+
=0 {did not add this}
107
+
=1 {added this}
108
+
one {and one other person added this}
109
+
other {and # others added this}
110
+
}.`
111
+
)
112
+
113
+
offsetMessage({ NUM_ADDS:0 })
115
114
// "You did not add this."
116
115
117
-
offsetMessage({ NUM_ADDS:1 });
116
+
offsetMessage({ NUM_ADDS:1 })
118
117
// "You added this."
119
118
120
-
offsetMessage({ NUM_ADDS:2 });
119
+
offsetMessage({ NUM_ADDS:2 })
121
120
// "You and one other person added this."
122
121
123
-
offsetMessage({ NUM_ADDS:3 });
122
+
offsetMessage({ NUM_ADDS:3 })
124
123
// "You and 2 others added this."
125
124
```
126
125
@@ -138,7 +137,7 @@ Because native or polyfilled support is not guaranteed, you must call `setIntlSu
138
137
Supported parameters are 'short', 'default', 'long' , or 'full'.
139
138
140
139
```javascript
141
-
var mf =newMessageFormat(['en', 'fi']).setIntlSupport(true);
mf.compile('The time is now {T, time}')({ T:Date.now() })
183
182
// 'The time is now 11:26:35 PM'
184
183
185
184
mf.compile('Kello on nyt {T, time}', 'fi')({ T:Date.now() })
186
185
// 'Kello on nyt 23.26.35'
187
186
188
-
var cf =mf.compile('The Eagle landed at {T, time, full} on {T, date, full}');
187
+
constcf=mf.compile('The Eagle landed at {T, time, full} on {T, date, full}')
189
188
cf({ T:'1969-07-20 20:17:40 UTC' })
190
189
// 'The Eagle landed at 10:17:40 PM GMT+2 on Sunday, July 20, 1969'
191
190
```
@@ -197,12 +196,12 @@ MessageFormat also supports custom formatters. Call `addFormatters()` on your Me
197
196
198
197
199
198
```javascript
200
-
var mf =newMessageFormat('en-GB');
199
+
constmf=newMessageFormat('en-GB')
201
200
mf.addFormatters({
202
201
upcase:function(v) { returnv.toUpperCase(); },
203
202
locale:function(v, lc) { return lc; },
204
203
prop:function(v, lc, p) { return v[p] }
205
-
});
204
+
})
206
205
207
206
mf.compile('This is {VAR, upcase}.')({ VAR:'big' })
208
207
// 'This is BIG.'
@@ -240,9 +239,9 @@ All types of messageformat statements may be nested within each other, to unlimi
240
239
The characters `{` and `}` must be escaped with a `\` to be included in the output as literal characters. Within plural statements, `#` must also be similarly escaped. Keep in mind that you'll need to double-escape with `\\` within e.g. JavaScript and JSON strings.
241
240
242
241
```javascript
243
-
var mf =newMessageFormat('en');
244
-
var escMessage =mf.compile('\\{ {S, plural, other{# is a \\#}} \\}');
242
+
constmf=newMessageFormat('en')
243
+
constescMessage=mf.compile('\\{ {S, plural, other{# is a \\#}} \\}')
Copy file name to clipboardExpand all lines: index.md
+23-21Lines changed: 23 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,16 @@ layout: default
3
3
title: Home
4
4
---
5
5
6
-
The experience and subtlety of your program's text can be important. MessageFormat is a mechanism for handling both **pluralization** and **gender** in your applications. It can also lead to much better translations, as it's designed to support [all the languages](http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html) included in the [Unicode CLDR](http://cldr.unicode.org/).
6
+
The experience and subtlety of your program's text can be important. Messageformat is a mechanism for handling both **pluralization** and **gender** in your applications. It can also lead to much better translations, as it's designed to support [all the languages](http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html) included in the [Unicode CLDR](http://cldr.unicode.org/).
7
7
8
-
The ICU has an [official guide](http://userguide.icu-project.org/formatparse/messages) for the format. Messageformat.js supports and extends all parts of the [standard](http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html), with the exception of the deprecated ChoiceFormat.
8
+
The ICU has an [official guide](http://userguide.icu-project.org/formatparse/messages) for the format. Messageformat supports and extends all parts of the [standard](http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html), with the exception of the deprecated ChoiceFormat.
9
9
10
10
There is a good slide-deck on [Plural and Gender in Translated Messages](https://docs.google.com/presentation/d/1ZyN8-0VXmod5hbHveq-M1AeQ61Ga3BmVuahZjbmbBxo/pub?start=false&loop=false&delayms=3000#slide=id.g1bc43a82_2_14) by Markus Scherer and Mark Davis. But, again, remember that many of these problems apply even if you're only outputting english.
11
11
12
12
13
13
## What problems does it solve?
14
14
15
-
Using messageformat.js, you can separate your code from your text formatting, while enabling much more humane expressions. In other words, you won't need to see this anymore in your output:
15
+
Using messageformat, you can separate your code from your text formatting, while enabling much more humane expressions. In other words, you won't need to see this anymore in your output:
16
16
17
17
> There are 1 results.
18
18
> There are 1 result(s).
@@ -24,31 +24,33 @@ Using messageformat.js, you can separate your code from your text formatting, wh
0 commit comments