Fetching List of message codes from

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, 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().


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 Grape Apple Apple Square
square.yellow.label=yellow Square 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.

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

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

Inline Feedbacks
View all comments
Back to top button