Interface LocalizationFunction

All Known Implementing Classes:
ResourceBundleLocalizationFunction

public interface LocalizationFunction
Functional interface accepting a localization key (complete path used to get the appropriate translations) and returning a map of discord locales to their localized strings

Implementation note: The localization key is composed of the command/option/choice tree being walked, where each command/option/choice's name is separated by a dot
Additionally, there is an "options" key between the command path and the name of the option, as well as a "choices" key between the options and the choices
Note: the final key is lowercase and spaces replaced by underscores

A few examples of localization keys:

  • The name of a command named "ban": ban.name
  • The name of a message context named "Get content raw": get_content_raw.name
  • The description of a command named "ban": ban.description
  • The name of a subcommand "perm" in a command named "ban": ban.perm.name
  • The description of an option "duration" in a subcommand "perm" in a command named "ban": ban.perm.options.duration.description
  • The name of a choice (here, "1 day") in an option "duration" in a subcommand "perm" in a command named "ban": ban.perm.options.duration.choices.1_day.name

Extremely naive implementation of LocalizationFunction

 public class MyFunction implements LocalizationFunction {
   @Override
   public Map<DiscordLocale, String> apply(String localizationKey) {
     Map<DiscordLocale, String> map = new HashMap<>();
     switch (localizationKey) {
       case "ban.name":
         map.put(DiscordLocale.SPANISH, "prohibiciĆ³n");
         map.put(DiscordLocale.FRENCH, "bannir");
         break;
       case "ban.description"
         map.put(DiscordLocale.SPANISH, "Prohibir a un usuario del servidor");
         map.put(DiscordLocale.FRENCH, "Bannir un utilisateur du serveur");
         break;
       //etc etc
     }

     return map;
   }
 }
 
Also, since this is a functional interface, the following is also possible

 LocalizationFunction myfunc = s -> {
   Map<DiscordLocale, String> map = new HashMap<>();
    switch (localizationKey) {
      case "ban.name":
        map.put(DiscordLocale.SPANISH, "prohibiciĆ³n");
        map.put(DiscordLocale.FRENCH, "bannir");
        break;
      case "ban.description"
        map.put(DiscordLocale.SPANISH, "Prohibir a un usuario del servidor");
        map.put(DiscordLocale.FRENCH, "Bannir un utilisateur du serveur");
        break;
      //etc etc
    }

    return map;
 }
 

You can look at a complete localization example here

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(String localizationKey)
    Retrieves the localization mappings of the specified localization key
  • Method Details

    • apply

      @Nonnull Map<DiscordLocale,String> apply(@Nonnull String localizationKey)
      Retrieves the localization mappings of the specified localization key
      Parameters:
      localizationKey - The localization key to get the translations from
      Returns:
      Never-null map of discord locales to their localized strings