Core Java

Java: Moving conditions into Message files

The Java classes ResourceBundle and MessageFormat provide a nice toolset for resolving localized messages inside Java applications. This post provides a small example on how you can move simple message related conditions from your Java code into message files using ChoiceFormat. If you already know ChoiceFormat I do not think you will learn anything new in this post. However, in my experience many developers do not know about this nice little feature.

Let’s assume we have an application in which users can comment some kinds of content. Somewhere in the application we want to display a simple message that shows how often a certain piece of content has been commented. We want to show the following messages based on the number of comments:

Number of commentsMessage
0This element contains no comments
1This element contains one comment
2+This element contains [numberOfComments] comments

To implement this feature using Java’s ResourceBundle and MessageFormat we could use the following code.

Message file (e.g. messages_en.properties):

comments.no=This element contains no comments
comments.one=This element contains one comment
comments.multiple=This element contains {0} comments

Java code:

private String resolveMessage(String key, Object... args) {
  String pattern = bundle.getString(key);
  return MessageFormat.format(pattern, args);
}

private String getMessage(int numberOfComments) {
  String message = null;
  if (numberOfComments == 0) {
    message = resolveMessage("comments.no");
  } else if (numberOfComments == 1) {
    message = resolveMessage("comments.one");
  } else {
    message = resolveMessage("comments.multiple", numberOfComments);
  }
  return message;
}

The method resolveMessage() is used to resolve a message key to an actual message using ResourceBundle and MessageFormat. To implement the requested feature we added three message keys to a properties file. Within getMessage() we implemented the logic to decide which message key should be used based on the passed numberOfComments variable.

The getMessage() method produces the expected result:

getMessage(0)   // "This element contains no comments"
getMessage(1)   // "This element contains one comment"
getMessage(2)   // "This element contains 2 comments"
getMessage(10)  // "This element contains 10 comments"

However, there is actually an easier way to do this. Actually we can move the complete logic implemented in getMessage() into the properties file.

We only need to define a single key:

comments.choice=This element contains {0,choice,0#no comments|1#one comment|1<{0} comments}

Using this message we can completely remove the logic of getMessage():

private String getMessageUsingChoice(int numberOfComments) {
  return resolveMessage("comments.choice", numberOfComments);
}

The result is exactly the same:

getMessageUsingChoice(0)   // "This element contains no comments"
getMessageUsingChoice(1)   // "This element contains one comment"
getMessageUsingChoice(2)   // "This element contains 2 comments"
getMessageUsingChoice(10)  // "This element contains 10 comments"

Let’s have a closer look at the defined message:

  • 0,choice – tells MessageFormat we want to apply a ChoiceFormat for the first parameter (0)
  • 0#no comments – means we want to use the message no comments if the first parameter is 0
  • 1#one comment – returns one comment if the first parameter is 1
  • 1<{0} comments – uses the sub pattern {0} comments if the first parameter is greater than 1

In conclusion choices provide a nice way to move simple message related conditions from Java code into message files.
 

Michael Scharhag

Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
josetesan
josetesan
10 years ago

Wow, didn’t know that.
Really like the way it works .. thanks a lot. :)

Back to top button