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
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: LocaleThe 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>>
): stringTranslates into the default locale, as a command builder's name or description needs.
Parameters
| Name | Type | Description |
|---|---|---|
key | K | A message key of the default catalog. |
...params | ParamsArgs<MessageAt<C, K>> | The message's params, when it takes any. |
Returns
stringThe 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
| Name | Type | Description |
|---|---|---|
interaction | Interaction | The interaction being answered. |
options? | { public?: boolean } |
|
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
| Name | Type | Description |
|---|---|---|
guild | Guild | 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
| Name | Type | Description |
|---|---|---|
locale | Locale | `${Locale}` | A discord.js |
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
| Name | Type | Description |
|---|---|---|
key | LocalizationKey<C> | A plain message key without |
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.