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: KeyParamsArgs<C, K>
): string

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

Parameters

NameType
keyK

A message key of the default catalog.

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

NameType
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

NameType
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

NameType
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<K extends ParamLocalizationKey<C>>(
  key: K,
  params: LocalizationParams<MessageAt<C, K>>,
): Partial<Record<Locale, string>>

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

A message with {params} in every locale other than the default whose own catalog has it, each filled with its own placement of the params, for a builder's setNameLocalizations or setDescriptionLocalizations.

A param's value is a word or a number, used as written in every locale, or a function of the locale for a value that is itself translated, such as a mode's name. The function is called once for each locale whose message uses it. A value, static or from a function, is used as written: a {param} in it stays as text, as t() leaves it. Otherwise the result follows the rules of localizations(key): locales without the message and empty translations are left out, and the default's own message, filled, is added beside its fallback partner.

Parameters

NameTypeSince
keyK

A plain message key with {params}, typed from the default catalog; plurals are refused.

paramsLocalizationParams<MessageAt<C, K>>4.3.0

A value for each of the default message's params.

Returns

Partial<Record<Locale, string>>

The filled message keyed by locale.

Throws

  • When params give no value, or undefined, for a param the default message takes.

Examples

TypeScript
const enUS = defineCatalog({ lookup: { user: 'Show {mode} for a linked user' }, modes: { chaos: 'Chaos mode' } })
const ja = { lookup: { user: '連携済みユーザーの{mode}を表示' }, modes: { chaos: '混沌モード' } }
const t = createTranslator({ default: 'en-US', locales: { 'en-US': enUS, ja } })

const mode = (locale: Locale) => t.locale(locale)('modes.chaos')
new SlashCommandUserOption()
  .setName('user')
  .setDescription(t.default('lookup.user', { mode: t.default('modes.chaos') }))
  .setDescriptionLocalizations(t.localizations('lookup.user', { mode }))

Parameters

NameType
keyK

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.