Improve localisation
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 17 Apr 2018 11:05:37 +0000 (13:05 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 17 Apr 2018 11:05:37 +0000 (13:05 +0200)
org.argeo.cms.e4/src/org/argeo/cms/e4/handlers/LocaleAddon.java
org.argeo.cms/src/org/argeo/cms/CmsMsg.java
org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java
org.argeo.cms/src/org/argeo/cms/i18n/DefaultsResourceBundle.java
org.argeo.cms/src/org/argeo/cms/i18n/LocaleUtils.java
org.argeo.cms/src/org/argeo/cms/i18n/Localized.java

index 265b2130943ad92558a30247c2ae62b9c2eee27a..e1a2581dc27052c6f0e58f7730df64245e3c942b 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Locale;
 import javax.annotation.PostConstruct;
 import javax.security.auth.Subject;
 
+import org.argeo.eclipse.ui.specific.UiContext;
 import org.eclipse.e4.core.services.nls.ILocaleChangeService;
 
 public class LocaleAddon {
@@ -14,5 +15,6 @@ public class LocaleAddon {
                Subject subject = Subject.getSubject(AccessController.getContext());
                Locale locale = subject.getPublicCredentials(Locale.class).iterator().next();
                localeChangeService.changeApplicationLocale(locale);
+               UiContext.setLocale(locale);
        }
 }
index 89f1921c4d53dd3e160400cc61113ee8be31f9e8..521ceedef120417088f98b17d8f3b69ba13ba05b 100644 (file)
@@ -1,22 +1,9 @@
 package org.argeo.cms;
 
-import java.util.Locale;
-
-import org.argeo.cms.i18n.LocaleUtils;
 import org.argeo.cms.i18n.Localized;
 
 public enum CmsMsg implements Localized {
        username, password, login, logout, register, changePassword, currentPassword, newPassword, repeatNewPassword, passwordChanged;
 
-       public Object local(Locale locale) {
-               return LocaleUtils.local(this, locale);
-       }
-
-       public String lead() {
-               return LocaleUtils.lead(this);
-       }
 
-       public String lead(Locale locale) {
-               return LocaleUtils.lead(local(locale).toString(), locale);
-       }
 }
index ef56adc33a318773941ac56a69d3fec202e6e862..34f4457ed446021369a5a51e340bdf9d715cada3 100644 (file)
@@ -22,6 +22,7 @@ import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.security.acl.Group;
 import java.util.HashSet;
+import java.util.Locale;
 import java.util.Set;
 import java.util.UUID;
 
@@ -30,6 +31,7 @@ import javax.security.auth.x500.X500Principal;
 
 import org.argeo.cms.CmsException;
 import org.argeo.cms.internal.auth.CmsSessionImpl;
+import org.argeo.cms.internal.kernel.Activator;
 import org.argeo.node.NodeConstants;
 import org.osgi.service.useradmin.Authorization;
 
@@ -38,7 +40,7 @@ import org.osgi.service.useradmin.Authorization;
  * context.
  */
 public final class CurrentUser {
-//     private final static Log log = LogFactory.getLog(CurrentUser.class);
+       // private final static Log log = LogFactory.getLog(CurrentUser.class);
        // private final static BundleContext bc =
        // FrameworkUtil.getBundle(CurrentUser.class).getBundleContext();
        /*
@@ -48,16 +50,15 @@ public final class CurrentUser {
        /**
         * Technical username of the currently authenticated user.
         * 
-        * @return the authenticated username or null if not authenticated /
-        *         anonymous
+        * @return the authenticated username or null if not authenticated / anonymous
         */
        public static String getUsername() {
                return getUsername(currentSubject());
        }
 
        /**
-        * Human readable name of the currently authenticated user (typically first
-        * name and last name).
+        * Human readable name of the currently authenticated user (typically first name
+        * and last name).
         */
        public static String getDisplayName() {
                return getDisplayName(currentSubject());
@@ -68,6 +69,11 @@ public final class CurrentUser {
                return isAnonymous(currentSubject());
        }
 
+       /** Locale of the current user */
+       public final static Locale locale() {
+               return locale(currentSubject());
+       }
+
        /** Roles of the currently logged-in user */
        public final static Set<String> roles() {
                return roles(currentSubject());
@@ -115,6 +121,15 @@ public final class CurrentUser {
                return roles;
        }
 
+       public final static Locale locale(Subject subject) {
+               Set<Locale> locales = subject.getPublicCredentials(Locale.class);
+               if (locales.isEmpty()) {
+                       Locale defaultLocale = Activator.getNodeState().getDefaultLocale();
+                       return defaultLocale;
+               } else
+                       return locales.iterator().next();
+       }
+
        /** Whether this user is currently authenticated. */
        public static boolean isAnonymous(Subject subject) {
                if (subject == null)
@@ -152,8 +167,8 @@ public final class CurrentUser {
 
        /**
         * The node authenticated component (typically a CMS view) related to this
-        * display, or null if none is available from this call. <b>Not API: Only
-        * for low-level access.</b>
+        * display, or null if none is available from this call. <b>Not API: Only for
+        * low-level access.</b>
         */
        // private static CmsAuthenticated getNodeAuthenticated() {
        // return UiContext.getData(CmsAuthenticated.KEY);
@@ -171,8 +186,8 @@ public final class CurrentUser {
                        return false;
                CmsSessionImpl cmsSession = CmsSessionImpl.getByUuid(nodeSessionId.toString());
                cmsSession.close();
-//             if (log.isDebugEnabled())
-//                     log.debug("Logged out CMS session " + cmsSession.getUuid());
+               // if (log.isDebugEnabled())
+               // log.debug("Logged out CMS session " + cmsSession.getUuid());
                return true;
        }
 
index 1c9aff0ca5e1d48245975e8c7556b6ccfa4830f4..78d717a20e6ec8077b09d8dda2ad8a6aaa21edb7 100644 (file)
@@ -9,6 +9,7 @@ import java.util.Vector;
 import org.argeo.cms.CmsException;
 
 /** Expose the default values as a {@link ResourceBundle} */
+@Deprecated
 public class DefaultsResourceBundle extends ResourceBundle {
 
        @Override
index 700e731058037a26a2da15c998910b315523332c..e0a77bcf3bdda1d6abbf401adc7b8378cc1f086a 100644 (file)
@@ -5,6 +5,8 @@ import java.util.List;
 import java.util.Locale;
 import java.util.ResourceBundle;
 
+import org.argeo.cms.auth.CurrentUser;
+
 /** Utilities simplifying the development of localization enums. */
 public class LocaleUtils {
        public static Object local(Enum<?> en) {
@@ -37,9 +39,10 @@ public class LocaleUtils {
        }
 
        static Locale getCurrentLocale() {
+               return CurrentUser.locale();
                // return UiContext.getLocale();
                // FIXME look into Subject or settings
-               return Locale.getDefault();
+               // return Locale.getDefault();
        }
 
        /** Returns null if argument is null. */
index b34cc6ca8e76038f231ed15bc9e127c76ce9726f..9378d476d402bdc7af35f38e7987e58447285d74 100644 (file)
@@ -4,5 +4,17 @@ import java.util.Locale;
 
 /** Localized object. */
 public interface Localized {
-       public Object local(Locale locale);
+       /** Default assumes that this is an {@link Enum} */
+       default Object local(Locale locale) {
+               return LocaleUtils.local((Enum<?>) this, locale);
+       }
+
+       default String lead() {
+               return LocaleUtils.lead(this);
+       }
+
+       default String lead(Locale locale) {
+               return LocaleUtils.lead(local(locale).toString(), locale);
+       }
+
 }