org.springframework.context.i18n

## Class LocaleContextHolder

- java.lang.Object
- - org.springframework.context.i18n.LocaleContextHolder

-     public abstract class LocaleContextHolder
      extends Object

  Simple holder class that associates a LocaleContext instance with the current thread. The LocaleContext will be inherited by any child threads spawned by the current thread if the `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.

  Since:
    1.2
  Author:
    Juergen Hoeller, Nicholas Williams
  See Also:
    `LocaleContext`, `MessageSourceAccessor`, `DispatcherServlet`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `LocaleContextHolder()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    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, 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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### LocaleContextHolder

          public LocaleContextHolder()

  - ### Method Detail

    - #### resetLocaleContext

          public static void resetLocaleContext()

      Reset the LocaleContext for the current thread.

    - #### setLocaleContext

          public static void setLocaleContext(LocaleContext localeContext)

      Associate the given LocaleContext with the current thread, not exposing it as inheritable for child threads.

      The given LocaleContext may be a `TimeZoneAwareLocaleContext`, containing a locale with associated time zone information.

      Parameters:
        `localeContext` - the current LocaleContext, or `null` to reset the thread-bound context
      See Also:
        `SimpleLocaleContext`, `SimpleTimeZoneAwareLocaleContext`

    - #### setLocaleContext

          public static void setLocaleContext(LocaleContext localeContext,
                                              boolean inheritable)

      Associate the given LocaleContext with the current thread.

      The given LocaleContext may be a `TimeZoneAwareLocaleContext`, containing a locale with associated time zone information.

      Parameters:
        `localeContext` - the current LocaleContext, or `null` to reset the thread-bound context
        `inheritable` - whether to expose the LocaleContext as inheritable for child threads (using an `InheritableThreadLocal`)
      See Also:
        `SimpleLocaleContext`, `SimpleTimeZoneAwareLocaleContext`

    - #### getLocaleContext

          public static LocaleContext getLocaleContext()

      Return the LocaleContext associated with the current thread, if any.
      Returns:
        the current LocaleContext, or `null` if none

    - #### setLocale

          public static void setLocale(Locale locale)

      Associate the given Locale with the current thread, preserving any TimeZone that may have been set already.

      Will implicitly create a LocaleContext for the given Locale, not exposing it as inheritable for child threads.

      Parameters:
        `locale` - the current Locale, or `null` to reset the locale part of thread-bound context
      See Also:
        `setTimeZone(TimeZone)`, `SimpleLocaleContext.SimpleLocaleContext(Locale)`

    - #### setLocale

          public static void setLocale(Locale locale,
                                       boolean inheritable)

      Associate the given Locale with the current thread, preserving any TimeZone that may have been set already.

      Will implicitly create a LocaleContext for the given Locale.

      Parameters:
        `locale` - the current Locale, or `null` to reset the locale part of thread-bound context
        `inheritable` - whether to expose the LocaleContext as inheritable for child threads (using an `InheritableThreadLocal`)
      See Also:
        `setTimeZone(TimeZone, boolean)`, `SimpleLocaleContext.SimpleLocaleContext(Locale)`

    - #### getLocale

          public static Locale getLocale()

      Return the Locale associated with the current thread, if any, or the system default Locale else. This is effectively a replacement for `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()`

      Returns:
        the current Locale, or the system default Locale if no specific Locale has been associated with the current thread
      See Also:
        `LocaleContext.getLocale()`, `Locale.getDefault()`

    - #### setTimeZone

          public static void setTimeZone(TimeZone timeZone)

      Associate the given TimeZone with the current thread, preserving any Locale that may have been set already.

      Will implicitly create a LocaleContext for the given Locale, not exposing it as inheritable for child threads.

      Parameters:
        `timeZone` - the current TimeZone, or `null` to reset the time zone part of the thread-bound context
      See Also:
        `setLocale(Locale)`, `SimpleTimeZoneAwareLocaleContext.SimpleTimeZoneAwareLocaleContext(Locale, TimeZone)`

    - #### setTimeZone

          public static void setTimeZone(TimeZone timeZone,
                                         boolean inheritable)

      Associate the given TimeZone with the current thread, preserving any Locale that may have been set already.

      Will implicitly create a LocaleContext for the given Locale.

      Parameters:
        `timeZone` - the current TimeZone, or `null` to reset the time zone part of the thread-bound context
        `inheritable` - whether to expose the LocaleContext as inheritable for child threads (using an `InheritableThreadLocal`)
      See Also:
        `setLocale(Locale, boolean)`, `SimpleTimeZoneAwareLocaleContext.SimpleTimeZoneAwareLocaleContext(Locale, TimeZone)`

    - #### getTimeZone

          public static TimeZone getTimeZone()

      Return the TimeZone associated with the current thread, if any, or the system default TimeZone else. This is effectively a replacement for `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`.

      Returns:
        the current TimeZone, or the system default TimeZone if no specific TimeZone has been associated with the current thread
      See Also:
        `TimeZoneAwareLocaleContext.getTimeZone()`, `TimeZone.getDefault()`