Message Library

| GitHub |

The Message Library (or generally Message Service, after the class), is a simple utility for loading configurable messages. Due to it’s use of Java’s ResourceBundle class, this makes it incredibly easy to support translations in your plugin.

This was the first library in TeslaLibs and added by Simon_Flash.

MessageService

The namesake MessageService [link] class is used to retrieve ResourceBundles [link] from a ClassLoader. For ease of access, static methods exist to create a MessageService using the loader for the plugin’s asset folder as well as a specified path in the server files.

MessageService messages = MessageService.of(container, "messages"); //asset folder

Files used by the class must be .properties files (being a ResourceBundle), such as messages.properties, and files for a specific local such as French would be messages_fr.properties (research ResourceBundles for more info). An example messages.properties file is shown below.

message.one=String value
message.two=With &4<arg>

To retrieve a message, use MessageService#get with the message key (such as message.one) and a Locale. The class uses an Caffeine LoadingCache to handle requests for new ResourceBundles as necessary. The value of the message (or the key if absent) is returned in a Message to handle arguments.

Message

The Message class is a utility class that works with a String message to apply arguments in the form <arg>. The class contains methods for applying these arguments as well as retrieving the raw String or converting it to a Text object.

Message message = messages.get("messages.two", Locales.DEFAULT);
message.arg("arg", "fire!"); // message is now "With &4fire!"
Text text = message.toText();

Category: Developer Tools

Published on Mar 18, 2018

views

stars

watchers

total downloads

Promoted Versions

Members