Link Search Menu Expand Document

MessageFormat class

The core MessageFormat-to-JavaScript compiler

Signature:

export default class MessageFormat 

Example

import MessageFormat from '@messageformat/core'
const mf = new MessageFormat('en')

const msgSrc = `{GENDER, select,
  male {He} female {She} other {They}
} found {RES, plural,
  =0 {no results} one {1 result} other {# results}
}.`;
const msg = mf.compile(msgSrc)

msg({ GENDER: 'male', RES: 1 })    // 'He found 1 result.'
msg({ GENDER: 'female', RES: 1 })  // 'She found 1 result.'
msg({ GENDER: 'male', RES: 0 })    // 'He found no results.'
msg({ RES: 2 })                    // 'They found 2 results.'

Constructors

Constructor Modifiers Description
(constructor)(locale, options)   Create a new MessageFormat compiler

Properties

Property Modifiers Type Description
defaultLocale static string Used by the constructor when no locale argument is given. Default: 'en'

Methods

Method Modifiers Description
compile(message)   Compile a message into a function
escape(str, octothorpe) static Escape characaters that may be considered as MessageFormat markup
resolvedOptions()   Returns a new object with properties reflecting the default locale, plurals, and other options computed during initialization.
supportedLocalesOf(locales) static Returns a subset of locales consisting of those for which MessageFormat has built-in plural category support.