Documentation

API Referencefunction

function fmt()

Returns a (lazily) formatted string incorporating the provided values.

function fmt(
format: StringConvertible | StringConvertible[],
...values: any[]
): DeferredString;

Parameters

Return value

An instance of DeferredString.

Description

This function creates a new instance of DeferredString with the specified format string. When converted to a string, the text is translated, formatted, and cached (until the cache is invalidated, e.g. when the application I18n provider is changed).

The syntax of placeholders is as follows:

The following set of format specifiers can be used to format each value:

Within all format specifiers, additional placeholders such as {0} or {p} can be used to insert the value of another value from the argument list, or a property of the first argument as an object — e.g. for number of decimal places, or replacements passed to the ? placeholder. Such nested placeholders cannot contain format specifiers themselves.

Examples

// Format a string including a single number:
let s = fmt("Package weight: {} kg", 75.5);
String(s) // => "Package weight: 75.5 kg"

// Format a string including a single number:
let s = fmt("Package weight: {:.2f} kg", weight);
String(s) // => "Package weight: 75.50 kg"

// Format a string with multiple values:
let s = fmt("Customer ID {}: {}", 123, "John Doe");
String(s) // => "Customer ID 123: John Doe"

// or more explicitly:
let s = fmt("Customer ID {0}: {1}", 123, "John Doe");
String(s) // => "Customer ID 123: John Doe"

// ...or using an object:
let s = fmt("Customer ID {id}: {name}", { id: 123, name: "John Doe" });
String(s) // => "Customer ID 123: John Doe"

// Format a string with boolean check and pluralization:
let s = fmt("You have {:?/{0}/no} {0:+/message/messages}", 0);
String(s) // => "You have no messages"