This plugin is dependent on ProtocolLib
Having seen and worked with the various plugins available on the spigot platform, I've been dissatisfied with the support for custom messages, message variables, and most importantly seamless multi-language support.
So with a bit of research and elbow grease, I made this plugin.
Most plugins use yaml for their message configurations, but the system I designed with json out of personal preference and relative ease to parse.
But the design doesn't matter, as long as it's easy to understand.
LangUtils has three easily identifiable steps:
- Create your configurable message as json
- Register the file in LangUtils onEnable, or some other location beforehand
- Get the message and send it to your desired targets with optional variable replacements
Create your configurable message as json
The json structure for the message is:
{
"language_locale": [
"An array full of text ",
"which are concatenated before ",
"being sent to the player. "
{
"text": "It can also be expressed as an object ",
"color": "red"
},
{
"text": "with even more options.",
"color": "yellow",
"bold": true,
"hover event": {
"action": "SHOW_TEXT",
"value": [
"Such as ",
{
"text": "hover events",
"color": "blue"
}
]
}
}
]
}
where "language_locale" is a combination of two strings, normally in two characters long. For example, the language setting in-game "English (US)" is represented as "en_us," so the language of English and the locale of the united states. However, you do not need a new entry for every single locale of each language. If you have one English entry, it will apply it to all English locales, unless specifically overridden. Here's an example of a functional configurable message:
{
"en_us": [
{
"text": "english text",
"color": "blue"
}
],
"de_de": [
{
"text": "german text",
"color": "white",
"bold": true
}
]
}
Register the file in LangUtils onEnable, or some other location beforehand
Registering is as easy as
LangUtils. registerLanguageFile ( this , file ) ;
And that's it! However, I've included a few other options for ease of use.
LangUtils. registerLanguageFile ( this ) ;
This will go through and register all language files in your dataFolder/lang directory.
Additionally, you can even write a default with this method:
LangUtils. writeDefault ( this , file, localeMessage ) ;
which also registers the localeMessage, so no need to call the register method afterward!
Get the message and send it to your desired targets with optional variable replacements
To send a message, its as easy as getting the LocaleMessage object with
NamespacedKey namespacedKey = new NamespacedKey ( plugin, "file name without extension or path" ) ;
LangUtils. getMessage ( namespacedKey ) ;
From there, use one of LocalMessage's various "send" methods. Here's an example with a common variable replacement:
LangUtils. getMessage ( new NamespacedKey ( this , "test" ) ) . sendTo ( LocaleMessage. createArgumentBuilder ( )
. put ( "%player%" , ( ( Player ) sender ) . getDisplayName ( ) ) . asMap ( ) , ( Player ) sender ) ;
Using the LocaleMessage send methods is ideal when possible, as it will send the message to each player in their native language like magic!
Thanks for giving this a try!
Similar projects
Plugin →Let Me Despawn
Improves performance by tweaking mob despawn rules. Say bye to pesky unintentional persistent mobs.
25MSueta
JourneyMap
Real-time map used for mapping in-game or your browser as you explore.
14.7MSueta
Emotecraft
Create your own emotes in Minecraft.
7.5MSueta
Simple Tpa
A simple tpa plugin which sends requests
5.4MSueta
Common Network
Forge and Fabric Unified Networking Solution
3.9MSueta
Set Home
A lightweight plugin for players to set their homes.
3.8MSueta