【发布时间】:2011-11-06 21:12:08
【问题描述】:
我知道我可以使用“lang”参数自动更改当前语言环境,如docs 中所述,但是如何跟踪这些更改,例如更新存储在当前用户域对象中的语言?
request.locale 不起作用,因为它不反映通过“?lang=xx”所做的更改
【问题讨论】:
我知道我可以使用“lang”参数自动更改当前语言环境,如docs 中所述,但是如何跟踪这些更改,例如更新存储在当前用户域对象中的语言?
request.locale 不起作用,因为它不反映通过“?lang=xx”所做的更改
【问题讨论】:
在您的控制器中,您可以使用RequestContextUtils 获取语言环境。
import org.springframework.web.servlet.support.RequestContextUtils as RCU
然后解析请求的语言环境:
RCU.getLocale(request)
【讨论】:
您也可以使用不需要请求作为参数的LocaleContextHolder
import org.springframework.context.i18n.LocaleContextHolder;
LocaleContextHolder.getLocale();
【讨论】:
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)
查看
【讨论】:
如果你显示properties of RequestContextUtils.getLocale(request),你会发现如下:
ISO3Country=
ISO3Language=ara
displayCountry=
class=class java.util.Locale
default=fr_FR
language=ar
variant=
ISOLanguages=[Ljava.lang.String;@4269f8e3
availableLocales=[Ljava.util.Locale;@3b532125
displayName=arabe
ISOCountries=[Ljava.lang.String;@4ea52290
displayVariant=
displayLanguage=arabe
country=
【讨论】: