Grails how to change the current locale

2020-02-08 07:37发布

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 ?

6条回答
2楼-- · 2020-02-08 07:53

I had a similar issue, and it was because a space. I had:

[space]messages_de.properties instead messages_de.properties

查看更多
劫难
3楼-- · 2020-02-08 07:57

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")
查看更多
等我变得足够好
4楼-- · 2020-02-08 08:03

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.

查看更多
Animai°情兽
5楼-- · 2020-02-08 08:08

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.

查看更多
我命由我不由天
6楼-- · 2020-02-08 08:09

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.

查看更多
Anthone
7楼-- · 2020-02-08 08:16

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?.

查看更多
登录 后发表回答