@commandkit/i18n 0.1.0 → 0.1.1-dev.20250406075623

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +86 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -20,3 +20,89 @@ export default defineConfig({
20
20
  plugins: [i18n()]
21
21
  })
22
22
  ```
23
+
24
+ You can pass options to the plugin:
25
+
26
+ ```ts
27
+ import { defineConfig } from "commandkit"
28
+ import { i18n } from "@commandkit/i18n"
29
+
30
+ export default defineConfig({
31
+ plugins: [
32
+ i18n({
33
+ plugins: [i18nextPlugins]
34
+ })
35
+ ]
36
+ })
37
+ ```
38
+
39
+ Create `locales` directory inside `src/app` and add your translation files. The directory structure should look like this:
40
+
41
+ ```
42
+ src
43
+ └── app
44
+ ├── locales
45
+ │ ├── en-US
46
+ │ │ └── ping.json
47
+ │ └── fr
48
+ │ └── ping.json
49
+ └── commands
50
+ └── ping.ts
51
+ ```
52
+
53
+ CommandKit automatically localizes your commands if you follow the naming convention and translation file structure.
54
+
55
+ If your translation file contains `$command` key with localization object, it will be used to localize the command name and description.
56
+
57
+ ```json
58
+ {
59
+ "$command": {
60
+ "name": "Ping",
61
+ "description": "Ping the server",
62
+ "options": [
63
+ {
64
+ "name": "database",
65
+ "description": "Ping the database"
66
+ }
67
+ ]
68
+ },
69
+ "response": "Pong! The latency is {{latency}}ms"
70
+ }
71
+ ```
72
+
73
+ 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.
74
+
75
+ This plugin adds `locale()` function to your command context. You can use it to localize your command responses.
76
+
77
+ ```ts
78
+ export const chatInput: SlashCommand = async (ctx) => {
79
+ // ctx.locale() auto infers the localization of the current guild
80
+ // you can also pass a discord.js locale enum to use custom locale
81
+ // ctx.locale("fr") // uses french locale
82
+ const { t, i18n } = ctx.locale()
83
+ // ^ TFunction
84
+ // ^ i18next instance
85
+
86
+ ctx.interaction.reply({
87
+ content: t("response", { latency: client.ping }),
88
+ ephemeral: true
89
+ })
90
+ }
91
+ ```
92
+
93
+ 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.
94
+
95
+ ```ts
96
+ import { locale } from "@commandkit/i18n"
97
+
98
+ export async function run({ interaction }) {
99
+ const { t } = locale()
100
+
101
+ return interaction.reply({
102
+ content: t("response", { latency: client.ping }),
103
+ ephemeral: true
104
+ })
105
+ }
106
+ ```
107
+
108
+ This function is identical to the one in the context and it can also infer the locale automatically.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commandkit/i18n",
3
- "version": "0.1.0",
3
+ "version": "0.1.1-dev.20250406075623",
4
4
  "description": "CommandKit plugin that adds command localizations backed by i18next",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",