compileModule(messageformat, messages)

Compile a collection of messages into an ES module.

import compileModule from 'messageformat/compile-module'

With messages as a hierarchical structure of ICU MessageFormat strings, the output of compileModule() will be the source code of an ES module with a default export matching the input structure, with each string replaced by its corresponding JS function.

If this MessageFormat instance has been initialized with support for more than one locale, using a key that matches the locale's identifier at any depth of a messages object will set its child elements to use that locale.

Name Type Description
messageformat MessageFormat

A MessageFormat instance

messages object

The input messages to be compiled

  • String representation of the compiled module
import fs from 'fs'
import MessageFormat from 'messageformat'
import compileModule from 'messageformat/compile-module'

const mf = new MessageFormat('en')
const msgSet = {
  a: 'A {TYPE} example.',
  b: 'This has {COUNT, plural, one{one member} other{# members}}.',
  c: 'We have {P, number, percent} code coverage.'
const msgModule = compileModule(mf, msgSet)
fs.writeFileSync('messages.js', msgModule)


import messages from './messages'

messages.a({ TYPE: 'more complex' })  // 'A more complex example.'
messages.b({ COUNT: 3 })              // 'This has 3 members.'