How can I change the current locale ?
- I tried to put
controller/action?lang=de
but my locale is stillen_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 ?
I had a similar issue, and it was because a space. I had:
[space]messages_de.properties
insteadmessages_de.properties
This is problably way too late but for reference, I do this in my index page controller:
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_SESSION_ATTRIBUTE_NAME
in http session. You can check it there.Correct solution is to map root url to some controller in UrlMappings.
As far as I understand the way you are checking the locale "request.locale" is wrong, it gives the locale of browser, not the locale of grails applciation.
You should use "LocaleContextHolder.locale".
In 2.0.3 it changes the locale by simply passing parameter
lang=someLocale
.I had a problem with this awhile back when proxy-ing through an older version of Apache2.2 and using the grails (2.3.9) war file. I've had better luck using mod_proxy_html (3.1) / Apache 2.4. Maybe more advanced versions of grails fix this.
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: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 yourresources.groovy
spring configuration file as shown below: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?.