Line 11: |
Line 11: |
| == StringTextComponent == | | == StringTextComponent == |
| String text components are the most basic form of text components. They are raw text components that contain a single String object. These are not preferred for general mod development as they cannot be translated into other languages. All properties of the base <code>TextComponent</code> apply to string text components. | | String text components are the most basic form of text components. They are raw text components that contain a single String object. These are not preferred for general mod development as they cannot be translated into other languages. All properties of the base <code>TextComponent</code> apply to string text components. |
| + | |
| + | === Creating === |
| + | String text components are instantiated in the form <code>new StringTextComponent("Hello, world!")</code>. In practice, when sent to a player, this would simply be displayed as declared: <code>Hello, world!</code>. |
| + | |
| + | === Usage === |
| + | String text components are most commonly used to create empty or space-filling text components, which can then link multiple text components together using `appendSibling`. |
| | | |
| == TranslationTextComponent == | | == TranslationTextComponent == |
Line 16: |
Line 22: |
| | | |
| === Format modifiers === | | === Format modifiers === |
− | Translation text components also support format modifiers like used in <code>String#format</code>. These format modifiers are declared by using the string <code>%s</code> when creating a translation entry for a given translation key. This format modifier will then be expanded and replaced with the provided object when rendered by the client. Any other format modifiers like <code>%d</code> or <code>%n</code> are ''not supported'' and will throw a formatting error. The list of arguments to expand these are passed to the constructor of the <code>TranslationTextComponent</code> using an Object varargs parameter, similar to <code>String#format</code>. Any objects that are not an instance of <code>ITextComponent</code> will have <code>Object#toString</code> called during expansion. | + | Translation text components also support format modifiers like used in <code>String#format</code>. These format modifiers are declared by using the string <code>%s</code> when creating a translation entry for a given translation key. This format modifier will then be expanded and replaced with the provided object when rendered by the client. Any other format modifiers like <code>%d</code> or <code>%n</code> are ''not supported'' and will throw a formatting error. To get a normal percentage, a double percentage sign must be used (<code>%%</code>). The list of arguments to expand these are passed to the constructor of the <code>TranslationTextComponent</code> using an Object varargs parameter, similar to <code>String#format</code>. Any objects that are not an instance of <code>ITextComponent</code> will have <code>Object#toString</code> called during expansion. |
| + | |
| + | === Creating === |
| + | Translation text components are instantiated in the form <code>new TranslationTextComponent("your.translation_key.here", "thing1", "thing2")</code>. In your lang file, the entry would look something like: <syntaxhighlight lang="json">{ |
| + | "your.translation_key.here": "I like using %s and %s!" |
| + | }</syntaxhighlight> |
| + | In practice, when sent to a player, this would be displayed as <code>I like using thing1 and thing2!</code>. Note that any more values must be declared as comma-separated key-value pairs as required by the JSON specification, with the last key-value pair not ending with a comment. |
| + | |
| + | === Usage === |
| + | Translation text components are used for sending anything visual to the player. Using them provides a benefit of format modifiers and the ability to be translated into different languages. This means that, given a translator, one could translate the English translation file (by default, <code>en_us.json</code>) into another language for a given mod. This system is also used by Minecraft itself and has been used to translate the game into hundreds of langauges. Translation keys are also required for declaring the names of items and blocks in the game. |