User Tools

Site Tools


tutorial:lang

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:lang [2022/04/13 06:23] – map2fabricyarn daomephstatutorial:lang [2024/02/06 10:26] (current) – minecraft wiki rawdiamondmc
Line 3: Line 3:
  
 ===== Creating a lang file ===== ===== Creating a lang file =====
-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 [[https://minecraft.gamepedia.com/Language|this link]]. 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__. +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 [[https://minecraft.wiki/w/Language#Languages|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__. 
  
 ===== Adding a translation ===== ===== Adding a translation =====
Line 17: Line 17:
  
 ===== Using custom translatable text ===== ===== Using custom translatable text =====
-Whenever a function accepts ''<yarn class_2561>'', you have the option of giving it a ''new <yarn class_2585>()'',+Whenever a function accepts ''<yarn class_2561>'', you have the option of giving it a ''new <yarn class_2585>()'' 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 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 that would make it difficult to translate that text to another language, should you wish to do that. This is why
-whenever a ''<yarn class_2561>'' object is needed, you should give it a ''new <yarn class_2588>()'' with a translation key,+whenever a ''<yarn class_2561>'' object is needed, you should give it a ''new <yarn class_2588>()'' or ''Text.translatable'' with a translation key,
 and then translate the key in the lang file.  and then translate the key in the lang file. 
 For example, when adding a tooltip, do: For example, when adding a tooltip, do:
Line 26: Line 26:
 @Override @Override
 public void method_9568(class_1799 itemStack, class_1937 world, List<class_2561> tooltip, class_1836 tooltipContext) { public void method_9568(class_1799 itemStack, class_1937 world, List<class_2561> tooltip, class_1836 tooltipContext) {
 +    // 1.18.2 and before
     tooltip.add(new class_2588("item.tutorial.fabric_item.tooltip"));     tooltip.add(new class_2588("item.tutorial.fabric_item.tooltip"));
 +    
 +    // 1.19 and later
 +    tooltip.add(Text.translatable("item.tutorial.fabric_item.tooltip"));
 } }
 </yarncode> </yarncode>
Line 51: Line 55:
 int currentDay = 4; int currentDay = 4;
 int currentMonth = 7; int currentMonth = 7;
-tooltip.add(new new class_2588("item.tutorial.fabric_item.tooltip", currentDay, currentMonth));+ 
 +// 1.18.2 and before: 
 +tooltip.add(new class_2588("item.tutorial.fabric_item.tooltip", currentDay, currentMonth)); 
 + 
 +// 1.19 and later: 
 +tooltip.add(Text.translatable("item.tutorial.fabric_item.tooltip", currentDay, currentMonth));
 </yarncode> </yarncode>
  
Line 68: Line 77:
 Then add the ''<yarn class_2588>'' parts individually: Then add the ''<yarn class_2588>'' parts individually:
 <yarncode java> <yarncode java>
-tooltip.add(new new class_2588("item.tutorial.fabric_item.tooltip_1")); +// 1.18.2 and below: 
-tooltip.add(new new class_2588("item.tutorial.fabric_item.tooltip_2")); +tooltip.add(new class_2588("item.tutorial.fabric_item.tooltip_1")); 
-</code>+tooltip.add(new class_2588("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")); 
 +</yarncode>
 And the tooltip will be displayed as: And the tooltip will be displayed as:
-<code>+ 
 +<yarncode>
 Line 1 of my tooltip Line 1 of my tooltip
 Line 2 of my tooltip Line 2 of my tooltip
tutorial/lang.1649830986.txt.gz · Last modified: 2022/04/13 06:23 by daomephsta