Class Messages

java.lang.Object
com.oorian.i18n.Messages

public final class Messages extends Object
Central API for managing and resolving localized text from Java resource bundles.

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 — German
  • AppMessages_fr.properties — French
  • AppMessages_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 Details

    • registerBundle

      public static void registerBundle(String name, String baseName)
      Registers a named resource bundle.

      The base name follows standard ResourceBundle conventions. For example, a base name of "com.mycompany.AppMessages" loads from AppMessages.properties in the com/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

      public static void setDefaultBundle(String name)
      Sets the default bundle name for get(String, Object...) lookups.
      Parameters:
      name - the bundle name (must have been registered via registerBundle(java.lang.String, java.lang.String))
      Throws:
      IllegalArgumentException - if name is null
    • getDefaultBundle

      public static String getDefaultBundle()
      Returns the name of the default bundle, or null if none has been set.
      Returns:
      the default bundle name
    • get

      public static String get(String key, Object... params)
      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 key
      params - 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

      public static String getFrom(String bundleName, String key, Object... params)
      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 name
      key - the message key
      params - 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

      public static boolean hasKey(String key)
      Checks if the default bundle contains the given key for the current locale.
      Parameters:
      key - the message key
      Returns:
      true if the key exists in the default bundle
      Throws:
      IllegalStateException - if no default bundle has been set
    • hasKey

      public static boolean hasKey(String bundleName, String key)
      Checks if a named bundle contains the given key for the current locale.
      Parameters:
      bundleName - the registered bundle name
      key - the message key
      Returns:
      true if 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 ResourceBundle cache to ensure fresh lookups after re-registration.