X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2FLocaleUtils.java;h=5ddb1dab1c01e0657e90eea1e211d2ad22bae517;hb=3c7803ca05e2b0276d635e64046d924d3f1884c9;hp=24ca3558c7dcc57ae657cd445b2de509669de6ce;hpb=ba1fb3698ab4e033b67b3f38c7452ed72c1d108f;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/LocaleUtils.java b/org.argeo.cms/src/org/argeo/cms/LocaleUtils.java index 24ca3558c..5ddb1dab1 100644 --- a/org.argeo.cms/src/org/argeo/cms/LocaleUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/LocaleUtils.java @@ -8,10 +8,20 @@ import java.util.ResourceBundle; import javax.security.auth.Subject; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.cms.auth.CurrentUser; /** Utilities simplifying the development of localization enums. */ public class LocaleUtils { + private final static Log log = LogFactory.getLog(LocaleUtils.class); + + private final static ThreadLocal threadLocale = new ThreadLocal<>(); + + public static void setThreadLocale(Locale locale) { + threadLocale.set(locale); + } + public static String local(Enum en) { return local(en, getCurrentLocale(), "/OSGI-INF/l10n/bundle"); } @@ -64,10 +74,18 @@ public class LocaleUtils { } static Locale getCurrentLocale() { + Locale currentLocale = null; if (Subject.getSubject(AccessController.getContext()) != null) - return CurrentUser.locale(); - else - return Locale.getDefault(); + currentLocale = CurrentUser.locale(); + else if (threadLocale.get() != null) { + currentLocale = threadLocale.get(); + } + if (log.isTraceEnabled()) + log.trace("Thread #" + Thread.currentThread().getId() + " " + Thread.currentThread().getName() + " locale: " + + currentLocale); + if (currentLocale == null) + throw new IllegalStateException("No locale found"); + return currentLocale; // return UiContext.getLocale(); // FIXME look into Subject or settings // return Locale.getDefault();