You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/i18n/README.md
+86
Original file line number
Diff line number
Diff line change
@@ -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
+
exportdefaultdefineConfig({
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.
// 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
+
exportasyncfunction run({ interaction }) {
99
+
const { t } =locale()
100
+
101
+
returninteraction.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.
0 commit comments