Skip to content

Interface: ITranslator ​

The messages of one language, without giving the language at each call: I18n.translator gives one for a language, and the Translator injected in the objects of a request is the one of the request's language.

Properties ​

locale ​

readonly locale: string

The language tag of the translator (zh-CN).

Methods ​

date() ​

date(value, options?): string

Format a date (and its time when the options ask for it) in the translator's language, in the component's timeZone (the process's one without it).

Parameters ​

value ​

number | Date

The date, or a timestamp in milliseconds.

options? ​

DateTimeFormatOptions

The options of Intl.DateTimeFormat ({dateStyle: 'long'}); their timeZone replaces the component's one.

Returns ​

string

The formatted date.


format() ​

format(message, params?): string

Format an ICU MessageFormat message which is not in the catalogs, in the translator's language.

Parameters ​

message ​

string

The message (Hello, {name}).

params? ​

Record<string, unknown>

The arguments of the message, by their names.

Returns ​

string

The formatted message. With missing: 'key', {name} in place of a missing argument, a plain number for a currency argument without a currency (each with a warning, once).

Throws ​

When the message is not valid ICU MessageFormat.

Throws ​

When an argument is missing, or a currency argument has no currency, and the missing option is 'throw'.


has() ​

has(key): boolean

Tell whether a key has a message in the translator's language or in the languages it falls back to.

Parameters ​

key ​

string

The key of the message.

Returns ​

boolean

true when ITranslator.t finds a message for the key.


number() ​

number(value, options?): string

Format a number in the translator's language (1234.5 gives 1,234.5 in English, 1.234,5 in German).

Parameters ​

value ​

number | bigint

The number.

options? ​

NumberFormatOptions

The options of Intl.NumberFormat ({style: 'currency', currency: 'EUR'}); the component's currency option is not applied here.

Returns ​

string

The formatted number.


t() ​

t<K>(key, params?): string

Give the message of a key in the translator's language, formatted with its arguments (see I18n.t).

Type Parameters ​

K ​

K extends string

Parameters ​

key ​

K

The key of the message (order.count).

params? ​

MessageParams<K>

The arguments of the message, by their names.

Returns ​

string

The formatted message. With missing: 'key', the key itself when it is missing, {name} in place of a missing argument, a plain number for a currency argument without a currency (each with a warning, once).

Throws ​

When no message has the key and the missing option is 'throw'.

Throws ​

When an argument is missing, or a currency argument has no currency, and the missing option is 'throw'.

Throws ​

When the message is not valid ICU MessageFormat.

Released under the MIT License.