Skip to content

Class: LocaleTranslator ​

The messages of one language, without giving the language at each call: I18n.translator creates one, to use out of a request (a schedule, a singleton component, a background job). Its methods call the component's methods with its language, so the messages added later with I18n.addMessages are seen too. In a request, inject the Translator instead.

Example ​

typescript
import {Application} from 'lakutata'
import {buildI18nOptions, type I18n, type ITranslator} from 'lakutata/com/i18n'

Application
    .run(() => ({
        id: 'i18n.app',
        name: 'I18n',
        components: {
            i18n: buildI18nOptions({
                messages: {
                    'en': {order: {count: '{count, plural, =0 {No orders} one {# order} other {# orders}}'}},
                    'de': {order: {count: '{count, plural, =0 {Keine Bestellungen} one {# Bestellung} other {# Bestellungen}}'}}
                }
            })
        }
    }))
    .onLaunched(async (app: Application): Promise<void> => {
        const i18n: I18n = await app.getObject('i18n')
        const german: ITranslator = i18n.translator('de-DE')
        console.log(german.locale) //de-DE, its messages looked up in de-DE, then de, then en
        console.log(german.t('order.count', {count: 1000})) //1.000 Bestellungen
        console.log(german.number(1234.5)) //1.234,5
    })

Implements ​

Constructors ​

Constructor ​

new LocaleTranslator(i18n, locale): LocaleTranslator

Create the translator of a language; I18n.translator does it, normalizing the language first.

Parameters ​

i18n ​

I18n

The component holding the messages.

locale ​

string

The language tag, canonical.

Returns ​

LocaleTranslator

Properties ​

locale ​

readonly locale: string

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

Implementation of ​

ITranslator.locale

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.

Throws ​

When the date is invalid or the options are.

Implementation of ​

ITranslator.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'.

Implementation of ​

ITranslator.format


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 LocaleTranslator.t finds a message for the key.

Implementation of ​

ITranslator.has


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.

Throws ​

When the options are invalid (a currency style without its currency).

Implementation of ​

ITranslator.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.

Implementation of ​

ITranslator.t

Released under the MIT License.