public final 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
Modifier and Type | Method and Description |
---|---|
static Locale |
getLocale()
Return the Locale associated with the current thread, if any,
or the system default Locale otherwise.
|
static Locale |
getLocale(LocaleContext localeContext)
Return the Locale associated with the given user context, if any,
or the system default Locale otherwise.
|
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 otherwise.
|
static TimeZone |
getTimeZone(LocaleContext localeContext)
Return the TimeZone associated with the given user context, if any,
or the system default TimeZone otherwise.
|
static void |
resetLocaleContext()
Reset the LocaleContext for the current thread.
|
static void |
setDefaultLocale(Locale locale)
Set a shared default locale at the framework level,
as an alternative to the JVM-wide default locale.
|
static void |
setDefaultTimeZone(TimeZone timeZone)
Set a shared default time zone at the framework level,
as an alternative to the JVM-wide default time zone.
|
static void |
setLocale(Locale locale)
Associate the given Locale with the current thread,
preserving any TimeZone that may have been set already.
|
static void |
setLocale(Locale locale,
boolean inheritable)
Associate the given Locale with the current thread,
preserving any TimeZone that may have been set already.
|
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.
|
static void |
setTimeZone(TimeZone timeZone)
Associate the given TimeZone with the current thread,
preserving any Locale that may have been set already.
|
static void |
setTimeZone(TimeZone timeZone,
boolean inheritable)
Associate the given TimeZone with the current thread,
preserving any Locale that may have been set already.
|
public static void resetLocaleContext()
public static void setLocaleContext(@Nullable LocaleContext localeContext)
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 contextSimpleLocaleContext
,
SimpleTimeZoneAwareLocaleContext
public static void setLocaleContext(@Nullable 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
,
SimpleTimeZoneAwareLocaleContext
@Nullable public static LocaleContext getLocaleContext()
null
if nonepublic static void setLocale(@Nullable 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 locale part of thread-bound contextsetTimeZone(TimeZone)
,
SimpleLocaleContext(Locale)
public static void setLocale(@Nullable Locale locale, boolean inheritable)
Will implicitly create a LocaleContext for the given Locale.
locale
- the current Locale, or null
to reset
the locale part of thread-bound contextinheritable
- whether to expose the LocaleContext as inheritable
for child threads (using an InheritableThreadLocal
)setTimeZone(TimeZone, boolean)
,
SimpleLocaleContext(Locale)
public static void setDefaultLocale(@Nullable Locale locale)
NOTE: This can be useful to set an application-level default locale which differs from the JVM-wide default locale. However, this requires each such application to operate against locally deployed Spring Framework jars. Do not deploy Spring as a shared library at the server level in such a scenario!
locale
- the default locale (or null
for none,
letting lookups fall back to Locale.getDefault()
)getLocale()
,
Locale.getDefault()
public static Locale getLocale()
Locale.getDefault()
,
able to optionally respect a user-level Locale setting.
Note: This method has a fallback to the shared default Locale,
either at the framework level or at the JVM-wide system level.
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()
getLocaleContext()
,
LocaleContext.getLocale()
,
setDefaultLocale(Locale)
,
Locale.getDefault()
public static Locale getLocale(@Nullable LocaleContext localeContext)
Locale.getDefault()
,
able to optionally respect a user-level Locale setting.localeContext
- the user-level locale context to checkgetLocale()
,
LocaleContext.getLocale()
,
setDefaultLocale(Locale)
,
Locale.getDefault()
public static void setTimeZone(@Nullable TimeZone timeZone)
Will implicitly create a LocaleContext for the given Locale, not exposing it as inheritable for child threads.
timeZone
- the current TimeZone, or null
to reset
the time zone part of the thread-bound contextsetLocale(Locale)
,
SimpleTimeZoneAwareLocaleContext(Locale, TimeZone)
public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable)
Will implicitly create a LocaleContext for the given Locale.
timeZone
- the current TimeZone, or null
to reset
the time zone part of the thread-bound contextinheritable
- whether to expose the LocaleContext as inheritable
for child threads (using an InheritableThreadLocal
)setLocale(Locale, boolean)
,
SimpleTimeZoneAwareLocaleContext(Locale, TimeZone)
public static void setDefaultTimeZone(@Nullable TimeZone timeZone)
NOTE: This can be useful to set an application-level default time zone which differs from the JVM-wide default time zone. However, this requires each such application to operate against locally deployed Spring Framework jars. Do not deploy Spring as a shared library at the server level in such a scenario!
timeZone
- the default time zone (or null
for none,
letting lookups fall back to TimeZone.getDefault()
)getTimeZone()
,
TimeZone.getDefault()
public static TimeZone getTimeZone()
TimeZone.getDefault()
,
able to optionally respect a user-level TimeZone setting.
Note: This method has a fallback to the shared default TimeZone,
either at the framework level or at the JVM-wide system level.
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
.
getLocaleContext()
,
TimeZoneAwareLocaleContext.getTimeZone()
,
setDefaultTimeZone(TimeZone)
,
TimeZone.getDefault()
public static TimeZone getTimeZone(@Nullable LocaleContext localeContext)
TimeZone.getDefault()
,
able to optionally respect a user-level TimeZone setting.localeContext
- the user-level locale context to checkgetTimeZone()
,
TimeZoneAwareLocaleContext.getTimeZone()
,
setDefaultTimeZone(TimeZone)
,
TimeZone.getDefault()