1 16 17 package org.springframework.context.i18n; 18 19 import java.util.Locale ; 20 21 38 public abstract class LocaleContextHolder { 39 40 private static final ThreadLocal localeContextHolder = new ThreadLocal (); 41 42 private static final ThreadLocal inheritableLocaleContextHolder = new InheritableThreadLocal (); 43 44 45 48 public static void resetLocaleContext() { 49 localeContextHolder.set(null); 50 inheritableLocaleContextHolder.set(null); 51 } 52 53 59 public static void setLocaleContext(LocaleContext localeContext) { 60 setLocaleContext(localeContext, false); 61 } 62 63 70 public static void setLocaleContext(LocaleContext localeContext, boolean inheritable) { 71 if (inheritable) { 72 inheritableLocaleContextHolder.set(localeContext); 73 localeContextHolder.set(null); 74 } 75 else { 76 localeContextHolder.set(localeContext); 77 inheritableLocaleContextHolder.set(null); 78 } 79 } 80 81 85 public static LocaleContext getLocaleContext() { 86 LocaleContext localeContext = (LocaleContext) localeContextHolder.get(); 87 if (localeContext == null) { 88 localeContext = (LocaleContext) inheritableLocaleContextHolder.get(); 89 } 90 return localeContext; 91 } 92 93 101 public static void setLocale(Locale locale) { 102 setLocale(locale, false); 103 } 104 105 114 public static void setLocale(Locale locale, boolean inheritable) { 115 LocaleContext localeContext = (locale != null ? new SimpleLocaleContext(locale) : null); 116 setLocaleContext(localeContext, inheritable); 117 } 118 119 127 public static Locale getLocale() { 128 LocaleContext localeContext = getLocaleContext(); 129 return (localeContext != null ? localeContext.getLocale() : Locale.getDefault()); 130 } 131 132 } 133 | Popular Tags |