Saturday, December 7, 2013

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 about 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 comments Message
0 This element contains no comments
1 This 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 = resourceBundle.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. 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:
This element contains {0,choice,0#no comments|1#one comment|1<{0} comments}
  • 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.


Share this post using Facebook, Twitter or Google+

No comments:

Post a Comment