Be sure to read the overview documentation before you dive into the API details.
Messages are defined as simple JavaScript objects where the property key is the message key and the value is the
message. Messages should either be simple string
s or functions returning a string
.
Some functions allow you to either directly specify a Messages object or a LocalizedMessages function.
Merges the specified formats into the global formats.
Format messages using ICU message syntax.
Instead of using an object as parameter like formatObject this function accepts up to five parameters,
which are then available by the names 1
to 5
inside the message string.
Example:
format<number>(languageTag('en'), 'Current message count is {1, number}')(3);
the language to be used for message formatting (e.g. for numbers and plurals)
the message using ICU message syntax.
custom format options used by the message. Defaults to the globally registered formats.
a function accepting the specified number of parameters.
Format message using ICU message syntax.
Example:
const msg = formatObject<{msgCount: number}>(languageTag('en'),
'Current message count is {msgCount, number}');
msg({msgCount: 3}); // 'Current message count is 3'
the locale to be used for message formatting (e.g. for numbers and plurals)
the message using ICU message syntax.
custom format options used by the message. Defaults to the globally registered formats.
a function accepting the parameter object.
Global FormatOptions used by the format and formatObject function if nothing else is specified there. You can use setFormats and addFormats to register custom formats.
By default the number formats #
, #.#
, #.##
to #.#####
are defined.
You can use them in a message string like eg. {1, number, #.##}
to format a number with the specified number of
fraction digits.
Provides a LanguageTag for the specified string representation of a language tag.
It is guaranteed, that this function returns the same LanguageTag instance for the same string
(ignoring case). Thus it is save to use strict equality (===
) when comparing LanguageTags.
a language tag string as defined in BCP-47
the resulting language tag
Picks the best supported language based on the user's preferred languages.
the translations provided by this application
the user's preferred languages ordered by preference (most preferred first)
the best matching language from usersPreferredLanguages
or the first if no one matches.
Creates a pluralized message which will return the adequate message based on the provided number and language.
Example:
const msg = plural(languageTag('en'), {
zero: 'You have no new messages',
one: 'You have one new message',
other: n => format<number>(languageTag('en'), 'You have {1, number} new messages')(n)
});
msg(5); // 'You have 5 new messages'
the language to be used for decision which of the plural forms to choose.
the messages for the different cases.
a function accepting a numeral argument used to pick the right message.
The preferred language to be used when calling MessageProvider.messages.
By default this is set to navigator.language
.
Adjust it using setPreferredLanguage or selectPreferredLanguage.
Selects a message from SelectOptions based on the provided selection parameter.
the possible cases
a function expecting a selection string to pick the correct message from the provided options.
Selects and formats a message from SelectOptions based on the provided parameter object.
Example:
const msg = selectObject<Person>(languageTag('en'), p => p.gender, {
female: 'Dear Mrs. {name}',
male: 'Dear Mr. {name}',
other: 'Dear {name}'
});
msg({gender: 'female', name: 'Granger'}); // 'Dear Mrs. Granger'
a function that extracts the selection key from the provided parameter object.
the possible cases where each message will be formatted using
formatObject based on the parameters
object.
a function expecting a single parameter object containing the selector and the values referenced by the messages.
Sets the preferredLanguage based on the translations supported by this application and the user's language preferences.
the languages this app provides translations for
the user's preferred languages ordered by preference (most preferred first).
Replaces the current global formats.
Sets the preferred language to be used when calling MessageProvider.messages.
language to be used for retrieving messages with MessageProvider.messages.
Create a Translator with the specified default messages.
messages to be used if no messages for a selected translation are available.
translator with the specified default messages.
Generated using TypeDoc
Either a LanguageTag or a
string
which can be provided to languageTag().