public abstract class LocaleContextHolder extends Object
inheritable flag is set to true.
Used as a central holder for the current Locale in Spring, wherever necessary: for example, in MessageSourceAccessor. DispatcherServlet automatically exposes its current Locale here. Other applications can expose theirs too, to make classes like MessageSourceAccessor automatically use that Locale.
LocaleContext,
MessageSourceAccessor,
DispatcherServlet| Constructor and Description |
|---|
LocaleContextHolder() |
| Modifier and Type | Method and Description |
|---|---|
static Locale |
getLocale()
Return the Locale associated with the current thread, if any,
or the system default Locale else.
|
static LocaleContext |
getLocaleContext()
Return the LocaleContext associated with the current thread, if any.
|
static TimeZone |
getTimeZone()
Return the TimeZone associated with the current thread, if any,
or the system default TimeZone else.
|
static void |
resetLocaleContext()
Reset the LocaleContext for the current thread.
|
static void |
setLocale(Locale locale)
Associate the given Locale with the current thread.
|
static void |
setLocale(Locale locale,
boolean inheritable)
Associate the given Locale with the current thread.
|
static void |
setLocaleContext(LocaleContext localeContext)
Associate the given LocaleContext with the current thread,
not exposing it as inheritable for child threads.
|
static void |
setLocaleContext(LocaleContext localeContext,
boolean inheritable)
Associate the given LocaleContext with the current thread.
|
public static void resetLocaleContext()
public static void setLocaleContext(LocaleContext localeContext)
The given LocaleContext may be a TimeZoneAwareLocaleContext,
containing a locale with associated time zone information.
localeContext - the current LocaleContextSimpleLocaleContext,
SimpleTimeZoneAwareLocaleContextpublic static void setLocaleContext(LocaleContext localeContext, boolean inheritable)
The given LocaleContext may be a TimeZoneAwareLocaleContext,
containing a locale with associated time zone information.
localeContext - the current LocaleContext,
or null to reset the thread-bound contextinheritable - whether to expose the LocaleContext as inheritable
for child threads (using an InheritableThreadLocal)SimpleLocaleContext,
SimpleTimeZoneAwareLocaleContextpublic static LocaleContext getLocaleContext()
null if nonepublic static void setLocale(Locale locale)
Will implicitly create a LocaleContext for the given Locale, not exposing it as inheritable for child threads.
locale - the current Locale, or null to reset
the thread-bound contextSimpleLocaleContext.SimpleLocaleContext(java.util.Locale)public static void setLocale(Locale locale, boolean inheritable)
Will implicitly create a LocaleContext for the given Locale.
locale - the current Locale, or null to reset
the thread-bound contextinheritable - whether to expose the LocaleContext as inheritable
for child threads (using an InheritableThreadLocal)SimpleLocaleContext.SimpleLocaleContext(java.util.Locale)public static Locale getLocale()
Locale.getDefault(),
able to optionally respect a user-level Locale setting.
Note: This method has a fallback to the system default Locale.
If you'd like to check for the raw LocaleContext content
(which may indicate no specific locale through null, use
getLocaleContext() and call LocaleContext.getLocale()
LocaleContext.getLocale(),
Locale.getDefault()public static TimeZone getTimeZone()
TimeZone.getDefault(),
able to optionally respect a user-level TimeZone setting.
Note: This method has a fallback to the system default Locale.
If you'd like to check for the raw LocaleContext content
(which may indicate no specific time zone through null, use
getLocaleContext() and call TimeZoneAwareLocaleContext.getTimeZone()
after downcasting to TimeZoneAwareLocaleContext.
TimeZoneAwareLocaleContext.getTimeZone(),
TimeZone.getDefault()