java 如何在 Spring 中获取当前的语言环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31459103/
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
How to get current Locale in Spring?
提问by JohnWinter
I use this method
我用这个方法
LocaleContextHolder.getLocale()
LocaleContextHolder.getLocale()
to get switched locale (Japanise) but it returns me English (default). How can I retrieve the jp_JP locale?
切换语言环境(日语),但它返回英语(默认)。如何检索 jp_JP 语言环境?
回答by Mohammad tanvirul islam
//Return the Locale associated with the current thread,
// if any, or the system default Locale else(English)
LocaleContextHolder.getLocale();
so first you check is your current thread.for set you locale in current thread use :
所以首先你检查是你当前的线程。在当前线程使用中设置你的语言环境:
setLocale(Locale locale);
method and then LocaleContextHolder.getLocale() will return jp_JP locale
方法然后 LocaleContextHolder.getLocale() 将返回 jp_JP 语言环境
回答by DominikAngerer
RequestContextUtils
请求上下文实用程序
This should allow you to get the current locale of your request:
这应该允许您获取请求的当前语言环境:
RequestContextUtils.getLocaleResolver(request).resolveLocale(request);
Return the LocaleResolver
that has been bound to the request by the DispatcherServlet
.
@param request
current HTTP request
@return the current LocaleResolver
, or {@code null}
if not found:
返回LocaleResolver
已绑定到请求的DispatcherServlet
。
@paramrequest
当前 HTTP 请求
@return 当前的LocaleResolver
,或者{@code null}
如果没有找到:
public static LocaleResolver getLocaleResolver(HttpServletRequest request) {
return (LocaleResolver) request.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);
}
This will return a LocaleResolver from where you than can load the locale.
这将返回一个 LocaleResolver,您可以从中加载语言环境。
LocaleContextHolder
LocaleContextHolder
Or as metioned by: Mohammad tanvirul islam:
或者正如所提到的:Mohammad tanvirul islam:
LocaleContextHolder.getLocale();
You can have a look at the docu here:
您可以在此处查看文档:
RequestContextUtils: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/support/RequestContextUtils.html
LocaleResolver: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/LocaleResolver.html
LocaleContextHolder: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/i18n/LocaleContextHolder.html
RequestContextUtils:http: //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/support/RequestContextUtils.html
LocaleResolver:http: //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/LocaleResolver.html
LocaleContextHolder: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/i18n/LocaleContextHolder.html
回答by eightechess
There are several ways to create a Locale object.To get the current Local object.
有多种方法可以创建 Locale 对象。获取当前的 Locale 对象。
Locale locale = LocaleContextHolder.getLocale();
or
或者
Locale locale;
to get the current language
获取当前语言
Locale locale;
locale.getLanguage()
Locale locales = LocaleContextHolder.getLocale();
locales.getLanguage();