Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional translatable text information #182

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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