Locales directory
The src/app/locales
directory is managed by the @commandkit/i18n plugin. This directory can be used to store the translation files for your commands. The translation files should be named according to the locale they represent. For example, if you have a translation file for ping
command in English, you should name it src/app/locales/en-US/ping.json
.
The translation file contains one special key called $command
, which is used to define the localization for the command. CommandKit will automatically merge these localizations before registering the command. The $command
object looks similar to the shape of a typical interaction command. The following example shows how to define the localization for the ping
command in English:
{
"$command": {
"name": "ping",
"description": "Ping command",
"options": [
{
"name": "verbose",
"description": "Show verbose output"
}
]
}
}
The below is an example of a typical translation file for the ping
command in English:
{
"$command": {
"name": "ping",
"description": "Ping command",
"options": [
{
"name": "verbose",
"description": "Show verbose output"
}
]
},
"success": "Pong! {{ping}}ms"
}
Then you can use the t
function to get the translation for the current command. The t
function is returned by the ctx.locale()
function.
import { CommandData } from 'commandkit';
import { ApplicationCommandOptionType } from 'discord.js';
export const command: CommandData = {
name: 'ping',
description: 'Ping command',
options: [
{
name: 'verbose',
description: 'Show verbose output',
type: ApplicationCommandOptionType.Boolean,
},
],
};
export const chatInput: ChatInputCommand = async (ctx) => {
const ping = Date.now() - ctx.interaction.createdTimestamp;
const verbose = ctx.interaction.options.getBoolean('verbose', false);
// locale is auto inferred from the interaction if not specified
const { t } = ctx.locale();
const response = t('success', { ping });
if (verbose) {
response += `\nVerbose: true`;
}
await ctx.interaction.reply(response);
};