Michael Scharhag

About 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.

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.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

One Response to "Java: Moving conditions into Message files"

  1. josetesan says:

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

Leave a Reply

× 5 = ten

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books