Class Text
java.lang.Object
dev.xichil.mvui.Text
A piece of user-visible text, independent of Minecraft's text classes (which differ between versions). Immutable.
Every label, title, tooltip and message in this API is a
Text. There are two kinds:
- Translatable (
translatable(String, String, Object...)): a translation key, an English fallback and optional arguments. Backends turn it into the running version's translated text, looking the key up in the loaded language files (the mod'sassets/<modid>/lang/*files, in the format of each version:.langup to 1.12.2,.jsonfrom 1.13). If the key has no translation, the fallback is shown instead, so a mod works before it ships any lang file, and where the mod's lang files aren't loaded at all (Fabric without Fabric API). Write fallbacks as finished English text. - Literal (
literal(String)): shown as-is in every language. For player-made content: profile names, file names, server addresses, numbers.
Text title = Text.translatable("mymod.title", "My Mod Settings");
Text count = Text.translatable("mymod.count", "%s of %s enabled", enabled, total);
Text saved = Text.translatable("mymod.saved", "Saved to %s", Text.literal(fileName)); // nested Text
Text name = Text.literal(profile.name());
Fallback patterns and arguments
The fallback is aString.format(String, Object...) pattern, as Minecraft's lang files are: use %s for each argument (or
%1$s, %2$s to reorder). Arguments may be strings, numbers, booleans or other Texts, which
are translated too; other objects are shown as their toString(). Prefer %s over number formats like %d: Minecraft's own translation only supports
%s and positional %n$s.-
Method Summary
Modifier and TypeMethodDescriptionObject[]args()The arguments of translatable text.booleanEqual if key, fallback and arguments are equal.fallback()The English pattern for translatable text, the text itself for literal text.inthashCode()booleanWhether this isliteral(String)text (it has no key).key()The translation key, ornullfor literal text.static TextText shown as-is in every language, e.g.The fallback with the arguments filled in, nestedTextrendered in English too; what a player would see without any translation.toString()static Texttranslatable(String key, String fallback, Object... args) Translatable text: shown as the running language's translation ofkey, or asfallbackwhere the key has none.
-
Method Details
-
translatable
Translatable text: shown as the running language's translation ofkey, or asfallbackwhere the key has none.- Parameters:
key- the translation key, conventionally"<modid>.<what>", e.g."mymod.save"fallback- the English text, aString.format(String, Object...)pattern with one%sper argumentargs- arguments for the pattern's%s: strings, numbers orTexts (copied)- Returns:
- the text
- Throws:
IllegalArgumentException- ifkeyis null or emptyNullPointerException- iffallbackis null
-
literal
Text shown as-is in every language, e.g. a profile name or a file pattern. Not a format pattern:%is shown as it is.- Parameters:
text- the text- Returns:
- the text
- Throws:
NullPointerException- iftextis null
-
key
The translation key, ornullfor literal text.- Returns:
- the key, or
nullfor literal text
-
isLiteral
public boolean isLiteral()Whether this isliteral(String)text (it has no key).- Returns:
truefor literal text,falsefor translatable text
-
fallback
The English pattern for translatable text, the text itself for literal text.- Returns:
- the fallback pattern, or the literal text
-
args
The arguments of translatable text.- Returns:
- a copy of the arguments; empty for literal text
-
toEnglish
The fallback with the arguments filled in, nestedTextrendered in English too; what a player would see without any translation. For tests and logs: backends show the translation instead. If the pattern doesn't fit the arguments, the bare fallback is returned rather than throwing.- Returns:
- the English text
-
equals
-
hashCode
-
toString
-