@commandkit/i18n plugin provides internationalization (i18n) support for your CommandKit bot using i18next. Translate commands, responses, and interactions into multiple languages.
Installation
Setup
Add translations
Create translation files matching your command names:
src/app/locales/en-US/ping.json
src/app/locales/es-ES/ping.json
Import and add the plugin
Import the i18n plugin and add it to your CommandKit instance.
src/index.ts
Translation Files
File Structure
Translation files must be organized by locale and named after commands:Translation Format
Example: Complete Command Translation
Configuration
Customize i18n behavior with configuration options:src/index.ts
i18next initialization options.See i18next documentation for all options.
Default namespace for translations.
Fallback language when translation is missing.
Enable debug logging.
Additional i18next plugins to use.
Usage
In Commands
Access translations using thelocale() method:
src/commands/greet.ts
Interpolation
Pass variables to translations:Nested Keys
Access nested translation keys:Plurals
Handle pluralization:Specific Locale
Get translations for a specific locale:Hooks
locale()
Get localization context. Works in commands and events.
fetchT()
Get a translation function for a specific locale and namespace:
useI18n()
Get the i18next instance:
Event Translations
Translate content in event handlers:src/events/ready/log.ts
src/app/locales/en-US/ready.event.json
Context Menu Commands
Translate user and message context menu commands:Advanced Examples
Multi-Language Embed
src/commands/stats.ts
Language Selector Command
src/commands/language.ts
Supported Locales
CommandKit supports all Discord locales:en-US- English (US)en-GB- English (UK)es-ES- Spanishfr- Frenchde- Germanit- Italianpt-BR- Portuguese (Brazil)ru- Russianja- Japaneseko- Koreanzh-CN- Chinese (Simplified)zh-TW- Chinese (Traditional)- And more…
Best Practices
Organize by Command
Name translation files after commands for automatic namespace matching.
Use Fallback Language
Always provide en-US translations as fallback.
Consistent Keys
Use the same translation keys across all locales.
Context Matters
Provide translators with context about where text appears.
API Reference
i18n(options?)
Creates the i18n plugin instance.
Parameters:
options.plugins- Additional i18next pluginsoptions.i18nOptions- i18next initialization options
[I18nPlugin, I18nCliTemplatePlugin]
locale(locale?)
Gets the localization context.
Parameters:
locale- Optional locale to use (defaults to detected locale)
CommandLocalizationContext
fetchT(lng, ns?, keyPrefix?)
Fetches a translation function.
Parameters:
lng- Locale or array of localesns- Namespace (optional)keyPrefix- Key prefix (optional)
TFunction
useI18n()
Gets the i18next instance.
Returns: i18n
Throws: Error if i18n plugin is not registered.