Socket
Book a DemoInstallSign in
Socket

@commandkit/i18n

Package Overview
Dependencies
Maintainers
2
Versions
315
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@commandkit/i18n

CommandKit plugin that adds command localizations backed by i18next

0.1.2
latest
Source
npmnpm
Version published
Weekly downloads
1.2K
7.71%
Maintainers
2
Weekly downloads
 
Created
Source

@commandkit/i18n

A commandkit plugin for i18next integration.

Installation

$ npm i @commandkit/i18n

Usage

Add the following to your commandkit config:

import { defineConfig } from "commandkit"
import { i18n } from "@commandkit/i18n"

export default defineConfig({
  plugins: [i18n()]
})

You can pass options to the plugin:

import { defineConfig } from "commandkit"
import { i18n } from "@commandkit/i18n"

export default defineConfig({
  plugins: [
    i18n({
      plugins: [i18nextPlugins]
    })
  ]
})

Create locales directory inside src/app and add your translation files. The directory structure should look like this:

src
└── app
    ├── locales
    │   ├── en-US
    │   │   └── ping.json
    │   └── fr
    │       └── ping.json
    └── commands
        └── ping.ts

CommandKit automatically localizes your commands if you follow the naming convention and translation file structure.

If your translation file contains $command key with localization object, it will be used to localize the command name and description.

{
  "$command": {
    "name": "Ping",
    "description": "Ping the server",
    "options": [
      {
        "name": "database",
        "description": "Ping the database"
      }
    ]
  },
  "response": "Pong! The latency is {{latency}}ms"
}

The $command key defines localization for the command name and description (or options). These properties are later merged with the actual command to build the final command object with localizations that Discord understands. Anything else in the translation file is used to localize the command response.

This plugin adds locale() function to your command context. You can use it to localize your command responses.

export const chatInput: SlashCommand = async (ctx) => {
  // ctx.locale() auto infers the localization of the current guild
  // you can also pass a discord.js locale enum to use custom locale
  // ctx.locale("fr") // uses french locale
  const { t, i18n } = ctx.locale()
  //      ^ TFunction
  //         ^ i18next instance

  ctx.interaction.reply({
    content: t("response", { latency: client.ping }),
    ephemeral: true
  })
}

Additionally, you can use the locale() function outside of the context to get the localization api. This is mostly useful if you are using i18n with legacy commands plugin.

import { locale } from "@commandkit/i18n"

export async function run({ interaction }) {
  const { t } = locale()

  return interaction.reply({
    content: t("response", { latency: client.ping }),
    ephemeral: true
  })
}

This function is identical to the one in the context and it can also infer the locale automatically.

Keywords

commandkit

FAQs

Package last updated on 06 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.