Skip to content
GitHub

Translator

class in meocord/common Since 4.1.0

abstract class Translator<C = CatalogShape>

Translates messages from one catalog per locale, typed by the default one.

Make one with createTranslator at module scope, so command builders can use it, and pass it to @MeoCord({ i18n }) to inject it as Translator too.

Examples

TypeScript
const enUS = defineCatalog({ ban: { done: 'Banned {user}.' } })

@Service()
export class BanService {
  constructor(private readonly t: Translator<typeof enUS>) {}

  done(interaction: ChatInputCommandInteraction, user: User): string {
    return this.t.for(interaction)('ban.done', { user: user.toString() })
  }
}

Members

constructor

new Translator()

defaultLocale

abstract readonly defaultLocale: Locale

The locale whose catalog is the default.

locales

abstract readonly locales: readonly Locale[]

Every locale with a catalog, the default first.

default

abstract default<K extends MessageKey<C>>(
  key: K,
  ...params: ParamsArgs<MessageAt<C, K>>
): string

Translates into the default locale, as a command builder's name or description needs.

Parameters

NameTypeDescription
keyK

A message key of the default catalog.

...paramsParamsArgs<MessageAt<C, K>>

The message's params, when it takes any.

Returns

string

The message in the default locale.

for

abstract for(
  interaction: Interaction,
  options?: { public?: boolean },
): Translate<C>

Translates for the user of an interaction, in the language their Discord client uses.

Parameters

NameTypeDescription
interactionInteraction

The interaction being answered.

options?{ public?: boolean }

public: true uses the server's language instead, for a reply everyone there sees; outside a server, the user's.

options.public?boolean

Returns

Translate<C>

A function that translates a message key.

forGuild

abstract forGuild(guild: Guild): Translate<C>

Translates for a server, in its preferred language: for events and messages, which have no user locale.

Parameters

NameTypeDescription
guildGuild

The server to translate for.

Returns

Translate<C>

A function that translates a message key.

locale

abstract locale(locale: Locale | `${Locale}`): Translate<C>

Translates into a given locale.

Parameters

NameTypeDescription
localeLocale | `${Locale}`

A discord.js Locale; one without a catalog falls back as a user's would.

Returns

Translate<C>

A function that translates a message key.

localizations

abstract localizations(key: LocalizationKey<C>): Partial<Record<Locale, string>>

A message in every locale other than the default whose own catalog has it, for a builder's setNameLocalizations or setDescriptionLocalizations. Locales without it are left out, so Discord falls back to the default name for them, and so is a translation with a {param}, which expectCompleteCatalog reports.

Parameters

NameTypeDescription
keyLocalizationKey<C>

A plain message key without {params}: Discord shows a name or description as written, and has no plural forms for it.

Returns

Partial<Record<Locale, string>>

The message keyed by locale.

Throws

  • When the default catalog's message takes params, as one from a JSON file can.