Skip to content

Commit 18b60b8

Browse files
authored
Merge pull request #1 from cletusw/master
Document Intl and custom formatters
2 parents cd2a665 + c2a3bad commit 18b60b8

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

guide.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ title: Format Guide
77
* [Variables](#variables)
88
* [SelectFormat](#selectFormat)
99
* [PluralFormat](#pluralFormat)
10+
* [Intl Formatters](#intl-formatters)
11+
* [Custom Formatters](#custom-formatters)
1012
* [Nesting](#nesting)
1113
* [Escaping](#escaping)
1214

@@ -123,6 +125,96 @@ offsetMessage({ NUM_ADDS: 3 });
123125
```
124126

125127

128+
## Intl Formatters
129+
130+
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 = new MessageFormat(['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 = new MessageFormat('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 })
166+
// '3.14 is almost 3'
167+
168+
mf.compile('{P, number, percent} complete')({ P: 0.99 })
169+
// '99% complete'
170+
171+
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 = new MessageFormat(['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 = new MessageFormat('en-GB');
201+
mf.addFormatters({
202+
upcase: function(v) { return v.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}.')({ _: '' })
211+
// 'The current locale is en-GB.'
212+
213+
mf.compile('Answer: {obj, prop, a}')({ obj: {q: 3, a: 42} })
214+
// 'Answer: 42'
215+
```
216+
217+
126218
## Nesting
127219

128220
All types of messageformat statements may be nested within each other, to unlimited depth:

0 commit comments

Comments
 (0)