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.
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.
| Method from org.springframework.context.i18n.LocaleContextHolder Detail: |
public static Locale getLocale() {
LocaleContext localeContext = getLocaleContext();
return (localeContext != null ? localeContext.getLocale() : Locale.getDefault());
}
Return the Locale associated with the current thread, if any,
or the system default Locale else. |
public static LocaleContext getLocaleContext() {
LocaleContext localeContext = (LocaleContext) localeContextHolder.get();
if (localeContext == null) {
localeContext = (LocaleContext) inheritableLocaleContextHolder.get();
}
return localeContext;
}
Return the LocaleContext associated with the current thread, if any. |
public static void resetLocaleContext() {
localeContextHolder.set(null);
inheritableLocaleContextHolder.set(null);
}
Reset the LocaleContext for the current thread. |
public static void setLocale(Locale locale) {
setLocale(locale, false);
}
Associate the given Locale with the current thread.
Will implicitly create a LocaleContext for the given Locale,
not exposing it as inheritable for child threads. |
public static void setLocale(Locale locale,
boolean inheritable) {
LocaleContext localeContext = (locale != null ? new SimpleLocaleContext(locale) : null);
setLocaleContext(localeContext, inheritable);
}
|
public static void setLocaleContext(LocaleContext localeContext) {
setLocaleContext(localeContext, false);
}
Associate the given LocaleContext with the current thread,
not exposing it as inheritable for child threads. |
public static void setLocaleContext(LocaleContext localeContext,
boolean inheritable) {
if (inheritable) {
inheritableLocaleContextHolder.set(localeContext);
localeContextHolder.set(null);
}
else {
localeContextHolder.set(localeContext);
inheritableLocaleContextHolder.set(null);
}
}
Associate the given LocaleContext with the current thread. |