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: BotCommands-typesafe-messages/README.md
+3-98Lines changed: 3 additions & 98 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,107 +6,12 @@ without having to implement anything, alongside a few other benefits:
6
6
- Checks if template arguments map to function parameters, so all arguments have values
7
7
- Removes the need for magic strings (for the arguments), improving type safety and making regressions appear immediately
8
8
9
-
## Example
10
-
> [!NOTE]
11
-
> This example will use Kotlin but any other language should work.
9
+
## Usage
12
10
13
-
### Creating a localization bundle
14
-
Let's start by creating a localization bundle at `src/main/resources/bc_localization/MyBotMessages.json`,
15
-
for our example it will contain a single localization template
16
-
17
-
```json
18
-
{
19
-
"bot.info": "I am in {guild_count, number} {guild_count, choice, 0#guilds|1#guild|1<guilds} and I am up since {uptime_timestamp}."
20
-
}
21
-
```
22
-
23
-
- The key is `bot.info`
24
-
- The template is `I am in {guild_count, number} {guild_count, choice, 0#guilds|1#guild|1<guilds} and I am up since {uptime_timestamp}.`
25
-
-`guild_count` and `uptime_timestamp` are variables
26
-
-`number` and `choice` are format types
27
-
-`0#guilds|1#guild|1<guilds` is a subformat pattern for [ChoiceFormat](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/ChoiceFormat.html)
28
-
- See [MessageFormat](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/MessageFormat.html) for more details
29
-
30
-
### Creating our message source
31
-
32
-
Create an interface which extends `IMessageSource`;
33
-
it will contain functions annotated with `@LocalizedContent`,
34
-
the annotation's value is the key present in your localization bundle,
35
-
and the function needs to return a `String`.
36
-
37
-
```kt
38
-
interfaceCommandReplies : IMessageSource {
39
-
40
-
// The function can have any name you want
41
-
@LocalizedContent("bot.info")
42
-
funbotInfo(
43
-
// Parameter names are converted to snake_case for use in the template, here it's 'guild_count'
44
-
guildCount:Int,
45
-
// You could also pass a Timestamp as it has a proper `toString()`
46
-
uptimeTimestamp:String
47
-
): String
48
-
}
49
-
```
50
-
51
-
> [!NOTE]
52
-
> You do not need to implement this interface.
53
-
54
-
> [!TIP]
55
-
> You can inject instances of this interface in any interaction handler such as application commands, components and modals.
56
-
57
-
> [!TIP]
58
-
> You can override the locale using `@PreferLocale` or by passing a `DiscordLocale` or a `Locale` in the first parameter.
59
-
60
-
### Creating a factory for our source
61
-
62
-
We then need a way to get instances of our source;
63
-
create an interface extending `IMessageSourceFactory<CommandReplies>`
64
-
and annotate it with `@MessageSourceFactory("MyBotMessages")`,
65
-
the `MyBotMessages` string is the name of the bundle we added in the first step.
0 commit comments