locale « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ locale ’

Change user preferred locale using spring’s SessionLocaleResolver

Posted by on May 11th, 2012

Here we are going to talk about a scenario where there are some records in database which are specific to locale and they need to be displayed as per user’s current locale at number of places.Also user can change its preferred locale.

For this I preferred to set the user’s locale in the session object. There are two ways to do this :-

1. Add attribute “lang” to the request and it will be available to session automatically.

For example: whenever user clicks on the link, the language will change to English and the user will remain on the same page.


<g:link controller="${params.controller}" action="${params.action}" params="${params+[lang:'en']}">English</g:link>
/*Add different languages here*/

2.Also set default locale in the session in case, it is not set.

For that add following to any Filters :-


if (!session.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE') {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
localeResolver.setLocale(request, response, new Locale('en'));

}

This sets the locale in spring’s SessionLocaleResolver class. Session always refer for the locale from here only.

Now one can check the locale wherever session is available. For example in GSPs:


<g:if test="${session.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'.toString()=='en'}">

Note: This implementation is useful to set locale in session (for accessing many times). Otherwise locale is available directly from current request as given below:-

import org.springframework.web.servlet.support.RequestContextUtils as RCU
Locale locale=RCU.getLocale(request)

Hope this helped!
Nitesh Goel
[Intelligrape Software Pvt. Ltd.]

RequestContextUtils
Posted in Grails

Finding User’s Session Locale in GSP

Posted by on July 14th, 2010

There are situations where you might want to know the Locale of the user as set in the session in your GSP so that you can show text in a particular language/manner. Here the Spring framework’s SessionLocaleResolver can come to the rescue. In one of our recent Grails projects, we did the following in one of our GSPs to display the content accordingly.

Please note that our use-case was like that in which we have fields for different languages in our domain classes. For example:

class MyDomain {
String greetingEnglish
String greetingFrench
}

And now you have to conditionally display the object’s fields based on the user’s locale. If this were a static text, then the Grails in-built i18n support in the form of message bundles would be the ideal solution. But this is a different scenario.

So let’s start by writing the following in your GSP :

<g:set var="lang" value="${session.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'}"/>

The above statement returns the ISO Language Code from the session object which is a map. In Grails, the map values can be retrieved by the ‘.’ operator too by passing the keys. These codes are the lower-case, two-letter codes as defined by ISO-639. For example: ‘en’ for English, ‘fr’ for French and so on.

Once you know the locale, you can show the conditional content on your page. It could be something like this:

<g:if test="${lang.startsWith('en')}">
<h1> ${myDomainobject.greetingEnglish} </h1>
</g:if>
<g:elseif test="${lang.startsWith('fr')}">
<h1> ${myDomainobject.greetingFrench}</h1>
</g:elseif>
<g:else>
<h1> Default Greeting </h1>
</g:else>

Hope this will help !!!

- Abhishek Tejpaul

[Intelligrape Software Pvt. Ltd.]

Posted in Grails, HTML-UI-CSS