Namespace: formatters

MessageFormat.formatters

Default number formatting functions in the style of ICU's simpleArg syntax implemented using the Intl object defined by ECMA-402.

Note: Intl is not defined in default Node until 0.11.15 / 0.12.0, so earlier versions require a polyfill. Therefore MessageFormat.intlSupport needs to be true for these default functions to be available for inclusion in the output.

Source:
See:

Methods

(static) date(value, typeopt)

Represent a date as a short/default/long/full string

The input value needs to be in a form that the Date object can process using its single-argument form, new Date(value).

Parameters:
Name Type Attributes Default Description
value number | string

Either a Unix epoch time in milliseconds, or a string value representing a date

type string <optional>
'default'

One of 'short', 'default', 'long' , or full

Source:
Example
var mf = new MessageFormat(['en', 'fi']).setIntlSupport(true);

mf.compile('Today is {T, date}')({ T: Date.now() })
// 'Today is Feb 21, 2016'

mf.compile('Tänään on {T, date}', 'fi')({ T: Date.now() })
// 'Tänään on 21. helmikuuta 2016'

mf.compile('Unix time started on {T, date, full}')({ T: 0 })
// 'Unix time started on Thursday, January 1, 1970'

var cf = mf.compile('{sys} became operational on {d0, date, short}');
cf({ sys: 'HAL 9000', d0: '12 January 1999' })
// 'HAL 9000 became operational on 1/12/1999'

(static) number(value, type)

Represent a number as an integer, percent or currency value

Available in MessageFormat strings as {VAR, number, integer|percent|currency}. Internally, calls Intl.NumberFormat with appropriate parameters. currency will default to USD; to change, set MessageFormat#currency to the appropriate three-letter currency code.

Parameters:
Name Type Description
value number

The value to operate on

type string

One of 'integer', 'percent' , or currency

Source:
Example
var mf = new MessageFormat('en').setIntlSupport(true);
mf.currency = 'EUR';  // needs to be set before first compile() call

mf.compile('{N} is almost {N, number, integer}')({ N: 3.14 })
// '3.14 is almost 3'

mf.compile('{P, number, percent} complete')({ P: 0.99 })
// '99% complete'

mf.compile('The total is {V, number, currency}.')({ V: 5.5 })
// 'The total is €5.50.'

(static) time(value, typeopt)

Represent a time as a short/default/long string

The input value needs to be in a form that the Date object can process using its single-argument form, new Date(value).

Parameters:
Name Type Attributes Default Description
value number | string

Either a Unix epoch time in milliseconds, or a string value representing a date

type string <optional>
'default'

One of 'short', 'default', 'long' , or full

Source:
Example
var mf = new MessageFormat(['en', 'fi']).setIntlSupport(true);

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

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

var cf = mf.compile('The Eagle landed at {T, time, full} on {T, date, full}');
cf({ T: '1969-07-20 20:17:40 UTC' })
// 'The Eagle landed at 10:17:40 PM GMT+2 on Sunday, July 20, 1969'