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 [2021/02/11 07:13] – [Translation format] Stat type added arbeetutorial:lang [2024/08/25 14:35] (current) – [Adding a translation] solidblock
Line 1: Line 1:
-====== Name translations ======+====== Creating a lang file ======
 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. 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.
  
 ===== 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 TranslatableText). If you're following along in the wiki tutorial, remember to change modid to `tutorial`, or whatever modid you've chosen.+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.
  
 ===== Using custom translatable text ===== ===== Using custom translatable text =====
-Whenever a function accepts ''Text'', you have the option of giving it a ''new LiteralText()'', +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 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.  
-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: For example, when adding a tooltip, do:
-<code java>+<yarncode java>
 @Override @Override
-public void appendTooltip(ItemStack itemStackWorld world, List<Text> tooltip, TooltipContext tooltipContext) { +public void method_9568(ItemStack stackTooltipContext context, List<Text> tooltip, TooltipType type) { 
-    tooltip.add(new TranslatableText("item.tutorial.fabric_item.tooltip"));+    // 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"));
 } }
-</code>+</yarncode>
  
 And then add in the lang file: And then add in the lang file:
Line 37: Line 38:
 </code> </code>
  
-And the tooltip will be displayed as "My Tooltip" !+And the tooltip will be displayed as "My Tooltip"!
  
-==== Adding dynamic values to translatable text ==== +===== Adding dynamic values to translatable text ===== 
-Say you want the text to change based on some variable, like the current day and month.  +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:
-For a dynamic number, we put a %where you want the number to show in the lang entry value, for example:+
 <code JavaScript resources/assets/tutorial/lang/en_us.json> <code JavaScript resources/assets/tutorial/lang/en_us.json>
 { {
-  "item.tutorial.fabric_item.tooltip": "My Tooltip in day %d, and month %d+  "item.tutorial.fabric_item.tooltip": "My Tooltip in day %s, and month %s
 } }
 </code> </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: Then we pass the variables we use in our string by the order it appears in the text. First the day, then the month:
-<code java>+<yarncode java>
 int currentDay = 4; int currentDay = 4;
 int currentMonth = 7; int currentMonth = 7;
-tooltip.add(new TranslatableText("item.tutorial.fabric_item.tooltip", currentDay, currentMonth)); 
-</code> 
  
-And the tooltip will be displayed as "My Tooltip in day 4, and month 7".  +// 1.18.2 and before: 
-In order to pass a stringwe use ''%s'' instead of ''%d''. If you want for it to literally show ''%'', use ''%%''+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> 
 + 
 +In translations, the order of variables may change. Thereforeyou 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 [[https://dzone.com/articles/java-string-format-examples|Java String.format]] (it works the same way). For more information, see [[https://dzone.com/articles/java-string-format-examples|Java String.format]] (it works the same way).
  
 ==== Adding a new line ==== ==== Adding a new line ====
-Making ''\n'' work was far too difficult for Mojangso in order to have a string with multiple lines you must split the translation key into multiple keys:+The line-break ''\n'' work finely in command outputs. However in many cases, such as item tooltipsthey 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:
 <code JavaScript resources/assets/tutorial/lang/en_us.json> <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_1": "Line 1 of my tooltip"
   "item.tutorial.fabric_item.tooltip_2": "Line 2 of my tooltip"    "item.tutorial.fabric_item.tooltip_2": "Line 2 of my tooltip" 
 } }
 </code> </code>
-Then add the ''TranslatableText'' parts individually: +Then add the ''<yarn class_2588>'' parts individually: 
-<code java> +<yarncode java> 
-tooltip.add(new TranslatableText("item.tutorial.fabric_item.tooltip_1")); +// 1.18.2 and below: 
-tooltip.add(new TranslatableText("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
-</code> +</yarncode>
- +
-====== Translation format ====== +
-The translation key for objects you have registered is in the form +
-<code><object-type>.<modid>.<registry-id></code>  +
- +
-^ Object Type      ^ Format       ^ Example          ^ +
-| Block          | <code>block.<modid>.<registry-id></code>     |<code>"block.tutorial.example_block": "Example Block"  </code>       | +
-| Item    |<code> item.<modid>.<registry-id> </code> |<code> "item.tutorial.my_item": "My Item"</code>+
-| ItemGroup | <code> itemGroup.<modid>.<registry-id></code> | <code> "itemGroup.tutorial.my_group": "My Group"</code>+
-| Fluid | <code> fluid.<modid>.<registry-id> </code> || +
-| SoundEvent | <code> sound_event.<modid>.<registry-id> </code> || +
-| StatusEffect | <code> effect.<modid>.<registry-id> </code> || +
-| Enchantment | <code> enchantment.<modid>.<registry-id> </code> || +
-| EntityType | <code> entity.<modid>.<registry-id> </code> || +
-| Biome | <code> biome.<modid>.<registry-id> </code> || +
-| Stat | <code> stat.<modid>.<registry-id> </code> || +
- +
-For types not in this list, see ''net.minecraft.util.registry.Registry.java''+
- +
  
tutorial/lang.1613027610.txt.gz · Last modified: 2021/02/11 07:13 by arbee