Skip to content

Commit 6d0dbda

Browse files
committed
Style fixes
1 parent b88c1e8 commit 6d0dbda

2 files changed

Lines changed: 67 additions & 66 deletions

File tree

guide.md

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ title: Format Guide
1616
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.
1717

1818
```javascript
19-
var mf = new MessageFormat('en');
20-
var message = mf.compile('This is a message.');
19+
const mf = new MessageFormat('en')
20+
const message = mf.compile('This is a message.')
2121

22-
message();
22+
message()
2323
// "This is a message."
2424
```
2525

@@ -35,10 +35,10 @@ By default (like in the previous example), you are just writing a literal. Then
3535
Simply putting a variable name in between `{` and `}` will place that variable there in the output.
3636

3737
```javascript
38-
var mf = new MessageFormat('en');
39-
var varMessage = mf.compile('His name is {NAME}.');
38+
const mf = new MessageFormat('en')
39+
const varMessage = mf.compile('His name is {NAME}.')
4040

41-
varMessage({ NAME : "Jed" });
41+
varMessage({ NAME : 'Jed' })
4242
// "His name is Jed."
4343
```
4444

@@ -50,18 +50,18 @@ varMessage({ NAME : "Jed" });
5050
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.
5151

5252
```javascript
53-
var mf = new MesssageFormat('en');
54-
var selectMessage = mf.compile(
53+
const mf = new MesssageFormat('en')
54+
const selectMessage = mf.compile(
5555
'{GENDER, select, male{He} female{She} other{They}} liked this.'
56-
);
56+
)
5757

58-
selectMessage({ GENDER: 'male' });
58+
selectMessage({ GENDER: 'male' })
5959
// "He liked this."
6060

61-
selectMessage({ GENDER: 'female' });
61+
selectMessage({ GENDER: 'female' })
6262
// "She liked this."
6363

64-
selectMessage({});
64+
selectMessage({})
6565
// "They liked this."
6666
```
6767

@@ -79,18 +79,18 @@ The keyword for cardinal plurals is `plural`, and for ordinal plurals is `select
7979
Within a plural statement, `#` will be replaced by the variable value.
8080

8181
```javascript
82-
var mf = new MessageFormat('en');
83-
var pluralMessage = mf.compile(
84-
'There {NUM_RESULTS, plural, =0{are no results} one{is one result} other{are # results}}.'
85-
);
82+
const mf = new MessageFormat('en')
83+
const pluralMessage = mf.compile(
84+
'There {COUNT, plural, =0{are no results} one{is one result} other{are # results}}.'
85+
)
8686

87-
pluralMessage({ NUM_RESULTS: 0 });
87+
pluralMessage({ COUNT: 0 })
8888
// "There are no results."
8989

90-
pluralMessage({ NUM_RESULTS: 1 });
90+
pluralMessage({ COUNT: 1 })
9191
// "There is one result."
9292

93-
pluralMessage({ NUM_RESULTS: 100 });
93+
pluralMessage({ COUNT: 100 })
9494
// "There are 100 results."
9595
```
9696

@@ -100,27 +100,26 @@ pluralMessage({ NUM_RESULTS: 100 });
100100
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.
101101

102102
```javascript
103-
var mf = new MessageFormat('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+
const mf = new MessageFormat('en')
104+
const offsetMessage = 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 })
115114
// "You did not add this."
116115

117-
offsetMessage({ NUM_ADDS: 1 });
116+
offsetMessage({ NUM_ADDS: 1 })
118117
// "You added this."
119118

120-
offsetMessage({ NUM_ADDS: 2 });
119+
offsetMessage({ NUM_ADDS: 2 })
121120
// "You and one other person added this."
122121

123-
offsetMessage({ NUM_ADDS: 3 });
122+
offsetMessage({ NUM_ADDS: 3 })
124123
// "You and 2 others added this."
125124
```
126125

@@ -138,7 +137,7 @@ Because native or polyfilled support is not guaranteed, you must call `setIntlSu
138137
Supported parameters are 'short', 'default', 'long' , or 'full'.
139138

140139
```javascript
141-
var mf = new MessageFormat(['en', 'fi']).setIntlSupport(true);
140+
const mf = new MessageFormat(['en', 'fi']).setIntlSupport(true)
142141

143142
mf.compile('Today is {T, date}')({ T: Date.now() })
144143
// 'Today is Feb 21, 2016'
@@ -149,7 +148,7 @@ mf.compile('Tänään on {T, date}', 'fi')({ T: Date.now() })
149148
mf.compile('Unix time started on {T, date, full}')({ T: 0 })
150149
// 'Unix time started on Thursday, January 1, 1970'
151150

