Class Localizer

java.lang.Object
de.eldoria.eldoutilities.localization.Localizer
All Implemented Interfaces:
ILocalizer

public class Localizer extends Object implements ILocalizer
Compact localizer class.

Easy to use and fully automatic setup and updating of locales.

Requires to have at least one default locale and one fallback locale in the resources. Use the create(Plugin, String...) constructor for initial setup. This will create missing files and updates existing files.

You can change the currently used locale every time via setLocale(String).

The localizer also allows to use locales which are not included in the resources folder.

Since:
1.0.0
  • Method Details

    • create

      @Deprecated(forRemoval=true) public static ILocalizer create(Plugin plugin, String... includedLocales)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use the builder provided by builder(Plugin, String)
      Create a new localizer instance with default values.

      The message path and prefix will be "messages" and the fallback language the "en_US" locale.

      This instance will create locale files, which are provided in the "resources" directory.

      After this it will update all locale files inside the locales directory. For this the ref keys from the internal default locale file will be used.

      After a update check and an update if needed it will load the provided language or the fallback language if the provided language does not exist.

      Parameters:
      plugin - instance of plugin
      includedLocales - internal provided locales
      Returns:
      the created localizer instance
    • create

      @Deprecated(forRemoval=true) public static ILocalizer create(Plugin plugin, String localesPath, String localesPrefix, Locale fallbackLocale, String... includedLocales)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use the builder provided by builder(Plugin, String)
      Create a new localizer instance.

      This instance will create locale files, which are provided in the "resources" directory.

      After this it will update all locale files inside the locales directory. For this the ref keys from the internal default locale file will be used.

      After a update check and an update if needed it will load the provided language or the fallback language if the provided language does not exist.

      Parameters:
      plugin - instance of plugin
      localesPath - path of the locales directory
      localesPrefix - prefix of the locale files
      fallbackLocale - fallbackLocale
      includedLocales - internal provided locales
      Returns:
      the created localizer instance
    • builder

      public static LocalizerBuilder builder(Plugin plugin, String defaultLocale)
    • plugin

      public Plugin plugin()
      Specified by:
      plugin in interface ILocalizer
    • setLocale

      @Deprecated(forRemoval=true) public void setLocale(String language)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use
      Change the locale to the language. If the locale is not present the fallback locale will be used.
      Specified by:
      setLocale in interface ILocalizer
      Parameters:
      language - language to be used
    • getMessage

      public String getMessage(String key)
      Description copied from interface: ILocalizer
      Get a message.
      Specified by:
      getMessage in interface ILocalizer
      Parameters:
      key - message key
      Returns:
      message with replaced replacements if present.
    • setDefaultLocale

      public void setDefaultLocale(String language)
    • getMessage

      public String getMessage(String key, @Nullable @Nullable CommandSender sender)
      Specified by:
      getMessage in interface ILocalizer
    • getValue

      @Nullable public @Nullable String getValue(String key)
      Specified by:
      getValue in interface ILocalizer
    • getMessage

      public String getMessage(String key, String language)
      Specified by:
      getMessage in interface ILocalizer
    • getValue

      @Nullable public @Nullable String getValue(String key, CommandSender sender)
      Specified by:
      getValue in interface ILocalizer
    • getValue

      @Nullable public @Nullable String getValue(String key, String language)
      Specified by:
      getValue in interface ILocalizer
    • localeBundle

      public ResourceBundle localeBundle(String language)
      Specified by:
      localeBundle in interface ILocalizer
    • defaultBundle

      public ResourceBundle defaultBundle()
      Specified by:
      defaultBundle in interface ILocalizer
    • localize

      public String localize(String message)
      Translates a String with Placeholders. Can handle multiple messages with replacements.
      Specified by:
      localize in interface ILocalizer
      Parameters:
      message - Message to translate
      Returns:
      Replaced Messages
    • localize

      public String localize(CommandSender sender, String message)
      Description copied from interface: ILocalizer
      Translates a String with Placeholders. Can handle multiple messages with replacements. Add replacements in the right order.
      Specified by:
      localize in interface ILocalizer
      message - Message to translate
      Returns:
      Replaced Messages
    • getIncludedLocales

      @Deprecated(forRemoval=true) public String[] getIncludedLocales()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Get currently registered locales.
      Specified by:
      getIncludedLocales in interface ILocalizer
      Returns:
      array of available locales.
    • includedLocales

      public Set<String> includedLocales()
      Description copied from interface: ILocalizer
      Returns all available locales.
      Specified by:
      includedLocales in interface ILocalizer
      Returns:
      set of registered locales.
    • addLocaleCodes

      public void addLocaleCodes(Map<String,String> runtimeLocaleCodes)
      Description copied from interface: ILocalizer
      Add requested locale codes in runtime.

      This has to be done before calling ILocalizer.setLocale(String)

      Every key has one default value which will be added to the file if the key is not present.

      Specified by:
      addLocaleCodes in interface ILocalizer
      Parameters:
      runtimeLocaleCodes - map with locales codes to add.
    • registerChild

      public void registerChild(ILocalizer localizer)
      Specified by:
      registerChild in interface ILocalizer