How can I change the current locale ?
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 ?
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 calledlang
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?.
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
.
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.
This is problably way too late but for reference, I do this in my index page controller:
session['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] = new Locale("es", "PR")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With