Class MiniMessageTranslator
- All Implemented Interfaces:
Translator
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).
By default, the target will be a Pointered
instance that solely contains the Locale of the translation.
The locale can be obtained from the target using the Identity.LOCALE pointer.
You can override the target by using Argument.target(Pointered).
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 Summary
ConstructorsConstructorDescriptionConstructor for a MiniMessageTranslator using the default MiniMessage instance.MiniMessageTranslator(MiniMessage miniMessage) Constructor for a MiniMessageTranslator using a specific MiniMessage instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract @Nullable StringgetMiniMessageString(String key, Locale locale) Returns a raw MiniMessage string for the given key.final @Nullable MessageFormatGets a message format from a key and locale.final @Nullable Componenttranslate(TranslatableComponent component, Locale locale) Gets a translated component from a translatable component and locale.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Translator
canTranslate, hasAnyTranslations, name
-
Constructor Details
-
MiniMessageTranslator
public MiniMessageTranslator()Constructor for a MiniMessageTranslator using the default MiniMessage instance.- Since:
- 4.20.0
- See Also:
-
MiniMessageTranslator
Constructor for a MiniMessageTranslator using a specific MiniMessage instance.- Parameters:
miniMessage- the MiniMessage instance- Since:
- 4.20.0
- See Also:
-
-
Method Details
-
getMiniMessageString
Returns a raw MiniMessage string for the given key.If no string is found for the given key, returning
nullwill use thetranslatable component's fallback(or the key itself).- Parameters:
key- the keylocale- the locale- Returns:
- the resulting MiniMessage string
- Since:
- 4.20.0
-
translate
Description copied from interface:TranslatorGets a message format from a key and locale.When used in the
GlobalTranslator, this method is called only ifTranslator.translate(TranslatableComponent, Locale)returnsnull.- Specified by:
translatein interfaceTranslator- Parameters:
key- a translation keylocale- a locale- Returns:
- a message format or
nullto skip translation
-
translate
Description copied from interface:TranslatorGets a translated component from a translatable component and locale.Care should be taken to ensure you do not unintentionally remove the children or style of
component. This can be avoided by copying over the children/style using the following code as an example:final Component myNewComponent = ...; // get your component here return myNewComponent .append(component.children()) // ensure it has the original components children as well .applyFallbackStyle(component.style()); // apply a "fallback" style- Specified by:
translatein interfaceTranslator- Parameters:
component- a translatable componentlocale- a locale- Returns:
- a translated component or
nullto useTranslator.translate(String, Locale)instead (if available)
-