Class ResourceBundleLocalizationFunction

java.lang.Object
net.dv8tion.jda.api.interactions.commands.localization.ResourceBundleLocalizationFunction
All Implemented Interfaces:
LocalizationFunction

public class ResourceBundleLocalizationFunction extends Object implements LocalizationFunction
A default implementation for LocalizationFunction

This implementation supports Java's ResourceBundle to be used as a localization source

You can look at a complete localization example here

  • Method Details

    • apply

      @Nonnull public Map<DiscordLocale,String> apply(@Nonnull String localizationKey)
      Description copied from interface: LocalizationFunction
      Retrieves the localization mappings of the specified localization key
      Specified by:
      apply in interface LocalizationFunction
      Parameters:
      localizationKey - The localization key to get the translations from
      Returns:
      Never-null map of discord locales to their localized strings
    • fromBundle

      Creates an empty ResourceBundleLocalizationFunction builder and adds the provided bundle and locale.
      This is the same as using ResourceBundleLocalizationFunction.empty().addBundle(resourceBundle, locale)

      Example usage:
      This creates a LocalizationFunction from a French ResourceBundle (MyCommands_fr.properties)

      
           final LocalizationFunction localizationFunction = ResourceBundleLocalizationFunction
                       .fromBundle(ResourceBundle.getBundle("MyCommands", Locale.FRENCH), DiscordLocale.FRENCH)
                       .build();
       
      Parameters:
      resourceBundle - The resource bundle to get the localized strings from
      locale - The locale of the resources
      Returns:
      The new builder
      Throws:
      IllegalArgumentException -
    • fromBundles

      @Nonnull public static ResourceBundleLocalizationFunction.Builder fromBundles(@Nonnull String baseName, @Nonnull DiscordLocale... locales)
      Creates a ResourceBundleLocalizationFunction builder with the provided bundles.
      This will insert the resource bundles with the specified name, with each specified locale.
      This is the same as using ResourceBundleLocalizationFunction.empty().addBundles(baseName, locales)

      Example usage:
      This creates a LocalizationFunction from 2 resource bundles, one in Spanish (MyCommands_es_ES.properties) and one in French (MyCommands_fr.properties)

      
           final LocalizationFunction localizationFunction = ResourceBundleLocalizationFunction
                               .fromBundles("MyCommands", DiscordLocale.SPANISH, DiscordLocale.FRENCH)
                               .build();
       
      Parameters:
      baseName - The base name of the resource bundle, for example, the base name of "MyBundle_fr_FR.properties" would be "MyBundle"
      locales - The locales to get from the resource bundle
      Returns:
      The new builder
      Throws:
      IllegalArgumentException -
      • If the base name is null
      • If the locales or one of the locale is null
      • If one of the locale is DiscordLocale.UNKNOWN
    • empty

      Creates an empty ResourceBundleLocalizationFunction builder.
      Returns:
      The empty builder