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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7245004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:15:34  来源:igfitidea点击:

Retrieve active locale in Grails application

javagrailslocale

提问by thomers

I know I can use the "lang" parameter to automatically change the current locale as described in the docs, but how do I track those changes, for example to update the language stored in the current user domain object?

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

request.localedoes not work, since it does not reflect the changes done via "?lang=xx"

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

回答by Joshua Moore

Within your controller you can obtain the locale using the RequestContextUtils.

在您的控制器中,您可以使用RequestContextUtils.

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

Then to resolve the locale for the request:

然后解析请求的语言环境:

RCU.getLocale(request)

回答by Lauri Piispanen

Grails uses Spring internally. You can get the current locale from Spring's RequestContextUtils: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/support/RequestContextUtils.html#getLocale(javax.servlet.http.HttpServletRequest)

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)

Check <g:message>tag source for more info:

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

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

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

回答by Topera

You can also use the LocaleContextHolderwhich doesn't need a request as a parameter

您还可以使用LocaleContextHolder不需要请求的参数作为参数

import org.springframework.context.i18n.LocaleContextHolder;

LocaleContextHolder.getLocale();

回答by Abdennour TOUMI

If you display propertiesof RequestContextUtils.getLocale(request), you will find the following :

如果显示propertiesRequestContextUtils.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=