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.
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

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


− 6 = zero



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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close