MessageFormatter
Self-contained, extensible message formatting class. Handles parsing and caching of messages. Convenience wrapper around the message matcher, message parser, and evaluation engine.
new
Constructs a new message formatter.
Syntax
new MessageFormatter(options: MessageFormatterOptions)
Parameters
options: MessageFormatterOptions
- Options, including custom formatter functions, cache size, etc.
format
Formats a message with the given arguments. Internall the message is parsed and cached.
Syntax
format(message: string, positional: MessageArg[], named: MessageNamedArgs): string
Parameters
message: string
- Message to format
positional: MessageArg[]
- Arguments keyed by their position in the array
named: MessageNamedArgs
- Arguments keyed by name
Example
import { MessageArg, MessageFormatter } from '@phensley/messageformat';
const formatters = {
foo: (args: MessageArg[], options: string[]): string =>
options[0] === 'upper' ? args[0].toUpperCase() : args[0].toLowerCase()
};
const formatter = new MessageFormatter({ language: 'en', formatters });
log(formatter.format('{0}', ['Hello'], {}));
log(formatter.format('{0 foo upper}', ['Hello'], {}));
log(formatter.format('{0 foo lower}', ['Hello'], {}));
Hello HELLO hello