mscharhag, Programming and Stuff;

A blog about programming and software development topics, mostly focused on Java technologies including Java EE, Spring and Grails.

Saturday, 7 December, 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

The standard approach

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

Message file (e.g. element contains no comments 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("");
  } else if (numberOfComments == 1) {
    message = resolveMessage("");
  } 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"

Using ChoiceFormat

However, there is actually an easier way to do this using ChoiceFormat. 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.


  • Mikhail Batcer - Tuesday, 8 September, 2015

    What you described has nothing to do with ChoiceFormat, which is about including all forms in _one_ localized string.

  • Michael Scharhag - Tuesday, 8 September, 2015

    Hi Mikhail,
    I am not sure if you have read the post correctly but I think

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

    is _one_ localized string.

Leave a reply