How can I force Grails to use only one language?

前端 未结 4 1229
梦如初夏
梦如初夏 2021-02-05 12:29

I want to make my Grails application support only one language, that I can define somewhere, completely ignoring the client\'s headers or the \"lang\" parameter. Is there any wa

4条回答
  •  青春惊慌失措
    2021-02-05 13:11

    Define a LocaleResolver bean in your config/spring/resources.groovy to set the default locale.

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

    This is useful if you don't have to deal with the lang parameter - otherwise it would get overridden. To even ignore the lang parameter value you can set the locale in a Filter upon each request:

    import org.springframework.web.servlet.support.RequestContextUtils as RCU
    ...
    def filters = {
        all(controller:'*', action:'*') {
    
            before = {
                def locale = new Locale("sv","SV")
                RCU.getLocaleResolver(request).setLocale(request, response, locale)                  
            }
    
        }
    }
    

    This approach seems a bit repetitive as Locale is re-set on every request. It would be more elegant to disable the browsers locale detection via an config option.

提交回复
热议问题