SurfMessageBundle
A class for managing and loading message bundles used for translations in a plugin environment.
This class provides functionality to:
Load resource bundles from both the classpath and the plugin's data folder.
Copy missing resource bundles from the classpath to the data folder.
Update resource bundles in the data folder with missing keys from the bundled resources.
Register all loaded bundles with the global Adventure translator.
Provide utilities to fetch messages in a translatable format using keys.
The SurfMessageBundle ensures that translations are updated and available for use across the application by leveraging the Adventure library's translation capabilities.
Optimal Usage Example
// Create an object wrapper for the message bundle
object MessageBundleExample {
// Define a constant for the bundle's base name
private const val BUNDLE = "messages.ExampleBundle"
private val bundle = SurfMessageBundle(javaClass, BUNDLE, plugin.dataPath).apply { load() }
// Retrieve a translatable message
fun getMessage(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Component) = bundle.getMessage(key, *params)
// Retrieve a lazily-evaluated translatable message
fun lazyMessage(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Component) = bundle.lazyMessage(key, *params)
}
// Example usage
fun main() {
val message = MessageBundleExample.getMessage("example.key")
println(message)
}
Basic Usage Example
// Define the path to the message bundle
private const val BUNDLE = "messages.ExampleBundle"
// Create an instance of SurfMessageBundle and load it
val bundle = SurfMessageBundle(javaClass, BUNDLE, plugin.dataPath).apply { load() }
// Retrieve a translatable message
val message = bundle.getMessage("example.key")
// Use the message in your application
println(message)
Parameters
The class used for locating the resource bundles.
The relative path to the bundle files, excluding the file extension.
The directory used for storing and managing resource bundles.
The class loader used to load bundled resources. Defaults to the class loader of bundleClazz.
Constructors
Creates a new instance of SurfMessageBundle.
Properties
The class used to locate the resource bundles. Typically, this is the class where the resource files are packaged or loaded.
The class loader used to load resource bundles from the classpath. Defaults to the class loader of bundleClazz.
The directory where the plugin stores its data, including resource bundles. This is used to store and manage localized message files.
The relative path to the base name of the resource bundle files, excluding the file extension. For example, if the bundle is located at messages/example.properties
, the path would be messages.example
.