Notice how your item has a weird display name, such as item.tutorial.my_item? This is because your item's name doesn't have a translation in your game's selected language. Translations are used to support multiple different languages for a single string.
You can use lang files to provide translations for translatable strings in-game. You'll need to create a file with an appropriate file name for your language– to find your languages' code, visit Minecraft Wiki. English is en_us. Once you have your language code, create a JSON file at resources/assets/modid/lang/
; a full example for an English translation file would be resources/assets/tutorial/lang/en_us.json
.
After you've created the lang file, you can use this basic template to add translations:
{ "item.tutorial.my_item": "My Item", "item.tutorial.my_awesome.item": "My Awesome Item", [...] }
where the keys in JSON is the translation key. If you're following along in the wiki tutorial, remember to change namespace tutorial
to the namespace you actually use.
Whenever a function accepts Text
, you have the option of giving it a new LiteralTextContent()
or Text.literal()
(for versions since 1.19), which means minecraft will use the string in the constructor argument as-is. However, this is not advisable because that would make it difficult to translate that text to another language, should you wish to do that. This is why whenever a Text
object is needed, you should give it a new TranslatableTextContent()
or Text.translatable
with a translation key, and then translate the key in the lang file.
For example, when adding a tooltip, do:
@Override public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) { // 1.18.2 and before tooltip.add(new TranslatableTextContent("item.tutorial.fabric_item.tooltip")); // 1.19 and later tooltip.add(Text.translatable("item.tutorial.fabric_item.tooltip")); }
And then add in the lang file:
{ "item.tutorial.fabric_item.tooltip": "My Tooltip" }
And the tooltip will be displayed as “My Tooltip”!
Say you want the text to change based on some variable, like the current day and month. For a dynamic number, we put a %s
where you want the number to show in the lang entry value, for example:
{ "item.tutorial.fabric_item.tooltip": "My Tooltip in day %s, and month %s" }
Then we pass the variables we use in our string by the order it appears in the text. First the day, then the month:
int currentDay = 4; int currentMonth = 7; // 1.18.2 and before: tooltip.add(new TranslatableTextContent("item.tutorial.fabric_item.tooltip", currentDay, currentMonth)); // 1.19 and later: tooltip.add(Text.translatable("item.tutorial.fabric_item.tooltip", currentDay, currentMonth));
In translations, the order of variables may change. Therefore, you can explicitly specify the index of variables by using something like %1$s
and %2$s
.
If you want for it to literally show %
, use %%
.
For more information, see Java String.format (it works the same way).
The line-break \n
work finely in command outputs. However in many cases, such as item tooltips, they may not workd correctly. So in order to have a string with multiple lines in the tooltip, you must split the translation key into multiple keys:
{ "item.tutorial.fabric_item.tooltip_1": "Line 1 of my tooltip", "item.tutorial.fabric_item.tooltip_2": "Line 2 of my tooltip" }
Then add the TranslatableTextContent
parts individually:
// 1.18.2 and below: tooltip.add(new TranslatableTextContent("item.tutorial.fabric_item.tooltip_1")); tooltip.add(new TranslatableTextContent("item.tutorial.fabric_item.tooltip_2")); // 1.19 and later tooltip.add(Text.translatable("item.tutorial.fabric_item.tooltip_1")); tooltip.add(Text.translatable("item.tutorial.fabric_item.tooltip_2"));And the tooltip will be displayed as:
Line 1 of my tooltip Line 2 of my tooltip