Showing posts with label messageSource. Show all posts
Showing posts with label messageSource. Show all posts

Sunday, August 2, 2015

Grails :: How To Override g Message Tag

1. Create a tab lib (grails-app/taglib)

2. Extend with ValidationTagLib

3. Example

package com.test.taglib

import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib

class MyCustomTagLib extends ValidationTagLib {
 static validationTagLibStatic = null

    static namespace = "g"

    Closure message = { attrs ->
        ValidationTagLib validationTagLib = validationTagLibStatic ?: (validationTagLibStatic = grailsAttributes.applicationContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib'))
        validationTagLib.message.call(attrs)
    }
}

Friday, July 31, 2015

Grails Add New Resources To MessageSource Dynamically


First create a groovy file with the following contents

package com.pkm.message.test

import grails.util.Environment
import javax.servlet.ServletContext

class MessageSourceCreator {
    public static def grailsApplication = null
    public static def servletContext = null
    public static def messageSource = null
    private static final String MESSAGE_FILE_DIR = "/WEB-INF/resources/messages"

    /**
     * @param fileName "some.message.properties"
     */
    public static void addNewMessageFile(String fileName) {
        String basePath = servletContext.getRealPath("/")
        fileName = fileName.substring(0, fileName.lastIndexOf("."))
        String relativeFilePath = "${MESSAGE_FILE_DIR}/${fileName}"

        if (Environment.isWarDeployed()) {
            messageSource.pluginBaseNames.add(0, relativeFilePath)
        } 
        else {
            String separator = Environment.current == Environment.TEST ? "/" : ""
            messageSource.pluginBaseNames.add(0, basePath + separator + relativeFilePath)
        }
    }

    public static void initialize(def ga, def sc, def ms) {
        grailsApplication = ga
        servletContext = sc
        messageSource = ms
    }
}



From BootStrap, need to call first initialize & then addNewMessageFile


/**
 * From BootStrap, need to call first initialize & then addNewMessageFile
 */
import com.pkm.message.test.MessageSourceCreator

class BootStrap {
    def grailsApplication
    def servletContext
    def messageSource

    def init = { servletContext ->
        MessageSourceCreator.initialize(grailsApplication, servletContext, messageSource)
        MessageSourceCreator.addNewMessageFile("some.message.properties")
    }

    def destroy = {

    }
}

Saturday, February 15, 2014

Accessing i18n Messages from exception (validation errors) in Grails in a Service

To access your message, inject the MessageSource into your service:
def messageSource  

And to get the error messages from exception based on your i18n properties file, use the following code block in your service:
 
def messageSource;

def printAllMessage(Exception ex) {
    ex.errors.allErrors.eachWithIndex { ObjectError objectError, Integer index ->
        println "Error #${index}: " + messageSource.getMessage(objectError, Locale.default);
    }
}


Will output something like this based on you i18n messages.properties file:

Error #0: Property [accountId] cannot be null
Error #1: Property [displayName] cannot be null
Error #2: Property [emailAddress] with value [pritom@bitmascot.com] must be unique

If your i18n file contains something like:

default.null.message=Property [{0}] cannot be null
default.not.unique.message=Property [{0}] with value [{2}] must be unique