Docs (4.0.0)
Text formatting

strf

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

strf(format: StringConvertible | StringConvertible[], ...values: any[]): LazyString

Summary

This function creates a new instance of LazyString with the specified format string. When converted to a string, the text is translated, formatted (if arguments are provided), and cached using the following methods:

  • translate()Translates the string using the current I18n provider.
  • format(…args)Replaces placeholders in the string with string-formatted values.
  • cache()Caches the result after evaluating it once.

For format placeholder options and features such as pluralization, refer to the format() method.

Parameters

  • format — The format string, which may include placeholders for dynamically formatted values; or an array passed by a template literal source
  • values — Any number of values to include in the result; if no values are provided, the result is a LazyString instance that can be formatted later

Return value

An instance of LazyString.

Examples

// Format a string including a single number:
let balance = 20.5;
let s = strf("Your balance: $%.2f", balance);
String(s) // => "Your balance: $20.50"
// Format a string including property values:
let s = strf("Hello, %[user]!", { user: "human" });
String(s) // => "Hello, human!"
// Use template literal notation (useful only with i18n):
let a = "world";
let s = strf`Hello, ${a}!`;
String(s) // => "Hello, world!"