Class I18n

java.lang.Object
com.github.kaktushose.jda.commands.i18n.I18n

public class I18n extends Object

This class serves as an interface for application localization.

It is mostly a wrapper around Localizer but supports flexible specification of the bundle to be used.

To state which bundle to use the direct way is to include it in the key following the format bundle#key. For example a message with key user#not-found will be searched for in the bundle user and the key not-found.

If no bundle is specified, it will traverse the stack (the called methods) and search for the nearest @Bundle("mybundle") annotation with following order:

  1. method that called localize(Locale, String, Entry...)
  2. other called methods in the same class
  3. this methods class
  4. the class' packages package-info.java file

If no annotation is found, the previous method (in another class) is searched with the same pattern up to the class at the very beginning.

If even after this no bundle name could be found, the bundle default will be used.

Example

A.java:

package my.app;
class A {
    void aOne() {
        i18n.localize(Locale.GERMAN, "fail", Map.of())
    }

    void aTwo() {
        aOne();
    }
}

B.java:

package my.app.other;

@Bundle("b_bundle")
class B {
    A another = new A();

    void bOne() {
        a.aOne();
    }

    @Bundle("mB_bundle")
    void bTwo() {
        bOne();
    }
}

package-info.java:

@Bundle("pack_bundle")
package my.app;

The order in which the bundle name is searched for is following:

  1. method A#aOne()
  2. method A#aTwo()
  3. class A
  4. package-info.java of package my.app
  5. method B#bOne()
  6. method B#two()

The found bundle would be pack_bundle.

If localize(java.util.Locale, java.lang.String, com.github.kaktushose.jda.commands.i18n.I18n.Entry...) would be called in, for example, B#bTwo the bundle would be mB_bundle.

  • Field Details

  • Constructor Details

  • Method Details

    • localize

      public String localize(Locale locale, String combinedKey, Map<String,Object> placeholder)

      This method returns the localized method found by the provided Locale and key in the given bundle.

      The bundle can be either explicitly stated by adding it to the key in the following format: bundle#key. Alternatively, the bundle name can also be contextual retrieved by a search for the Bundle annotation, see class docs.

      Parameters:
      locale - the Locale to be used to localize the key
      combinedKey - the messages key
      placeholder - the placeholder to be used
      Returns:
      the localized message or the key if not found
    • localize

      public String localize(Locale locale, String key, I18n.Entry... placeholder)

      This method returns the localized message found by the provided Locale and key in the given bundle.

      The bundle can be either explicitly stated by adding it to the key in the following format: bundle#key. Alternatively, the bundle name can also be contextual retrieved by a search for the Bundle annotation, see class docs.

      Parameters:
      locale - the Locale to be used to localize the key
      key - the messages key
      placeholder - the placeholder to be used
      Returns:
      the localized message or the key if not found
    • entry

      public static I18n.Entry entry(String name, Object value)
      This method returns an I18n.Entry containing the name and value provided. It comes in handy when imported with a static import.
      Parameters:
      name - the name of the placeholder
      value - the value of the placeholder
      Returns:
      the I18n.Entry consisting of the name and value
    • localizationFunction

      public LocalizationFunction localizationFunction()
      Returns:
      the LocalizationFunction bases on this class, for use with JDA