createTranslator
function in meocord/common Since 4.1.0
createTranslator<
const Locales extends Readonly<Record<string, CatalogShape>>,
const Default extends keyof Locales & `${Locale}`,
>(options: TranslatorOptions<Locales, Default>): Translator<Locales[Default]>Creates the application's translator from one catalog per locale.
Create it at module scope: command builders are built when the controllers using them are decorated, before any
container exists, and use it for names and descriptions. Pass the same instance to @MeoCord({ i18n }) to inject it
as Translator.
Each locale is checked against the default catalog when the code compiles: every key must be one the default has,
and a message may use only the {params} the default's message takes, and {count} in a plural's forms, in any
order, all or some. The error names each mistake, such as:
id: ban.done takes no {usr}; the default is "Banned {user}."Parameters
| Name | Type | Description |
|---|---|---|
options | TranslatorOptions<Locales, Default> | The default locale and a catalog per locale. |
Returns
Translator<Locales[Default]>The translator, typed by the default catalog.
Throws
When the default locale has no catalog, or a key is not a Discord locale.
Examples
const enUS = defineCatalog({ ban: { description: 'Ban a member', done: 'Banned {user}.' } })
const id = { ban: { description: 'Blokir anggota', done: '{user} diblokir.' } }
export const t = createTranslator({ default: 'en-US', locales: { 'en-US': enUS, id } })
new SlashCommandBuilder()
.setName('ban')
.setDescription(t.default('ban.description'))
.setDescriptionLocalizations(t.localizations('ban.description'))
@Command('ban', CommandType.SLASH)
async ban(interaction: ChatInputCommandInteraction) {
await respond(interaction).send(t.for(interaction)('ban.done', { user: interaction.user.toString() }))
}