Class Locale

java.lang.Object
de.Keyle.MyPet.api.util.locale.Locale
All Implemented Interfaces:
Translator

public final class Locale extends Object implements Translator
MyPet's translation facility. Combines two roles:
  • Static facade — getComponent, getFormattedComponent, renderPlain, etc. that callers across the codebase use to look up a translation for a player, command sender, or explicit locale tag.
  • Adventure Translator implementation — a single instance is registered with GlobalTranslator on init(), so that any Component.translatable(...) created with a MyPet key resolves through this class regardless of where in the codebase it is rendered.

Bundles are loaded from locale/MyPet_<tag>.properties entries inside the plugin JAR, with sparse per-key overlay from plugins/MyPet/locale/ files of the same name. Values may contain MiniMessage tags and {0}/{1}-style placeholders that are substituted with the arguments passed via TranslatableComponent.arguments().

  • Method Details

    • init

      public static void init()
      Loads all translation bundles and registers this class as a translation source with GlobalTranslator. Idempotent — safe to call from both MyPetPlugin.onEnable and /mypet reload; an existing registration is removed before a fresh one is added.
    • getPlayerLanguage

      public static String getPlayerLanguage(Player player)
      Resolves a player's client locale, falling back to "en_us" when Bukkit returns an empty string.
      Parameters:
      player - the player whose client locale to read
      Returns:
      a lower-cased locale tag such as "en_us"
    • getCommandSenderLanguage

      public static String getCommandSenderLanguage(CommandSender sender)
      Resolves a command sender's locale: the player's client locale for Player senders, "en" for console.
      Parameters:
      sender - the sender whose locale to read
      Returns:
      a lower-cased locale tag
    • getComponent

      public static Component getComponent(String key, Player player)
      Renders a translation key against the player's client locale.
      Parameters:
      key - the translation key
      player - the reader; a null value renders the key itself
      Returns:
      the translated component
    • getComponent

      public static Component getComponent(String key, CommandSender sender)
      Renders a translation key against the sender's locale, or English for console.
      Parameters:
      key - the translation key
      sender - the reader; a null value renders the key itself
      Returns:
      the translated component
    • getComponent

      public static Component getComponent(String key, MyPetPlayer player)
      Renders a translation key against the owner's stored language.
      Parameters:
      key - the translation key
      player - the reader; a null value renders the key itself
      Returns:
      the translated component
    • getComponent

      public static Component getComponent(String key, String localeString)
      Renders a translation key against an explicit locale.
      Parameters:
      key - the translation key
      localeString - the locale tag to render for
      Returns:
      the translated component
    • getFormattedComponent

      public static Component getFormattedComponent(String key, Player player, Object... values)
      Renders a translation key against the player's client locale, substituting the given arguments.
      Parameters:
      key - the translation key
      player - the reader; a null value renders the key itself
      values - arguments substituted into the translation, in order
      Returns:
      the translated component
    • getFormattedComponent

      public static Component getFormattedComponent(String key, CommandSender sender, Object... values)
      Renders a translation key against the sender's locale, or English for console, substituting the given arguments.
      Parameters:
      key - the translation key
      sender - the reader; a null value renders the key itself
      values - arguments substituted into the translation, in order
      Returns:
      the translated component
    • getFormattedComponent

      public static Component getFormattedComponent(String key, MyPetPlayer player, Object... values)
      Renders a translation key against the owner's stored language, substituting the given arguments.
      Parameters:
      key - the translation key
      player - the reader; a null value renders the key itself
      values - arguments substituted into the translation, in order
      Returns:
      the translated component
    • getFormattedComponent

      public static Component getFormattedComponent(String key, String localeString, Object... values)
      Renders a translation key against an explicit locale, substituting the given arguments.
      Parameters:
      key - the translation key
      localeString - the locale tag to render for
      values - arguments substituted into the translation, in order
      Returns:
      the translated component
    • renderPlain

      public static String renderPlain(String key, String localeString)
      Renders a translation key to plain text against the given locale. Used by call sites that must materialize a translation into a String for storage (e.g. persisted pet names). All styling tags in the translation value are stripped.
      Parameters:
      key - the translation key
      localeString - the locale tag to render for
      Returns:
      the translation as plain text, with all styling removed
    • renderPlainAll

      public static Map<String,String> renderPlainAll(String key)
      Plain-text renderings of key in every loaded bundle that contains it.
      Parameters:
      key - the translation key
      Returns:
      locale tag to plain-text rendering, for every bundle holding the key
    • hasKey

      public static boolean hasKey(String key, String localeString)
      Read-only probe: true if MyPet has a translation for key resolvable for the given locale, using the exact same lookup + casing as rendering. Lets callers fall back before a missing key would surface as a raw translatable. Does not affect translate(java.lang.String, java.util.Locale).
      Parameters:
      key - the translation key to probe for
      localeString - the locale tag to resolve against
      Returns:
      true if a translation exists
    • name

      @NotNull public @NotNull Key name()
      Specified by:
      name in interface Translator
    • translate

      @Nullable public @Nullable MessageFormat translate(@NotNull @NotNull String key, @NotNull Locale locale)
      Specified by:
      translate in interface Translator
    • translate

      @Nullable public @Nullable Component translate(@NotNull @NotNull TranslatableComponent component, @NotNull Locale locale)
      Specified by:
      translate in interface Translator