Class Messages

java.lang.Object
java.util.ResourceBundle
org.apache.juneau.cp.Messages

public class Messages extends ResourceBundle
An enhanced ResourceBundle.

Wraps a ResourceBundle to provide some useful additional functionality.

  • Instead of throwing MissingResourceException, the ResourceBundle.getString(String) method will return "{!key}" if the message could not be found.
  • Supported hierarchical lookup of resources from parent parent classes.
  • Support for easy retrieval of localized bundles.
  • Support for generalized resource bundles (e.g. properties files containing keys for several classes).

The following example shows the basic usage of this class for retrieving localized messages:

# Contents of MyClass.properties foo = foo {0} MyClass.bar = bar {0}

public class MyClass { private static final Messages MESSAGES = Messages.of(MyClass.class); public void doFoo() { // A normal property. String foo = MESSAGES.getString("foo","x"); // == "foo x" // A property prefixed by class name. String bar = MESSAGES.getString("bar","x"); // == "bar x" // A non-existent property. String baz = MESSAGES.getString("baz","x"); // == "{!baz}" } }

The ability to resolve keys prefixed by class name allows you to place all your messages in a single file such as a common "Messages.properties" file along with those for other classes.

The following shows how to retrieve messages from a common bundle:

public class MyClass { private static final Messages MESSAGES = Messages.of(MyClass.class, "Messages"); }

Resource bundles are searched using the following base name patterns:

  • "{package}.{name}"
  • "{package}.i18n.{name}"
  • "{package}.nls.{name}"
  • "{package}.messages.{name}"

These patterns can be customized using the Messages.Builder.baseNames(String...) method.

Localized messages can be retrieved in the following way:

// Return value from Japan locale bundle. String foo = MESSAGES.forLocale(Locale.JAPAN).getString("foo");