This is an old revision of the document!
Table of Contents
名字翻译
请注意,您的商品具有奇怪的显示名称,例如item.tutorial.my_item? 这是因为您的物品名称没有使用游戏选择的语言进行翻译。 翻译用于为单个字符串支持多种不同的语言。
创建一个lang文件
您可以使用lang文件为游戏中的可翻译字符串提供翻译。 您需要使用适合您的语言的文件名来创建文件-要找到您的语言代码,请访问this link. 英语是en_us 简体中文是zh_cn 繁体中文是zh_tw 输入语言代码后, 在创建一个JSON文件 resources/assets/modid/lang/; 英文翻译文件的完整示例为 resources/assets/tutorial/lang/zh_cn.json.
添加翻译
创建lang文件后,您可以使用此基本模板添加翻译:
- resources/assets/tutorial/lang/zh_cn.json
{ "item.tutorial.my_item": "我的物品", "item.tutorial.my_awesome.item": "我的物品真棒", [...] }
其中第一个字符串是任何可翻译的字符串(例如项目名称或TranslatableText)。 如果您按照Wiki教程进行操作,请记住将modid更改为`tutorial`或您选择的任何modid。
使用自定义可翻译文本
每当函数接受文本
时,您可以选择为其赋予new LiteralText()
,
这意味着我的世界将按原样使用构造函数参数中的字符串。 但是,这是不可取的,因为
如果您希望这样做,将很难将文本翻译成另一种语言。 这就是为什么
每当需要文本
对象时,都应给它一个带有翻译键的new TranslatableText()
,
然后在lang文件中翻译密钥。
例如,添加工具提示时,请执行以下操作:
@Override public void appendTooltip(ItemStack itemStack, World world, List<Text> tooltip, TooltipContext tooltipContext) { tooltip.add(new TranslatableText("item.tutorial.fabric_item.tooltip")); }
然后添加lang文件:
- resources/assets/tutorial/lang/zh_cn.json
{ "item.tutorial.fabric_item.tooltip": "我的工具提示" }
tooltip将显示为“我的工具提示” !
向可翻译文本添加动态值
假设您希望文本根据某些变量(例如当前日期和月份)进行更改。 对于动态数字,我们在您希望数字显示在lang条目值中的位置放一个%d,例如:
- resources/assets/tutorial/lang/zh_cn.json
{ "item.tutorial.fabric_item.tooltip": "我在第%d天和第%d天的工具提示" }
然后,我们按照字符串在文本中出现的顺序传递在字符串中使用的变量。 首先是天,然后是月:
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).
添加新行
对于Mojang来说,使\ n
工作太困难了,因此,要使字符串具有多行,必须将翻译键拆分为多个键:
- resources/assets/tutorial/lang/zh_cn.json
{ "item.tutorial.fabric_item.tooltip_1": "第一行我的工具提示" "item.tutorial.fabric_item.tooltip_2": "第二行我的工具提示" }
然后分别添加TranslatableText
部分:
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:
第一行我的工具提示 第二行我的工具提示
翻译格式
您注册的对象的翻译key的格式为
<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
.