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 [2019/07/24 11:47] fudgetutorial: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 14: Line 14:
 } }
 </code> </code>
-where the first string is any translatable string (such as an item name, or TranslatableTextComponent). If you're following along in the wiki tutorial, remember to change modid to `tutorial`, or whatever modid you've chosen.+where the first string is any translatable string (such as an item name, or ''<yarn class_2588>''). If you're following along in the wiki tutorial, remember to change modid to `tutorial`, or whatever modid you've chosen.
  
-====== Translation format ====== +===== Using custom translatable text ===== 
-The translation key for objects you have registered is in the form +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), 
-<code><object-type>.<modid>.<registry-id></code+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 ''<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.  
 +For example, when adding a tooltip, do: 
 +<yarncode java> 
 +@Override 
 +public void method_9568(class_1799 itemStack, class_1937 world, List<class_2561tooltip, class_1836 tooltipContext) { 
 +    // 1.18.2 and before 
 +    tooltip.add(new class_2588("item.tutorial.fabric_item.tooltip")); 
 +     
 +    // 1.19 and later 
 +    tooltip.add(Text.translatable("item.tutorial.fabric_item.tooltip")); 
 +
 +</yarncode>
  
-^ Object      ^ Format       ^ Example          ^ +And then add in the lang file: 
-| Block          | <code>block.<modid>.<registry-id></code>     |<code>"block.tutorial.example_block": "Example Block"  </code      | +<code JavaScript resources/assets/tutorial/lang/en_us.json
-| Item    |<code> item.<modid>.<registry-id> </code> |<code> "item.tutorial.my_item": "My Item"</code> |+
 +  "item.tutorial.fabric_item.tooltip": "My Tooltip" 
 +
 +</code>
  
 +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:
 +<code JavaScript resources/assets/tutorial/lang/en_us.json>
 +{
 +  "item.tutorial.fabric_item.tooltip": "My Tooltip in day %d, and month %d" 
 +}
 +</code>
 +Then we pass the variables we use in our string by the order it appears in the text. First the day, then the month:
 +<yarncode java>
 +int currentDay = 4;
 +int currentMonth = 7;
 +
 +// 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>
 +
 +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 [[https://dzone.com/articles/java-string-format-examples|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:
 +<code JavaScript 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" 
 +}
 +</code>
 +Then add the ''<yarn class_2588>'' parts individually:
 +<yarncode java>
 +// 1.18.2 and below:
 +tooltip.add(new class_2588("item.tutorial.fabric_item.tooltip_1"));
 +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:
 +
 +<yarncode>
 +Line 1 of my tooltip
 +Line 2 of my tooltip
 +</yarncode>
 +
 +====== Translation format ======
 +The translation key for objects you have registered is in the form
 +''<object-type>.<namespace>.<path>'' (namespace and path as defined by the registered ''<yarn class_2960>'').
  
 +^ Object Type      ^ Format       ^ Example          ^
 +| <yarn class_2248>          | ''block.<namespace>.<path>''     |''"block.tutorial.example_block": "Example Block"''       |
 +| <yarn class_1792>    |''item.<namespace>.<path>'' |''"item.tutorial.my_item": "My Item"'' |
 +| <yarn class_1761> | ''itemGroup.<namespace>.<path>'' | ''"itemGroup.tutorial.my_group": "My Group"''|
 +| <yarn class_3611> | ''fluid.<namespace>.<path>''||
 +| <yarn class_3414> | ''sound_event.<namespace>.<path>''||
 +| <yarn class_1291> | ''effect.<namespace>.<path>''||
 +| <yarn class_1887> | ''enchantment.<namespace>.<path>''||
 +| <yarn class_1299> | ''entity.<namespace>.<path>''||
 +| <yarn class_1959> | ''biome.<namespace>.<path>''||
 +| <yarn class_3445> | ''stat.<namespace>.<path>''||
  
 +For types not in this list, see ''<yarn net.minecraft.class_2378>''.
tutorial/lang.txt · Last modified: 2024/02/06 10:26 by rawdiamondmc