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

01
02
03
04
05
06
07
08
09
10
11
12
13
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:

1
2
3
4
5
6
7
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:

01
02
03
04
05
06
07
08
09
10
11
12
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:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
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:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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.
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 ....
I agree to the Terms and Privacy Policy

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