X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2FLocaleUtils.java;h=8aca8768a04d9e3af196ce9690ea449ba1d7d651;hb=0e533d2562def311fdd7aa71f1d0d704e466861e;hp=46b70d75677f974f0bdff353a74d8db20f44eb7d;hpb=9e220ea5f945806f9fd8ef9f60ac7cf7b4b1edd1;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 46b70d756..8aca8768a 100644 --- a/org.argeo.cms/src/org/argeo/cms/LocaleUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/LocaleUtils.java @@ -1,20 +1,15 @@ package org.argeo.cms; -import java.security.AccessController; -import java.util.ArrayList; -import java.util.List; import java.util.Locale; 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; +import org.argeo.api.cms.CmsLog; /** Utilities simplifying the development of localization enums. */ public class LocaleUtils { - private final static Log log = LogFactory.getLog(LocaleUtils.class); + final static String DEFAULT_OSGI_l10N_BUNDLE = "/OSGI-INF/l10n/bundle"; + + private final static CmsLog log = CmsLog.getLog(LocaleUtils.class); private final static ThreadLocal threadLocale = new ThreadLocal<>(); @@ -22,36 +17,55 @@ public class LocaleUtils { threadLocale.set(locale); } + public static String local(Localized localized) { + return local(localized.name(), localized.getClass().getClassLoader()); + } + + public static String local(Localized localized, Locale locale) { + if (localized.name() == null) // untranslated + return localized.local(locale); + return local(localized.name(), locale, localized.getClass().getClassLoader()); + } + + @Deprecated public static String local(Enum en) { - return local(en, getCurrentLocale(), "/OSGI-INF/l10n/bundle"); + return local(en, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE); } + @Deprecated public static String local(Enum en, Locale locale) { - return local(en, locale, "/OSGI-INF/l10n/bundle"); + return local(en, locale, DEFAULT_OSGI_l10N_BUNDLE); } + @Deprecated public static String local(Enum en, Locale locale, String resource) { return local(en, locale, resource, en.getClass().getClassLoader()); } + @Deprecated public static String local(Enum en, Locale locale, String resource, ClassLoader classLoader) { return local(en.name(), locale, resource, classLoader); } public static String local(String key, ClassLoader classLoader) { - return local(key, getCurrentLocale(), "/OSGI-INF/l10n/bundle", classLoader); + return local(key, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE, classLoader); } public static String local(String key, Locale locale, ClassLoader classLoader) { - return local(key, locale, "/OSGI-INF/l10n/bundle", classLoader); + return local(key, locale, DEFAULT_OSGI_l10N_BUNDLE, classLoader); } + /** Where the search for a message is actually performed. */ public static String local(String key, Locale locale, String resource, ClassLoader classLoader) { ResourceBundle rb = ResourceBundle.getBundle(resource, locale, classLoader); - assert key.length() > 2; - if (isLocaleKey(key)) + if (isLocaleKey(key)) { + assert key.length() > 1; key = key.substring(1); - return rb.getString(key); + } + if (rb.containsKey(key)) + return rb.getString(key); + else // for simple cases, the key will actually be the English word + return key; } public static boolean isLocaleKey(String str) { @@ -61,27 +75,35 @@ public class LocaleUtils { return false; } - public static String lead(String raw, Locale locale) { + /** Lead transformation on the translated string. */ + public static String toLead(String raw, Locale locale) { return raw.substring(0, 1).toUpperCase(locale) + raw.substring(1); } + public static String lead(Localized localized, ClassLoader classLoader) { + Locale locale = getCurrentLocale(); + if (localized.name() == null)// untranslated + return toLead(localized.local(locale), locale); + return toLead(local(localized.name(), getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE, classLoader), locale); + } + public static String lead(Localized localized) { - return lead(localized, getCurrentLocale()); + return lead(localized, localized.getL10nClassLoader()); } public static String lead(Localized localized, Locale locale) { - return lead(localized.local(locale).toString(), locale); + return toLead(local(localized, locale), locale); } static Locale getCurrentLocale() { Locale currentLocale = null; - if (Subject.getSubject(AccessController.getContext()) != null) + if (CurrentUser.isAvailable()) currentLocale = CurrentUser.locale(); else if (threadLocale.get() != null) { currentLocale = threadLocale.get(); } - if (log.isDebugEnabled()) - log.debug("Thread #" + Thread.currentThread().getId() + " " + Thread.currentThread().getName() + " locale: " + if (log.isTraceEnabled()) + log.trace("Thread #" + Thread.currentThread().getId() + " " + Thread.currentThread().getName() + " locale: " + currentLocale); if (currentLocale == null) throw new IllegalStateException("No locale found"); @@ -91,26 +113,4 @@ public class LocaleUtils { // return Locale.getDefault(); } - /** Returns null if argument is null. */ - public static List asLocaleList(Object locales) { - if (locales == null) - return null; - ArrayList availableLocales = new ArrayList(); - String[] codes = locales.toString().split(","); - for (int i = 0; i < codes.length; i++) { - String code = codes[i]; - // variant not supported - int indexUnd = code.indexOf("_"); - Locale locale; - if (indexUnd > 0) { - String language = code.substring(0, indexUnd); - String country = code.substring(indexUnd + 1); - locale = new Locale(language, country); - } else { - locale = new Locale(code); - } - availableLocales.add(locale); - } - return availableLocales; - } }