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
MessageFormat also includes date, number, and time formatting functions in the style of ICU's [simpleArg syntax](http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html). They are implemented using the [Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) object defined by ECMA-402.
131
+
132
+
**Note**: Intl is not defined in Node by default until 0.11.15 / 0.12.0 and is not available in all browsers, so you may need to use a [polyfill](https://www.npmjs.com/package/intl).
133
+
134
+
Because native or polyfilled support is not guaranteed, you must call `setIntlSupport()` on your MessageFormat object before these will be available.
135
+
136
+
### date
137
+
138
+
Supported parameters are 'short', 'default', 'long' , or 'full'.
139
+
140
+
```javascript
141
+
var mf =newMessageFormat(['en', 'fi']).setIntlSupport(true);
142
+
143
+
mf.compile('Today is {T, date}')({ T:Date.now() })
144
+
// 'Today is Feb 21, 2016'
145
+
146
+
mf.compile('Tänään on {T, date}', 'fi')({ T:Date.now() })
147
+
// 'Tänään on 21. helmikuuta 2016'
148
+
149
+
mf.compile('Unix time started on {T, date, full}')({ T:0 })
150
+
// 'Unix time started on Thursday, January 1, 1970'
151
+
152
+
var cf =mf.compile('{sys} became operational on {d0, date, short}');
153
+
cf({ sys:'HAL 9000', d0:'12 January 1999' })
154
+
// 'HAL 9000 became operational on 1/12/1999'
155
+
```
156
+
157
+
### number
158
+
159
+
Supported parameters are 'integer', 'percent' , or 'currency'.
160
+
161
+
```javascript
162
+
var mf =newMessageFormat('en').setIntlSupport(true);
163
+
mf.currency='EUR'; // needs to be set before first compile() call
164
+
165
+
mf.compile('{N} is almost {N, number, integer}')({ N:3.14 })
mf.compile('The total is {V, number, currency}.')({ V:5.5 })
172
+
// 'The total is €5.50.'
173
+
```
174
+
175
+
### time
176
+
177
+
Supported parameters are 'short', 'default', 'long' , or 'full'.
178
+
179
+
```javascript
180
+
var mf =newMessageFormat(['en', 'fi']).setIntlSupport(true);
181
+
182
+
mf.compile('The time is now {T, time}')({ T:Date.now() })
183
+
// 'The time is now 11:26:35 PM'
184
+
185
+
mf.compile('Kello on nyt {T, time}', 'fi')({ T:Date.now() })
186
+
// 'Kello on nyt 23.26.35'
187
+
188
+
var cf =mf.compile('The Eagle landed at {T, time, full} on {T, date, full}');
189
+
cf({ T:'1969-07-20 20:17:40 UTC' })
190
+
// 'The Eagle landed at 10:17:40 PM GMT+2 on Sunday, July 20, 1969'
191
+
```
192
+
193
+
194
+
## Custom Formatters
195
+
196
+
MessageFormat also supports custom formatters. Call `addFormatters()` on your MessageFormat object and provide it with a map of formatter names to formatter functions. Given a string containing `{var, yourFormatterName, arg1, arg2}`, your callback will be called with three parameters: the value of the variable, the current locale, and an array of [arg1, arg2].
197
+
198
+
199
+
```javascript
200
+
var mf =newMessageFormat('en-GB');
201
+
mf.addFormatters({
202
+
upcase:function(v) { returnv.toUpperCase(); },
203
+
locale:function(v, lc) { return lc; },
204
+
prop:function(v, lc, p) { return v[p] }
205
+
});
206
+
207
+
mf.compile('This is {VAR, upcase}.')({ VAR:'big' })
208
+
// 'This is BIG.'
209
+
210
+
mf.compile('The current locale is {_, locale}.')({ _:'' })
0 commit comments