Class Messages
Applications register named bundles at startup, then look up localized strings
using simple static methods. The current locale is obtained from OorianLocale.get(),
so messages are automatically resolved in the user's current locale.
Setup (in Application.initialize()):
Messages.registerBundle("app", "com.mycompany.AppMessages");
Messages.setDefaultBundle("app");
Property files follow standard ResourceBundle conventions:
AppMessages.properties— default (English)AppMessages_de.properties— GermanAppMessages_fr.properties— FrenchAppMessages_ja.properties— Japanese
Usage:
// From the default bundle
String greeting = Messages.get("greeting");
String welcome = Messages.get("welcome.user", "name", "John");
// From a specific named bundle
String error = Messages.getFrom("errors", "not.found", "item", "User");
Message templates use {name} placeholders for parameter substitution,
with parameters passed as alternating key-value pairs.
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidclear()Removes all registered bundles and clears the default bundle setting.static StringResolves a message from the default bundle using the current locale.static StringReturns the name of the default bundle, ornullif none has been set.static StringResolves a message from a named bundle using the current locale.static booleanChecks if the default bundle contains the given key for the current locale.static booleanChecks if a named bundle contains the given key for the current locale.static voidregisterBundle(String name, String baseName) Registers a named resource bundle.static voidsetDefaultBundle(String name) Sets the default bundle name forget(String, Object...)lookups.
-
Method Details
-
registerBundle
Registers a named resource bundle.The base name follows standard
ResourceBundleconventions. For example, a base name of"com.mycompany.AppMessages"loads fromAppMessages.propertiesin thecom/mycompany/package directory.- Parameters:
name- the bundle name used for lookups (e.g.,"app")baseName- the base name of the resource bundle- Throws:
IllegalArgumentException- if name or baseName is null
-
setDefaultBundle
Sets the default bundle name forget(String, Object...)lookups.- Parameters:
name- the bundle name (must have been registered viaregisterBundle(java.lang.String, java.lang.String))- Throws:
IllegalArgumentException- if name is null
-
getDefaultBundle
Returns the name of the default bundle, ornullif none has been set.- Returns:
- the default bundle name
-
get
Resolves a message from the default bundle using the current locale.Parameters are passed as alternating key-value pairs for
{name}substitution:Messages.get("welcome.user", "name", "John"); // Template: "Welcome, {name}!" → "Welcome, John!"- Parameters:
key- the message keyparams- alternating key-value pairs for parameter substitution- Returns:
- the resolved message, or the key itself if not found
- Throws:
IllegalStateException- if no default bundle has been set
-
getFrom
Resolves a message from a named bundle using the current locale.The locale is obtained from
OorianLocale.get(). If the key is not found in the bundle, the key itself is returned.- Parameters:
bundleName- the registered bundle namekey- the message keyparams- alternating key-value pairs for parameter substitution- Returns:
- the resolved message, or the key itself if not found
- Throws:
IllegalArgumentException- if the bundle name is not registered
-
hasKey
Checks if the default bundle contains the given key for the current locale.- Parameters:
key- the message key- Returns:
trueif the key exists in the default bundle- Throws:
IllegalStateException- if no default bundle has been set
-
hasKey
Checks if a named bundle contains the given key for the current locale.- Parameters:
bundleName- the registered bundle namekey- the message key- Returns:
trueif the key exists in the bundle- Throws:
IllegalArgumentException- if the bundle name is not registered
-
clear
public static void clear()Removes all registered bundles and clears the default bundle setting.Primarily intended for testing. Also clears the
ResourceBundlecache to ensure fresh lookups after re-registration.
-