format
Replaces placeholders in the string with string-formatted values.
format(...args: any[]): LazyString
Summary
This method provides the core functionality for strf() and bound.strf(). It inserts the provided list of values (or properties of a single object) into the format string, formatted in a specific way, as determined by the placeholder.
Placeholders are compatible with C-style sprintf, e.g. %s, %+8i, %.5f, etc., as well as the following custom placeholders:
%[property]
placeholders take a property from the first argument which must be an object%[property:format]
placeholders take a property and apply the specified format, e.g.%[foo:s]
for string formatting,%[foo:.2f]
for a number formatted in the same way as%.2f
, etc.#{a/b}
,#{a/b/c}
for pluralization: select an option based on the quantity (a number) from the first value in the parameter list (e.g.strf("%i file#{/s}", n)
)#n${a/b}
,#n${a/b/c}
to select an option based on a quantity that’s passed as a different argument, at position n (1-based index, e.g.strf("User %s has %i message#2${/s}", userName, nMessages)
)%[property:plural|a|b]
for pluralization based on a property from the first argument%n
(non-standard) general-purpose number format, never results in exponential notation%_
to insert nothing at all (blank string)%{uc}
,%{lc}
to convert strings to uppercase or lowercase%{s|abc}
to insert a string, orabc
if the string is empty or not defined%{?|a}
to insert string a if the value== true
, otherwise a blank string%{?|a|b}
to insert one ofa
orb
: the first option if the value== true
, the second if not%{plural|...|...}
for pluralization, but with its own quantity value in the argument list%{local|...}
for i18n-formatted values; the type part(s) are variable, and will need to be implemented by the I18nProvider.format() method of the current i18n provider, e.g.strf("Today is %{local|date}", new Date())
.
Using arguments within placeholders — Asterisks (*
) anywhere in a placeholder are replaced by the next value in the parameter list (before the value being represented itself), e.g. in strf("%.*f", precision, number)
and strf("%{local|currency:*}", currency, number)
.
Decimal separator — Floating point numbers are formatted using the decimal separator specified by the decimalSeparator
attribute of the currently registered i18n interface, if any. Number grouping separators aren’t supported, and if necessary numbers will need to be formatted using %{local|…}.
Argument positions — Use position specifiers (i.e. n$
) to change the order of parameters used, e.g. strf("A is %i and B is %i, so %1$i + %2$i equals %i", i1, i2, i1 + i2)
. This is mostly helpful for translations where the position of words or numbers is different.
Return value
A new LazyString instance.
Related
- class LazyStringAn object that encapsulates a string, evaluated only when needed.