Class Text

java.lang.Object
dev.xichil.mvui.Text

public final class Text extends Object
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's assets/<modid>/lang/* files, in the format of each version: .lang up to 1.12.2, .json from 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 a String.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 Details

    • translatable

      public static Text translatable(String key, String fallback, Object... args)
      Translatable text: shown as the running language's translation of key, or as fallback where the key has none.
      Parameters:
      key - the translation key, conventionally "<modid>.<what>", e.g. "mymod.save"
      fallback - the English text, a String.format(String, Object...) pattern with one %s per argument
      args - arguments for the pattern's %s: strings, numbers or Texts (copied)
      Returns:
      the text
      Throws:
      IllegalArgumentException - if key is null or empty
      NullPointerException - if fallback is null
    • literal

      public static Text literal(String text)
      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 - if text is null
    • key

      public String key()
      The translation key, or null for literal text.
      Returns:
      the key, or null for literal text
    • isLiteral

      public boolean isLiteral()
      Whether this is literal(String) text (it has no key).
      Returns:
      true for literal text, false for translatable text
    • fallback

      public String fallback()
      The English pattern for translatable text, the text itself for literal text.
      Returns:
      the fallback pattern, or the literal text
    • args

      public Object[] args()
      The arguments of translatable text.
      Returns:
      a copy of the arguments; empty for literal text
    • toEnglish

      public String toEnglish()
      The fallback with the arguments filled in, nested Text rendered 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

      public boolean equals(Object o)
      Equal if key, fallback and arguments are equal.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object