Support i18n in directory
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 23 Sep 2022 07:57:18 +0000 (09:57 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 23 Sep 2022 07:57:18 +0000 (09:57 +0200)
org.argeo.util/src/org/argeo/util/directory/HierarchyUnit.java
org.argeo.util/src/org/argeo/util/directory/ldap/AbstractLdapDirectory.java
org.argeo.util/src/org/argeo/util/directory/ldap/DefaultLdapEntry.java
org.argeo.util/src/org/argeo/util/directory/ldap/LdapEntry.java
org.argeo.util/src/org/argeo/util/directory/ldap/LdapHierarchyUnit.java
swt/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/CmsFeedback.java
swt/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/LightweightDialog.java

index 1acd34f487278a5e1c06a2d978f97980abc75aa0..947b6bc8553124e189f16fc6ab64671fffc6f1ed 100644 (file)
@@ -1,12 +1,16 @@
 package org.argeo.util.directory;
 
 import java.util.Dictionary;
+import java.util.Locale;
 
 /** A unit within the high-level organisational structure of a directory. */
 public interface HierarchyUnit {
        /** Name to use in paths. */
        String getHierarchyUnitName();
 
+       /** Name to use in UI. */
+       String getHierarchyUnitLabel(Locale locale);
+
        /**
         * The parent {@link HierarchyUnit}, or <code>null</code> if a
         * {@link Directory}.
index 04398bb4bff0cd74881e8da7e3a5ce1d632d46a0..28d8d081ccdcfb875e5ce8941086f01cc3be3c00 100644 (file)
@@ -10,6 +10,7 @@ import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Locale;
 import java.util.Optional;
 import java.util.StringJoiner;
 
@@ -296,6 +297,16 @@ public abstract class AbstractLdapDirectory implements Directory, XAResourceProv
                return getName();
        }
 
+       @Override
+       public String getHierarchyUnitLabel(Locale locale) {
+               String key = LdapNameUtils.getLastRdn(getBaseDn()).getType();
+               Object value = LdapEntry.getLocalized(asLdapEntry().getProperties(), key, locale);
+               if (value == null)
+                       value = getHierarchyUnitName();
+               assert value != null;
+               return value.toString();
+       }
+
        @Override
        public HierarchyUnit getParent() {
                return null;
index c4d20feba922576dc181129ebcd75dae523fbde5..218e0c57e0977dd5805d49fb37903b7b9a069e80 100644 (file)
@@ -35,8 +35,8 @@ public class DefaultLdapEntry implements LdapEntry {
 //     private Attributes publishedAttributes;
 
        // Temporarily expose the fields
-       protected AttributeDictionary properties;
-       protected AttributeDictionary credentials;
+       private AttributeDictionary properties;
+       private AttributeDictionary credentials;
 
        protected DefaultLdapEntry(AbstractLdapDirectory directory, LdapName dn) {
                Objects.requireNonNull(directory);
index f34de7eb5022f5ed6b7f7603b801c3cca30898c2..823bc9502e6eb9a57c9e51e6d381c9c29b94f9c8 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Dictionary;
 import java.util.List;
+import java.util.Locale;
 import java.util.Set;
 import java.util.StringJoiner;
 import java.util.TreeSet;
@@ -42,4 +43,14 @@ public interface LdapEntry {
                currentObjectClasses.forEach((s) -> values.add(s));
                properties.put(LdapAttrs.objectClasses.name(), values.toString());
        }
+
+       public static Object getLocalized(Dictionary<String, Object> properties, String key, Locale locale) {
+               if (locale == null)
+                       return null;
+               Object value = null;
+               value = properties.get(key + ";lang-" + locale.getLanguage() + "-" + locale.getCountry());
+               if (value == null)
+                       value = properties.get(key + ";lang-" + locale.getLanguage());
+               return value;
+       }
 }
index bd12244eafb84d9a1daa2f6623698bc4bf917054..961f2e35886c1fd783d60dd7d10d0b421cc3f602 100644 (file)
@@ -1,5 +1,7 @@
 package org.argeo.util.directory.ldap;
 
+import java.util.Locale;
+
 import javax.naming.ldap.LdapName;
 import javax.naming.ldap.Rdn;
 
@@ -39,6 +41,16 @@ public class LdapHierarchyUnit extends DefaultLdapEntry implements HierarchyUnit
                return name;
        }
 
+       @Override
+       public String getHierarchyUnitLabel(Locale locale) {
+               String key = LdapNameUtils.getLastRdn(getDn()).getType();
+               Object value = LdapEntry.getLocalized(getProperties(), key, locale);
+               if (value == null)
+                       value = getHierarchyUnitName();
+               assert value != null;
+               return value.toString();
+       }
+
        @Override
        public String getBase() {
                return getDn().toString();
index 69b117849949f33a01c3065b4bf2fc5f2751f3a9..91885c74b21deddf70c28a066cc283721e0e0c51 100644 (file)
@@ -8,6 +8,7 @@ import org.argeo.api.cms.CmsLog;
 import org.argeo.cms.CmsMsg;
 import org.argeo.cms.swt.Selected;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
@@ -95,6 +96,13 @@ public class CmsFeedback extends LightweightDialog {
                return messageLbl;
        }
 
+       protected Point getInitialSize() {
+               if (exception != null)
+                       return new Point(800, 600);
+               else
+                       return new Point(600, 400);
+       }
+
        protected void append(String message) {
                stack.append(message);
                stack.append("\n");
index 9e6a8d5b04013b13d4ecb361d7919ae5cdd3670a..d74be6aa20c59c3ba4348c56f680f438a3514987 100644 (file)
@@ -183,9 +183,6 @@ public class LightweightDialog {
        }
 
        protected Point getInitialSize() {
-               // if (exception != null)
-               // return new Point(800, 600);
-               // else
                return new Point(600, 400);
        }