Skip to content

Commit

Permalink
Additional information about translatable text (FabricMC#182)
Browse files Browse the repository at this point in the history
Co-authored-by: Calum H. <contact@mineblock11.dev>
  • Loading branch information
LordEnder-Kitty and IMB11 authored Dec 27, 2024
1 parent 2edd223 commit f13fb5f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions develop/text-and-translations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: Text and Translations
description: Comprehensive documentation for Minecraft's handling of formatted text and translations.
authors:
- IMB11
- LordEnder-Kitty
---

# Text and Translations {#text-and-translations}
Expand Down Expand Up @@ -59,6 +60,36 @@ The language file, `en_us.json`, looks like the following:
}
```

If you wish to be able to use variables in the translation, similar to how death messages allow you to use the involved players and items in the translation, you may add said variables as parameters. You may add however many parameters you like.

```java
Text translatable = Text.translatable("my_mod.text.hello", player.getDisplayName());
```

You may reference these variables in the translation like so:

```json
{
"my_mod.text.hello": "%1$s said hello!"
}
```

In the game, %1\$s will be replaced with the name of the player you referenced in the code. Using `player.getDisplayName()` will make it so that additional information about the entity will appear in a tooltip when hovering over the name in the chat message as opposed to using `player.getName()`, which will still get the name; however, it will not show the extra details. Similar can be done with itemStacks, using `stack.toHoverableText()`.

As for what %1\$s even means, all you really need to know is that the number corresponds to which variable you are trying to use. Let's say you have three variables that you are using.

```java
Text translatable = Text.translatable("my_mod.text.whack.item", victim.getDisplayName(), attacker.getDisplayName(), itemStack.toHoverableText());
```

If you want to reference what, in our case, is the attacker, you would use %2\$s because it's the second variable that we passed in. Likewise, %3\$s refers to the itemStack. A translation with this many additional parameters might look like this:

```json
{
"my_mod.text.whack.item": "%1$s was whacked by %2$s using %3$s"
}
```

## Serializing Text {#serializing-text}

<!-- NOTE: These have been put into the reference mod as they're likely to be updated to codecs in the next few updates. -->
Expand Down

0 comments on commit f13fb5f

Please sign in to comment.