SurfMessageBundle

class SurfMessageBundle @JvmOverloads constructor(val bundleClazz: Class<*>, val pathToBundle: @NonNls String, val dataFolder: Path, val classLoader: ClassLoader = bundleClazz.classLoader)

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

bundleClazz

The class used for locating the resource bundles.

pathToBundle

The relative path to the bundle files, excluding the file extension.

dataFolder

The directory used for storing and managing resource bundles.

classLoader

The class loader used to load bundled resources. Defaults to the class loader of bundleClazz.

Constructors

Link copied to clipboard
constructor(bundleClazz: Class<*>, pathToBundle: @NonNls String, dataFolder: Path, classLoader: ClassLoader = bundleClazz.classLoader)

Creates a new instance of SurfMessageBundle.

Properties

Link copied to clipboard

The class used to locate the resource bundles. Typically, this is the class where the resource files are packaged or loaded.

Link copied to clipboard

The class loader used to load resource bundles from the classpath. Defaults to the class loader of bundleClazz.

Link copied to clipboard

The directory where the plugin stores its data, including resource bundles. This is used to store and manage localized message files.

Link copied to clipboard
val pathToBundle: @NonNls String

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.

Functions

Link copied to clipboard
operator fun get(key: String, vararg params: Component): @NotNull TranslatableComponent

Operator function for retrieving a translatable message as a TranslatableComponent.

Link copied to clipboard
fun getMessage(key: String, vararg params: Component): @NotNull TranslatableComponent

Retrieves a translatable message as a TranslatableComponent.

Link copied to clipboard
fun lazyMessage(key: String, vararg params: Component): Supplier<@NotNull TranslatableComponent>

Retrieves a lazily-evaluated message supplier as a TranslatableComponent.

Link copied to clipboard
fun load()

Loads and updates resource bundles from both the classpath and the data folder. This method ensures that missing resource bundles and keys are added to the data folder, and all bundles are registered with the global translator.