views:

1871

answers:

2

Hi,

How can I change the current locale ?

  • I tried to put controller/action?lang=de but my locale is still en_US
  • I tried to override the value using this piece of code:

    def key = "org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER"
    def localeResolver = request.getAttribute(key)
    localeResolver.setLocale(request, response, new Locale("de","DE"))
    

Nothing changed.

  • I tried to override the value using this piece of code:

    import org.springframework.web.servlet.support.RequestContextUtils as RCU;
    RCU.getLocaleResolver(request).setLocale(request, response, new Locale("de","DE"))
    

And... nothing happened. i still got my locale set to en_US.

Any idea to change the locale ?

Thanks a lot!

+2  A: 

According to the chapter 10. Internationalization of the Grails documentation, Grails supports i18n out of the box and you should indeed be able to change the locale using the lang parameter:

By default the user locale is detected from the incoming Accept-Language header. However, you can provide users the capability to switch locales by simply passing a parameter called lang to Grails as a request parameter:

/book/list?lang=de

Grails will automatically switch the user locale and store it in a cookie so subsequent requests will have the new header.

But sometimes you may want to preset the default language because not all your applications will be in english. To do this, all you have to do is to set your localeResolver in your resources.groovy spring configuration file as shown below:

beans = {
   localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {
      defaultLocale = new Locale("de","DE")
      java.util.Locale.setDefault(defaultLocale)
   }
}

Now, without more details, I can't say why using the lang parameter isn't working in your case. Just in case, how do you know that the locale is still en_US?.

Pascal Thivent
Thanks for your response. I tried your solution to change the defaultLocale but I still get an en_US locale. Good question to retrieve my current locale I use 'request.locale'. I can also see that grails is still reading the default bundle message and not the 'de' version. Thanks for any clues!
Wickramben
Just wanted to be sure your validation process was ok and it is :) But I must say that this is really weird. Can you say if the `Accept-Language` header is set? What version of grails are you using?
Pascal Thivent
Yes weird is definitely the word... I'm using Grails 1.1. I've just check the header of the http request and I can see : 'Accept-Language: en-us,en;q=0.5'
Wickramben
That's not the expected behavior. However, I couldn't find anything close to your problem in Grails's Jira. This can't be missed though if it's a bug. I don't get it.
Pascal Thivent
+1  A: 

Hello,

Do you try to change locale in application root url (eg. http://localhost:8080/myapp/?lang=de)?

In Grails basic setup trying to change locale in application root url does not work. Grails change locale in localChangeInterceptor which is called before all controllers are called. When you access application root url, no controller is called as can be seen in default UrlMappings.

That's why changing locale in application root url does not work. If you try to change url in some controller, it works.

Current locale is stored under key org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE in http session. You can check it there.

Correct solution is to map root url to some controller in UrlMappings.

Ondra Zahradnik
Thanks for your answer. But yes I was trying to change the locale on one of my controller and it didn't work. That was really weird because the documentation was describing the same behavior as what you are telling but my locale wasn’t changing… so I build my own internationalization feature using a session variable to store the current locale.
Wickramben