Changing request Locale for i18n in grails application using Interceptor

Posted By : Shakil Pathan | 09-Jun-2015

Hi,

In this blog, I am going to explain you about how to change request parameters using Interceptor in your grails application.

In one of my grails projects I need to change the request locale because in grails we define the i18n files like messages_en_US.properties for language English and country US. But I was creating APIs for ios which gives the lang attribute in parameters like en-US, which creating problems in retreiving the locale messages. For avoiding this problem I found the solution that I am going to explain in this blog.

In grails, we use lang attribute in request parameters for defining the locale, like we use lang=fr_CA for language french and country Canada.

For changing the request locale we first need to define a spring bean class in resource.groovy like:

 beans = {
	localeChangeInterceptor(com.example.CustomLocaleChangeInterceptor) {
		paramName = "lang"
	}
}
 

In the above code we define the bean which override the preHandle method of class LocaleChangeInterceptor and changing the lang parameter of the request. Below is the code that I used in my project:

 package com.example

import groovy.transform.CompileStatic
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.beans.propertyeditors.LocaleEditor
import org.springframework.util.StringUtils
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor
import org.springframework.web.servlet.support.RequestContextUtils

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@CompileStatic
class CustomLocaleChangeInterceptor extends LocaleChangeInterceptor {

    String paramName = DEFAULT_PARAM_NAME

    void setParamName(String name) {
        paramName = name
        super.setParamName name
    }

    @Override
    boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        GrailsWebRequest webRequest = GrailsWebRequest.lookup(request)
        def params = webRequest.params

        def localeParam = params?.get(paramName)
        if (!localeParam) {
            return super.preHandle(request, response, handler)
        }

        try {
            // choose first if multiple specified
            if (localeParam.getClass().isArray()) {
                localeParam = ((Object[])localeParam)[0]
            }

            //If locale hyphenated, then change to underscore
//            if(localeParam.toString()?.contains('-')){
//                localeParam = StringUtils.replace(localeParam.toString(), "-", "_")
//            }
            localeParam = (localeParam.toString().substring(0, 2) == 'fr' ? "fr" : "en")
            def localeResolver = RequestContextUtils.getLocaleResolver(request)
            def localeEditor = new LocaleEditor()
            localeEditor.setAsText localeParam.toString()
            localeResolver?.setLocale request, response, (Locale)localeEditor.value
            return true
        }
        catch (Exception e) {
            println "Error intercepting locale change: ${e.message}"
            return true
        }
    }
}

 

In the above commented code I replaces the dash(-) with the underscore(_) but my application uses only two language, so I set the locale accordingly as you can see in the above code.

Hope it helps!

 

Thanks

 

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..