在Grails应用程序中检索活动区域设置

tho*_*ers 19 java grails locale

我知道我可以使用"lang"参数自动更改文档中描述的当前语言环境,但如何跟踪这些更改,例如更新存储在当前用户域对象中的语言?

request.locale不起作用,因为它不反映通过" ?lang=xx" 所做的更改

Jos*_*ore 36

在您的控制器中,您可以使用RequestContextUtils.获取区域设置.

import org.springframework.web.servlet.support.RequestContextUtils as RCU
Run Code Online (Sandbox Code Playgroud)

然后解决请求的区域设置:

RCU.getLocale(request)
Run Code Online (Sandbox Code Playgroud)


Lau*_*nen 12

Grails在内部使用Spring.您可以从Spring的RequestContextUtils获取当前语言环境:http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/support/RequestContextUtils.html#getLocale(javax.servlet). http.HttpServletRequest)

import org.springframework.web.servlet.support.RequestContextUtils

def locale = RequestContextUtils.getLocale(request)
Run Code Online (Sandbox Code Playgroud)

检查<g:message>标记源以获取更多信息:

http://grails.org/doc/latest/ref/Tags/message.html


Top*_*era 12

您也可以使用LocaleContextHolder不需要请求的参数

import org.springframework.context.i18n.LocaleContextHolder;

LocaleContextHolder.getLocale();
Run Code Online (Sandbox Code Playgroud)