i18n Plugin
The i18n plugin integrates i18next into CommandKit, enabling you to create multilingual Discord bots that can automatically adapt to your users' preferred languages. This plugin provides seamless internationalization support for commands, events, and all bot interactions.
Featuresβ
- π Automatic locale detection - Automatically uses Discord's guild preferred locale
- π§ Easy setup - Simple configuration with sensible defaults
- π File-based translations - Organize translations in JSON files
- π― Context-aware - Access translations in commands, events, and legacy handlers
- π i18next ecosystem - Full compatibility with i18next plugins and features
- π Command metadata localization - Localize command names, descriptions, and options
Installationβ
- npm
- yarn
- pnpm
npm install @commandkit/i18n
yarn add @commandkit/i18n
pnpm add @commandkit/i18n
Basic Setupβ
Add the i18n plugin to your CommandKit configuration:
import { defineConfig } from 'commandkit';
import { i18n } from '@commandkit/i18n';
export default defineConfig({
plugins: [i18n()],
});
Advanced Configurationβ
You can customize the i18n plugin by passing options to it:
import { defineConfig } from 'commandkit';
import { i18n } from '@commandkit/i18n';
export default defineConfig({
plugins: [
i18n({
plugins: [someI18nextPlugin],
// Add other i18next configuration options as needed
}),
],
});
Translation Files Structureβ
Create a locales
directory inside your src/app
folder with subdirectories for each language. Each language directory should contain JSON files for your translations.
src
βββ app
βββ locales
β βββ en-US
β β βββ ping.json
β β βββ messageCreate.event.json
β βββ fr
β βββ ping.json
β βββ messageCreate.event.json
βββ commands
β βββ ping.ts
β βββ help.ts
βββ events
βββ messageCreate
βββ handler.ts
Supported Localesβ
CommandKit uses Discord's locale identifiers. Please refer to Discord's Locales documentation for a complete list.
Quick Start Exampleβ
Here's a complete example to get you started:
- Configure the plugin:
import { defineConfig } from 'commandkit';
import { i18n } from '@commandkit/i18n';
export default defineConfig({
plugins: [i18n()],
});
- Create translation files:
{
"$command": {
"name": "ping",
"description": "Check the bot's latency"
},
"response": "π Pong! Latency: **{{latency}}ms**"
}
{
"$command": {
"name": "ping",
"description": "VΓ©rifier la latence du bot"
},
"response": "π Pong! Latence: **{{latency}}ms**"
}
- Use translations in your command:
import type { ChatInputCommand, CommandData } from 'commandkit';
export const command: CommandData = {
name: 'ping',
description: "Check the bot's latency",
};
export const chatInput: ChatInputCommand = async (ctx) => {
const { t } = ctx.locale();
await ctx.interaction.reply({
content: t('response', { latency: ctx.client.ws.ping }),
});
};
That's it! Your bot will now automatically respond in the user's guild preferred language.