问题描述
我想让我的Grails应用程序只支持一种语言,我可以在某处定义,完全忽略客户端的头文件或lang参数。有什么办法可以做到吗?感谢。
在<$ c $中定义一个 LocaleResolver
beans = {$ c> config / spring / resources.groovy 来设置默认语言环境。 b $ b的LocaleResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver){
defaultLocale =新的区域( 德, DE)
java.util.Locale.setDefault(defaultLocale)
$ b这非常有用,如果你不必处理 lang 参数 - 否则会被覆盖。甚至可以忽略 lang 参数值,您可以在每次请求时在 Filter 中设置区域设置:
import org.springframework.web.servlet.support.RequestContextUtils as RCU
...
def filters = {
全部(控制器:'*',动作:'*'){
之前= {
def locale = new Locale(sv,SV)
RCU.getLocaleResolver(request).setLocale(request,response,locale)
}
$ b $ <$ $ c $>这种方法似乎有点重复,因为每次请求都重置了Locale。通过配置选项禁用浏览器区域设置会更加优雅。
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 way I can do so? Thanks.
解决方案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.
这篇关于我如何强制Grails只使用一种语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!