Separating messages from code

How to properly separate messages from code? For example, for the opportunity to translate the plugin into other languages. An analogue of the messages.yml file in Bukkit/Spigot.

Just my approach: A config serializable class dedicated to messages

Which results in these: claim-blocks/translations at master Ā· randombyte-developer/claim-blocks Ā· GitHub

There, I used some kind of custom serializer for the TextTemplates, which is very broken apparently. It works for simple texts though. For recent projects I switched to simple String.replace(...) and then deserializing the String to a Text.

Of course the default config can just be shipped in the jar as a *.conf file instead of setting the defaults in code.

Thereā€™s no ā€˜properā€™ way to fully set this up, and Spongeā€™s TextTemplate class leaves a lot to be desired. This should be getting a refactor come API 8, but unfortunately thereā€™s no native system in Sponge that will do what you want in API 7.

To throw my hat in the ring, you can use the Message Library in TeslaLibs to handle both configurable messages and translations easily. This uses Javaā€™s ResourceBundle class to load messages, so on the Java side Iā€™d say this counts as the proper way if one exists. The MessageService class supports translations pulled from your pluginā€™s asset folder or a config folder on the server (as well as any other ClassLoader, if you desire), so itā€™s pretty simple to set up and doesnā€™t require you to declare a list of languages or anything like that.

If you have any questions with how to use this in your project, feel free to reach out to me on Discord and Iā€™d be happy to help. Good luck!

Good. The next version of the Sponge API will have built-in tools for this, right?

2 Likes