This is an old revision of the document!
Table of Contents
名字翻译
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.
创建一个lang文件
您可以使用lang文件为游戏中的可翻译字符串提供翻译。 您需要使用适合您的语言的文件名来创建文件-要找到您的语言代码,请访问this link. 英语是en_us 简体中文是zh_cn 繁体中文是zh_tw 输入语言代码后, 在创建一个JSON文件 resources/assets/modid/lang/; 英文翻译文件的完整示例为 resources/assets/tutorial/lang/en_us.json.
Adding a translation
创建lang文件后,您可以使用此基本模板添加翻译:
- resources/assets/tutorial/lang/en_us.json
{ "item.tutorial.my_item": "My Item", "item.tutorial.my_awesome.item": "My Awesome Item", [...] }
其中第一个字符串是任何可翻译的字符串(例如项目名称或TranslatableText)。 如果您按照Wiki教程进行操作,请记住将modid更改为`tutorial`或您选择的任何modid。
Using custom translatable text
Whenever a function accepts Text
, you have the option of giving it a new LiteralText()
,
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 TranslatableText()
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 itemStack, World world, List<Text> tooltip, TooltipContext tooltipContext) { tooltip.add(new TranslatableText("item.tutorial.fabric_item.tooltip")); }
And then add in the lang file:
- resources/assets/tutorial/lang/en_us.json
{ "item.tutorial.fabric_item.tooltip": "My Tooltip" }
And the tooltip will be displayed as “My Tooltip” !
Adding dynamic values to translatable text
Say you want the text to change based on some variable, like the current day and month. For a dynamic number, we put a %d where you want the number to show in the lang entry value, for example:
- resources/assets/tutorial/lang/en_us.json
{ "item.tutorial.fabric_item.tooltip": "My Tooltip in day %d, and month %d" }
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; tooltip.add(new TranslatableText("item.tutorial.fabric_item.tooltip", currentDay, currentMonth));
And the tooltip will be displayed as “My Tooltip in day 4, and month 7”.
In order to pass a string, we use %s
instead of %d
. If you want for it to literally show %
, use %%
.
For more information, see Java String.format (it works the same way).
Adding a new line
Making \n
work was far too difficult for Mojang, so in order to have a string with multiple lines you must split the translation key into multiple keys:
- resources/assets/tutorial/lang/en_us.json
{ "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 TranslatableText
parts individually:
tooltip.add(new TranslatableText("item.tutorial.fabric_item.tooltip_1")); tooltip.add(new TranslatableText("item.tutorial.fabric_item.tooltip_2"));
And the tooltip will be displayed as:
Line 1 of my tooltip Line 2 of my tooltip
Translation format
The translation key for objects you have registered is in the form
<object-type>.<modid>.<registry-id>
Object Type | Format | Example |
---|---|---|
Block | block.<modid>.<registry-id> | "block.tutorial.example_block": "Example Block" |
Item | item.<modid>.<registry-id> | "item.tutorial.my_item": "My Item" |
ItemGroup | itemGroup.<modid>.<registry-id> | "itemGroup.tutorial.my_group": "My Group" |
Fluid | fluid.<modid>.<registry-id> |
|
SoundEvent | sound_event.<modid>.<registry-id> |
|
StatusEffect | mob_effect.<modid>.<registry-id> |
|
Enchantment | enchantment.<modid>.<registry-id> |
|
EntityType | entity_type.<modid>.<registry-id> |
|
Potion | potion.<modid>.<registry-id> |
|
Biome | biome.<modid>.<registry-id> |
For types not in this list, see net.minecraft.util.registry.Registry.java
.