152-
var cf = mf.compile('{sys} became operational on {d0, date, short}');
151+
const cf = mf.compile('{sys} became operational on {d0, date, short}')
153152
cf({ sys: 'HAL 9000', d0: '12 January 1999' })
154153
// 'HAL 9000 became operational on 1/12/1999'
155154
```
@@ -159,8 +158,8 @@ cf({ sys: 'HAL 9000', d0: '12 January 1999' })
159158
Supported parameters are 'integer', 'percent' , or 'currency'.
160159

161160
```javascript
162-
var mf = new MessageFormat('en').setIntlSupport(true);
163-
mf.currency = 'EUR'; // needs to be set before first compile() call
161+
const mf = new MessageFormat('en').setIntlSupport(true)
162+
mf.currency = 'EUR' // needs to be set before first compile() call
164163

165164
mf.compile('{N} is almost {N, number, integer}')({ N: 3.14 })
166165
// '3.14 is almost 3'
@@ -177,15 +176,15 @@ mf.compile('The total is {V, number, currency}.')({ V: 5.5 })
177176
Supported parameters are 'short', 'default', 'long' , or 'full'.
178177

179178
```javascript
180-
var mf = new MessageFormat(['en', 'fi']).setIntlSupport(true);
179+
const mf = new MessageFormat(['en', 'fi']).setIntlSupport(true)
181180

182181
mf.compile('The time is now {T, time}')({ T: Date.now() })
183182
// 'The time is now 11:26:35 PM'
184183

185184
mf.compile('Kello on nyt {T, time}', 'fi')({ T: Date.now() })
186185
// 'Kello on nyt 23.26.35'
187186

188-
var cf = mf.compile('The Eagle landed at {T, time, full} on {T, date, full}');
187+
const cf = mf.compile('The Eagle landed at {T, time, full} on {T, date, full}')
189188
cf({ T: '1969-07-20 20:17:40 UTC' })
190189
// 'The Eagle landed at 10:17:40 PM GMT+2 on Sunday, July 20, 1969'
191190
```
@@ -197,12 +196,12 @@ MessageFormat also supports custom formatters. Call `addFormatters()` on your Me
197196

198197

199198
```javascript
200-
var mf = new MessageFormat('en-GB');
199+
const mf = new MessageFormat('en-GB')
201200
mf.addFormatters({
202201
upcase: function(v) { return v.toUpperCase(); },
203202
locale: function(v, lc) { return lc; },
204203
prop: function(v, lc, p) { return v[p] }
205-
});
204+
})
206205

207206
mf.compile('This is {VAR, upcase}.')({ VAR: 'big' })
208207
// 'This is BIG.'
@@ -240,9 +239,9 @@ All types of messageformat statements may be nested within each other, to unlimi
240239
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.
241240

242241
```javascript
243-
var mf = new MessageFormat('en');
244-
var escMessage = mf.compile('\\{ {S, plural, other{# is a \\#}} \\}');
242+
const mf = new MessageFormat('en')
243+
const escMessage = mf.compile('\\{ {S, plural, other{# is a \\#}} \\}')
245244

246-
escMessage({ S: 5 });
245+
escMessage({ S: 5 })
247246
// "{ 5 is a # }"
248247
```

index.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ layout: default
33
title: Home
44
---
55

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/).
77

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.
99

1010
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.
1111

1212

1313
## What problems does it solve?
1414

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:
1616

1717
> There are 1 results.
1818
> There are 1 result(s).
@@ -24,31 +24,33 @@ Using messageformat.js, you can separate your code from your text formatting, wh
2424
With this message:
2525

2626
```js
27-
var msg =
28-
'{GENDER, select, male{He} female{She} other{They} }' +
29-
' found ' +
30-
'{RES, plural, =0{no results} one{1 result} other{# results} }' +
31-
' in the ' +
32-
'{CAT, selectordinal, one{#st} two{#nd} few{#rd} other{#th} }' +
33-
' category.';
27+
const msgSrc = `{GENDER, select,
28+
male {He}
29+
female {She}
30+
other {They}
31+
} found {RES, plural,
32+
=0 {no results}
33+
one {1 result}
34+
other {# results}
35+
}.`
3436
```
3537

3638
You'll get these results:
3739

3840
```js
39-
var mfunc = new MessageFormat('en').compile(msg);
41+
const msg = new MessageFormat('en').compile(msgSrc)
4042

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

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

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

50-
mfunc({ RES: 2, CAT: 2 })
51-
// 'They found 2 results in the 2nd category.'
52+
msg({ RES: 2 })
53+
// 'They found 2 results.'
5254
```
5355

5456

@@ -74,8 +76,8 @@ npm install messageformat
7476
```
7577

7678
```js
77-
var MessageFormat = require('messageformat');
78-
var mf = new MessageFormat('en');
79+
import MessageFormat from 'messageformat'
80+
const mf = new MessageFormat('en')
7981
```
8082

8183
### Bower

0 commit comments

Comments
 (0)