Bad manners when programming public plugins is the lack of the ability to translate the plugin into other languages. Indeed, Minecraft is perhaps one of the most popular games in the world and is played by people from all over the world. But what if the plugin has a lot of text? One hundred lines, or even a thousand? It all turns into a terrible routine. Write a big and long language file, read it every time you need to initialize a field.. I myself have come across this quite often.
With Minority, multi-language support is also extremely easy.
Example plugin
Let's write some simple plugin with the ability to translate it.
@Translatable @Configurable(file = "tutorial.yml", path = "tutorial-core")
public class PlayerTutorial implements MinorityFeature, Listener {
@TranslationKey(section = "messages", name = "join-message", value = "Welcome to our server! The official language of the server is English, but you can also communicate in your own language!")
private String joinMessage;
@TranslationKey(section = "messages", name = "tree-break-message", value = "On our server, it's considered good form to chop down a whole tree, not just logs! Break the leaves, plant a sapling in the place of the tree, make the world a better place!")
private String treeBreakMessage;
@ConfigurationKey(name = "show-message-chance", type = Type.DOUBLE, value = "0.15", comment = "The chance that a tutorial message will be sent to the player.")
private double chance;
public PlayerTutorial(final MinorityExtension plugin) {
plugin.getConfigurationWizard().generate(this.getClass());
this.init(this, this.getClass(), plugin);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
private void onPlayerJoin(final PlayerJoinEvent event) {
if (chance >= Math.random()) {
event.getPlayer().sendMessage(joinMessage);
}
}
@EventHandler
private void onTreeBreak(final BlockBreakEvent event) {
final Material type = event.getBlock().getType();
if (chance >= Math.random() && type.equals(Material.OAK_LOG)) {
event.getPlayer().sendMessage(treeBreakMessage);
}
}
}
The main thing to pay attention to is the @Translatable and @TranslationKey annotations. The mechanism of their work is practically no different from @Configurable and @ConfigurationKey (with which, by the way, they can be combined, as shown in the example).
The default language is, of course, English. The first time when our plugin is launched, the language key will be added to config.yml, and the languages folder with the en.yml file will be created in the plugin directory.
Result
config.yml
# This configuration file was automatically generated with Minority.
language: en
tutorial.yml
# This configuration file was automatically generated with Minority.
tutorial-core:
# The chance that a tutorial message will be sent to the player.
show-message-chance: '0.15'
en.yml
# This language file was automatically generated with Minority.
messages:
join-message: Welcome to our server! The official language of the server is English,
but you can also communicate in your own language!
tree-break-message: On our server, it's considered good form to chop down a whole
tree, not just logs! Break the leaves, plant a sapling in the place of the tree,
make the world a better place!