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?