Skip to content

Responding using contexts

The framework allows you to inject contexts which allow you to get messages:

To get an instance of them, add them as a parameter of your interaction handler or text command, and annotate them with @LocalizationBundle.

Example

Using the following localization bundles:

Root bundle @ src/main/resources/bc_localization/ContextReplies.json
{
  "commands" : {
    "replies": {
      "context_localization": "I am translated using a context ! Sent {send_timestamp}"
    }
  }
}

The root bundle must exist as a fallback.

French bundle @ src/main/resources/bc_localization/ContextReplies_fr.json
{
  "commands" : {
    "replies": {
      "context_localization": "Je suis traduis avec un contexte ! Envoyé {send_timestamp}"
    }
  }
}

We reply using the user's locale (as provided by UserLocaleProvider), passing the send_timestamp argument.

@Command
class SlashContextLocalization {

    @JDASlashCommand(name = "context_localization", description = "Send localized responses using a context!")
    fun onSlashEventLocalization(
        event: GuildSlashEvent,
        @LocalizationBundle("ContextReplies") lc: AppLocalizationContext,
    ) {
        event.reply(lc.localizeUser("commands.replies.context_localization", "send_timestamp" to TimeFormat.RELATIVE.now()))
            .setEphemeral(true)
            .queue()
    }
}
@Command
public class SlashContextLocalization {

    @JDASlashCommand(name = "context_localization", description = "Send localized responses using the event!")
    public void onSlashEventLocalization(GuildSlashEvent event,
                                         @LocalizationBundle("ContextReplies") AppLocalizationContext lc) {
        // Use a static import for io.github.freya022.botcommands.api.localization.Localization.Entry.entry
        event.reply(lc.localizeUser("commands.replies.context_localization", entry("send_timestamp", TimeFormat.RELATIVE.now())))
                .setEphemeral(true)
                .queue();
    }
}