Class MiniMessageTranslator

java.lang.Object
net.kyori.adventure.text.minimessage.translation.MiniMessageTranslator
All Implemented Interfaces:
Translator

public abstract class MiniMessageTranslator extends Object implements Translator
A Translator implementation that translates strings using MiniMessage.

To use this feature, you should extend this class, implementing the getMiniMessageString(String, Locale) method to return the MiniMessage string for a given key and locale. After that, you can use the translator as-is using translate(TranslatableComponent, Locale), or automatically (depending on the implementing platform) using the GlobalTranslator.

This system supports arguments using <arg:0> tags (or argument, where 0 is the index of the argument to use). Alternatively, you can use named arguments by creating the translatable component using returned ComponentLike instances provided by the methods available in the Argument utility class. The provided name will be available for use in a tag as <name>, in addition to the index-based arg tag. These tags will use Tag.selfClosingInserting(Component) to create self-closing tags that insert a component representation of the argument. This can also be used to add tag instances using Argument.tag(String, Tag).

You can also make arbitrary tag resolvers available to the deserialization process by using the tagResolver methods on Argument. Note that these tag resolvers will not be available using the <arg:0> index-based standard tag and will not cause the index to be incremented. It is therefore recommended that you put these last in the translatable component arguments to avoid potential confusion.

An example of how you might construct a translatable component would be:


 Component.translatable(
   "my.translation.key", // the translation key, you'd return the MiniMessage string by implementing getMiniMessageString
   Component.text("hello"), // available as <arg:0> or <argument:0>
   Argument.string("today", "monday"), // available as <arg:1>, <argument:1> or <today>
   Argument.tag("danger", Tag.styling(NamedTextColor.RED)), // available as <arg:1>, <argument:1> or <red>, can be closed if needed
   Argument.tagResolver(StandardTags.pride()) // you can even add arbitrary tag resolvers!
 );
 

For an easier way to create a MiniMessage translator, see MiniMessageTranslationStore.

Since:
4.20.0
See Also:
  • Constructor Details

    • MiniMessageTranslator

      public MiniMessageTranslator()
      Constructor for a MiniMessageTranslator using the default MiniMessage instance.
      Since:
      4.20.0
      See Also:
    • MiniMessageTranslator

      public MiniMessageTranslator(@NotNull @NotNull MiniMessage miniMessage)
      Constructor for a MiniMessageTranslator using a specific MiniMessage instance.
      Parameters:
      miniMessage - the MiniMessage instance
      Since:
      4.20.0
      See Also:
  • Method Details

    • getMiniMessageString

      @Nullable protected abstract @Nullable String getMiniMessageString(@NotNull @NotNull String key, @NotNull @NotNull Locale locale)
      Returns a raw MiniMessage string for the given key.

      If no string is found for the given key, returning null will use the translatable component's fallback (or the key itself).

      Parameters:
      key - the key
      locale - the locale
      Returns:
      the resulting MiniMessage string
      Since:
      4.20.0
    • translate

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

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