Home » JVM Languages » Groovy » Fetching List of message codes from message.properties

About Neha Gupta

Neha has more than 3 years of experience working technologies like J2EE, Core Java ,Groovy and Grails. She is highly ambitious person, who believes in striving hard to achieve something great in life which alligns well with her philosphy of “hard work conquers all". She has an impetuous for her future goals and has an optimistic outlook towards life. You can check her more blogs here.

Fetching List of message codes from message.properties

Normally messages from message properties are fetched via, key i.e. message code, What if we want to select more than one message property, like a list. To get a list of select message codes from message.properties, we need to customize messageSource bean. To do that, lets create a class ‘CustomisedPluginAwareResourceBundleMessageSource’ which should extend class ‘PluginAwareResourceBundleMessageSource’.

To fetch all properties we will use getMergedProperties().

import org.codehaus.groovy.grails.context.support.PluginAwareResourceBundleMessageSource

class CustomisedPluginAwareResourceBundleMessageSource extends PluginAwareResourceBundleMessageSource {
    List listMessageCodes(Locale locale, String lookupMessageCode) {
        Properties properties = getMergedProperties(locale).properties
        List listOfCodes = []
        properties.each {
            if (it.key.toString().matches(/^[\w.]*${lookupMessageCode}[.\w]*$/))
        return listOfCodes

Here, ‘listMessageCodes()’ takes two parameters, first is the ‘locale’ we are looking in and second is ‘string’ which we are searching for ; and it returns the list of codes which contains that string.

Next we need to do is re-define ‘messageSource’ bean in the resources file:

import com.ig.demoApp.CustomisedPluginAwareResourceBundleMessageSource

beans = {
    messageSource(CustomisedPluginAwareResourceBundleMessageSource)  {
        basenames = "WEB-INF/grails-app/i18n/messages"

That’s it!

All we need to do is call the method ‘listMessageCodes()’. Here is a small example.

Mentioned below is a sample of message codes, in message.properties:

fruit.black.label=Black Grape
fruit.red.label=Red Apple
fruit.green.label=Green Apple

square.black.label=Black Square
square.yellow.label=yellow Square
square.red.label=Pink Square

circle.violet.label=Violet Circle
circle.magenta.label=Magenta Circle
circle.olive.label=Olive Circle

and a controller like:

package demoapp

class DemoController {

    def messageSource

    def show() {
        [fruits: messageSource.listMessageCodes(request.locale, "fruit"),
        squares: messageSource.listMessageCodes(request.locale, "square"),
        circles: messageSource.listMessageCodes(request.locale, "circle"),
        blackColorItems:messageSource.listMessageCodes(request.locale, "black"),
        redColorItems:messageSource.listMessageCodes(request.locale, "red")]

a gsp:

    <p>Available Fruits</p>
    <g:each in="${fruits}" var="fruit">
            <input type="radio" name="fruit">
            <label><g:message code="${fruit}"/></label>

    <p>Available Squares</p>
    <g:each in="${squares}" var="square">
            <input type="radio" name="square">
            <label><g:message code="${square}"/></label>

    <p>Available Circles</p>
    <g:each in="${circles}" var="circle">
            <input type="radio" name="circle">
            <label><g:message code="${circle}"/></label>

    <p>Available Black Color Items</p>
    <g:each in="${blackColorItems}" var="blackColorItem">
            <input type="radio" name="blackColorItem">
            <label><g:message code="${blackColorItem}"/></label>

    <p>Available Red Color Items</p>
    <g:each in="${redColorItems}" var="redColorItem">
            <input type="radio" name="redColorItem">
            <label><g:message code="${redColorItem}"/></label>


That’s it :)

  • You can also find the demo here.

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 our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

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

Inline Feedbacks
View all comments