Localized Messages
The @Rest(messages) annotation identifies the location of the resource bundle for a @Rest-annotated class if it's different from the class name.
By default, the resource bundle name is assumed to match the class name.
For example, given the class MyClass.java
, the resource bundle is assumed to be MyClass.properties
.
This property allows you to override this setting to specify a different location such as MyMessages.properties
by
specifying a value of MyMessages
.
Resource bundles are searched using the following base name patterns:
{package}.{name}
{package}.i18n.{name}
{package}.nls.{name}
{package}.messages.{name}
This annotation is used to provide request-localized (based on Accept-Language
) messages for the following method:
Request-localized messages are also available by passing either of the following args into your Java method:
The value can be a relative path like nls/Messages
, indicating to look for the resource bundle
com.foo.sample.nls.Messages
if the resource class is in com.foo.sample
, or it can be an absolute path like
com.foo.sample.nls.Messages
.
#--------------------------------------------------------------------------------
# Contents of org/apache/foo/nls/MyMessages.properties
#--------------------------------------------------------------------------------
HelloMessage = Hello {0}!
// Contents of org/apache/foo/MyResource.java
@Rest(messages="nls/MyMessages")
public class MyResource {
@RestGet("/hello/{you}")
public Object helloYou(RestRequest req, Messages messages, @Path("name") String you) {
String msg;
// Get it from the RestRequest object.
msg = req.getMessage("HelloMessage", you);
// Or get it from the method parameter.
msg = messages.getString("HelloMessage", you);
// Or get the message in a locale different from the request.
msg = messages.forLocale(Locale.UK).getString("HelloMessage", you);
return msg;
}
}
When using shared resource bundles, keys can be prefixed by class names like so and still retrieve by simple key names:
#--------------------------------------------------------------------------------
# Contents of shared org/apache/foo/nls/MyMessages.properties
#--------------------------------------------------------------------------------
MyResource.HelloMessage = Hello {0}!
Messages are automatically inherited from super classes. If a string cannot be found in the bundle of the current class, it will be searched for up the class hierarchy.