Class FluavaLocalizer

java.lang.Object
io.github.kaktushose.jdac.message.i18n.FluavaLocalizer
All Implemented Interfaces:
Localizer

public final class FluavaLocalizer extends Object implements Localizer

The default variant of Localizer which implements it with help of the amazing java implementation of project fluent.

Due to limitations set by fluent, dots contained in keys will be replaced by -. So my.key becomes my-key. Note that all localization keys generated by jda are also effected by this.

This class uses a sub instance of the passed Fluava instance with following changes:

IMentionable function

By default, this adds a Fluava function that automatically formats IMentionable by calling IMentionable.getAsMention(). This allows you to directly pass subtypes of IMentionable such as member or role as your placeholders.

You can disable this behavior by calling FluavaLocalizer.Builder#mentionableFunction(false). Alternatively, you can register your own Fluava function at the parent instance called MENTIONABLE to override this default implementation.

Implementation Note:
This class caches all loaded bundles.
  • Method Details

    • builder

      public static FluavaLocalizer.Builder builder(dev.goldmensch.fluava.Fluava parent)
      Creates a new FluavaLocalizer.Builder backed by the passed Fluava instance.
      Parameters:
      parent - the backing Fluava instance
      Returns:
      the newly created FluavaLocalizer.Builder
    • create

      public static FluavaLocalizer create(dev.goldmensch.fluava.Fluava parent)
      Creates a new FluavaLocalizer instance backed by the passed Fluava instance.
      Parameters:
      parent - the backing Fluava instance
      Returns:
      the newly created FluavaLocalizer
    • create

      public static FluavaLocalizer create(Locale fallback)
      Creates a new FluavaLocalizer with Fluava.fallback() set to passed locale. This is just a shorthand for: FluavaLocalizer.create(Fluava.create(<your locale>))
      Parameters:
      fallback - the Locale used for FluavaBuilder.fallback(Locale)
      Returns:
      the newly created FluavaLocalizer
    • localize

      public Optional<String> localize(Locale locale, String bundle, String key, Map<String, @Nullable Object> arguments)

      This method localizes a given key for a specific bundle and locale. If no message is found for this combination of locale, bundle and key or an error occurred Optional.empty() should be returned.

      For further information regarding functionality, you can take a look at the documentation of:

      • Fluava.loadBundle(String) for bundle loading
      • Bundle.apply(java.util.Locale, java.lang.String, java.util.Map) for localization of a given key
      Specified by:
      localize in interface Localizer
      Parameters:
      locale - the asked for locale
      bundle - the bundle to search the key in
      key - the key to be searched for. Note that all dots will be replaced by -.
      arguments - values for placeholder in the message
      Returns:
      the localized message or Optional.empty() if no key is found/an error occurred
    • localizeJDA

      public Optional<String> localizeJDA(Locale locale, String bundle, String key, Map<String, @Nullable Object> arguments)

      This method localizes a given key (coming from JDA's LocalizationFunction) for a specific bundle and locale. If no message is found for this combination of locale, bundle and key or an error occurred Optional.empty() should be returned.

      The difference to Localizer.localize(Locale, String, String, Map) is, that this method is only used inside our implementation of LocalizationFunction, thus the keys always follow the JDA format (parts are separated by a dot .).

      For example, project fluent (Fluava) disallows . as a "normal" separator in localization keys, therefore we have to replace all . with - and then pass it to the Localizer.localize(Locale, String, String, Map) method.

      For further information regarding functionality, you can take a look at the documentation of:

      • Fluava.loadBundle(String) for bundle loading
      • Bundle.apply(java.util.Locale, java.lang.String, java.util.Map) for localization of a given key
      Specified by:
      localizeJDA in interface Localizer
      Parameters:
      locale - the asked for locale
      bundle - the bundle to search the key in
      key - the key to be searched for. Note that all dots (.) will be replaced by -
      arguments - values for placeholder in the message
      Returns:
      the localized message or Optional.empty() if no key is found/an error occurred