Skip to content

Translation and Configuration and Variables

JDA-Boot provides robust systems for managing translations and configurations, allowing you to create dynamic and localized bots with ease. This page explains how to use these systems and customize them for your project.

Translation System

The translation system in JDA-Boot allows you to use localized strings in your bot. By default, JDA-Boot uses Java resource bundles for translations, but you can also implement your own TranslationProvider.

Using Translations

Translations can be used in any place where normal variables are supported by using the syntax #{TRANSLATION_KEY}. You can also use variables within translations to dynamically customize the content.

Default Translation Files

  • The default language file is messages.properties.
  • For other languages, use the format messages_<lang>.properties (e.g., messages_de.properties for German).

Example

messages.properties:

welcome.message=Welcome, ${username}!

Usage in code:

@Embed(
    title = "#{welcome.message}",
    description = "Enjoy your stay!"
)
private TemplateEmbed embed;

If the username variable is set to "John", the title will render as "Welcome, John!".

Custom Translation Providers

You can create a custom translation provider by implementing the TranslationProvider interface and setting it in the translationProvider field of the @JDABootConfiguration annotation.

Example

@JDABootConfiguration(
    translationProvider = CustomTranslationProvider.class
)
public class Main {
    // Main Class
}

Configuration System

The configuration system in JDA-Boot allows you to manage settings for your bot. Configuration values can be retrieved dynamically or injected into fields using annotations.

Using Configuration

Configuration values can be used in any place where normal variables are supported by using the syntax ?{CONFIG_KEY}. Alternatively, you can use the @SetValue annotation to inject configuration values into fields.

Example

config.yml:

app:
  name: "MyBot"

config.properties:

app.name=MyBot

Usage in code:

@SetValue("app.name")
private String appName;

The appName field will be automatically set to "MyBot".

Configuration Providers

By default, JDA-Boot supports the following configuration providers: 1. Environment Variables (including .env files): Variables must have the prefix JDA_BOOT_ and be in uppercase with underscores (e.g., JDA_BOOT_APP_NAME). 2. Properties Files: Configuration can be stored in .properties files. 3. YAML Files: Configuration can be stored in .yml files.

These providers are organized in a chain, so if a value is not found in one provider, the next provider in the chain is used. The default order is: 1. Environment Variables 2. Properties Files 3. YAML Files

Configuration Profiles

You can define configuration profiles to manage different environments (e.g., development, production). The active profile is determined by the profile configuration key. For example, if the profile is set to dev, the files config-dev.yml or config-dev.properties will be used.

Example

config.yml:

profile: "dev"

config-dev.yml:

app:
  name: "MyBot (Development)"

config.properties:

profile=dev

config-dev.properties:

app.name=MyBot (Development)

Custom Configuration Providers

You can create a custom configuration provider by implementing the ConfigProvider class and adding it to the additionalConfigProviders field in the @JDABootConfiguration annotation.

Example

@JDABootConfiguration(
    additionalConfigProviders = {CustomConfigProvider.class}
)
public class Main {
    // Main Class
}

Global Variables

Global variables are accessible throughout your entire application and can be set or updated at runtime. They are useful for values that need to be shared across different parts of your bot.

Setting Global Variables

You can set global variables either as static values or as dynamic values using a Supplier. Dynamic variables allow the value to be computed each time it is accessed.

Example

// Static global variable
GlobalVariables.set("botVersion", "1.0");

// Dynamic global variable (e.g., current time)
GlobalVariables.setDynamicValue("currentTime", () -> LocalDateTime.now().toString());

Now, you can use ${botVersion} or ${currentTime} in your embeds or anywhere variables are supported.

Default Variables

Variable Name Content
guildCount The number of guilds in which the bot is
shardCount The number of shards
selfUsername The current username of the bot application

Variable Resolution Order

When resolving a variable, JDA-Boot checks the following sources in order:

  1. Local variables (set on the specific component)
  2. Global variables (set via GlobalVariables)
  3. Default variables (set via the annotation)