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: KeyParamsArgs<C, K>
): stringTranslates into the default locale, as a command builder's name or description needs.
Parameters
| Name | Type |
|---|---|
key | K |
A message key of the default catalog. | |
...params | KeyParamsArgs<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 |
|---|---|
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 |
|---|---|
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 |
|---|---|
locale | Locale | `${Locale}` |
A discord.js | |
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
| Name | Type | Since |
|---|---|---|
key | K | |
A plain message key with | ||
params | LocalizationParams<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
paramsgive no value, orundefined, for a param the default message takes.
Examples
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
| Name | Type |
|---|---|
key | K |
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.