X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2FLocaleUtils.java;h=46b70d75677f974f0bdff353a74d8db20f44eb7d;hb=9e220ea5f945806f9fd8ef9f60ac7cf7b4b1edd1;hp=24ca3558c7dcc57ae657cd445b2de509669de6ce;hpb=bc03da7975e7c493aa839aa328fb76e0db4f4581;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..46b70d756 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.isDebugEnabled()) + log.debug("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();