Change user preferred locale using spring’s SessionLocaleResolver

11 / May / 2012 by Nitesh 5 comments

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
FOUND THIS USEFUL? SHARE IT

comments (5)

  1. dyrektorzenia

    I believe that avoiding prepared foods would be the first step to be able to lose weight. They might taste fine, but packaged foods have got very little nutritional value, making you feed on more only to have enough strength to get with the day. If you’re constantly ingesting these foods, changing to grain and other complex carbohydrates will help you to have more energy while having less. Good blog post.

    Reply
  2. sierdzil

    I used to be suggested this web site through my cousin. I’m now not positive whether or not this submit is written through him as nobody else understand such detailed about my trouble. You’re wonderful! Thanks!

    Reply
  3. pierwotki

    Thanks for the interesting things you have exposed in your article. One thing I’d like to reply to is that FSBO human relationships are built eventually. By releasing yourself to owners the first weekend their FSBO will be announced, ahead of the masses get started calling on Monday, you make a good relationship. By giving them equipment, educational resources, free reports, and forms, you become a good ally. By subtracting a personal curiosity about them as well as their scenario, you produce a solid relationship that, in many cases, pays off when the owners opt with a realtor they know in addition to trust – preferably you actually.

    Reply
  4. swiecianke

    Thanks for the auspicious writeup. It in fact used to be a amusement account it. Glance complicated to more delivered agreeable from you! By the way, how could we keep up a correspondence?

    Reply

Leave a comment -