Class: MessageFormat


MessageFormat-to-JavaScript compiler


new MessageFormat( [locale] [, options])

Create a new MessageFormat compiler

import MessageFormat from 'messageformat'
Parameters:
Name Type Argument Description
locale string | Array <optional>

Define the locale or locales supported by this MessageFormat instance. If given multiple valid locales, the first will be the default. If locale is empty, it will fall back to MessageFormat.defaultLocale.

String locale values will be matched to plural categorisation functions provided by the Unicode CLDR. If defining your own instead, use named functions instead, optionally providing them with the properties: cardinals: string[], ordinals: string[], module: string (to import the formatter as a runtime dependency, rather than inlining its source).

If locale has the special value '*', it will match all available locales. This may be useful if you want your messages to be completely determined by your data, but may provide surprising results if your input message object includes any 2-3 character keys that are not locale identifiers.

options MessageFormat~Options <optional>

Compiler options

Source:

Members


<static> defaultLocale

Used by the constructor when no locale argument is given.

Default Value:
  • 'en'
Source:

Methods


<static> escape(str [, octothorpe])

Escape special characaters by surrounding the characters { and } in the input string with 'quotes'. This will allow those characters to not be considered as MessageFormat control characters.

Parameters:
Name Type Argument Default Description
str string

The input string

octothorpe boolean <optional>
false

Also escape #

Source:
Returns:

The escaped string

Type
string

<static> supportedLocalesOf(locales)

Returns a subset of locales consisting of those for which MessageFormat has built-in plural category support.

Parameters:
Name Type Description
locales string | Array.<string>
Source:
Returns:
Type
Array.<string>

compile(message)

Compile a message into a function

Given a string message with ICU MessageFormat declarations, the result is a function taking a single Object parameter representing each of the input's defined variables, using the first valid locale.

Parameters:
Name Type Description
message string

The input message to be compiled, in ICU MessageFormat

Source:
Returns:
  • The compiled function
Type
function
Example
const mf = new MessageFormat('en')
const msg = mf.compile('A {TYPE} example.')

msg({ TYPE: 'simple' })  // 'A simple example.'

resolvedOptions()

Returns a new object with properties reflecting the default locale, plurals, and other options computed during initialization.

Source:
Returns:
Type
MessageFormat~ResolvedOptions

Type Definitions


Options

The shape of the options object that may be used as the second argument of the constructor.

Type:
  • Object
Properties:
Name Type Argument Default Description
biDiSupport boolean <optional>
false

Add Unicode control characters to all input parts to preserve the integrity of the output when mixing LTR and RTL text

currency string <optional>
'USD'

The currency to use when formatting {V, number, currency}

customFormatters Object <optional>

Map of custom formatting functions to include. See the Format Guide for more details.

requireAllArguments boolean <optional>
false

Require all message arguments to be set with a defined value

returnType 'string' | 'values' <optional>
'string'

Return type of compiled functions; either a concatenated string or an array (possibly hierarchical) of values

strictNumberSign boolean <optional>
false

Allow # only directly within a plural or selectordinal case, rather than in any inner select case as well.

Source:

ResolvedOptions

Type:
  • Object
Properties:
Name Type Description
biDiSupport boolean

Whether Unicode control characters be added to all input parts to preserve the integrity of the output when mixing LTR and RTL text

customFormatters object

Map of custom formatting functions

locale string

The default locale

plurals Array.<object>

All of the supported plurals

strictNumberSign boolean

Is # only allowed directly within a plural or selectordinal case

Source: