Groovy

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]*$/))
                listOfCodes.add(it.key)
        }
        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.orange.label=Orange
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:

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

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

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

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

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

</g:form>

That’s it :)

  • You can also find the demo here.

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button