incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1141340 - in /sling/trunk/contrib/extensions/i18n/src: main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java
Date Thu, 30 Jun 2011 00:15:17 GMT
Author: justin
Date: Thu Jun 30 00:15:17 2011
New Revision: 1141340

URL: http://svn.apache.org/viewvc?rev=1141340&view=rev
Log:
SLING-2111 - querying for jcr:language values which either match the value of Locale.toString()
or the RFC 4646 version of the Locale's String representation.

Modified:
    sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
    sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java

Modified: sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java?rev=1141340&r1=1141339&r2=1141340&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
(original)
+++ sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
Thu Jun 30 00:15:17 2011
@@ -150,9 +150,23 @@ public class JcrResourceBundle extends R
             final String baseName) {
         StringBuilder buf = new StringBuilder(64);
 
-        buf.append("//element(*,mix:language)[@jcr:language='");
-        buf.append(locale);
-        buf.append('\'');
+        buf.append("//element(*,mix:language)[");
+
+        String localeString = locale.toString();
+        String localeRFC4646String = toRFC4646String(locale);
+
+        if (localeString.equals(localeRFC4646String)) {
+            buf.append("@jcr:language='");
+            buf.append(localeString);
+            buf.append('\'');
+        } else {
+            buf.append("(@jcr:language='");
+            buf.append(localeString);
+            buf.append('\'');
+            buf.append(" or @jcr:language='");
+            buf.append(localeRFC4646String);
+            buf.append("\')");
+        }
 
         if (baseName != null) {
             buf.append(" and @");
@@ -168,4 +182,9 @@ public class JcrResourceBundle extends R
 
         return buf.toString();
     }
+
+    // Would be nice if Locale.toString() output RFC 4646, but it doesn't
+    private static String toRFC4646String(Locale locale) {
+        return locale.toString().replace('_', '-');
+    }
 }

Modified: sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java?rev=1141340&r1=1141339&r2=1141340&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java
(original)
+++ sling/trunk/contrib/extensions/i18n/src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java
Thu Jun 30 00:15:17 2011
@@ -244,6 +244,8 @@ public class JcrResourceBundleTest exten
     // test data to add to the repository (use linked hash map for insertion order)
     public static final Map<String, Message> MESSAGES_DE = new LinkedHashMap<String,
Message>();
     public static final Map<String, Message> MESSAGES_EN = new LinkedHashMap<String,
Message>();
+    public static final Map<String, Message> MESSAGES_EN_DASH_US = new LinkedHashMap<String,
Message>();
+    public static final Map<String, Message> MESSAGES_EN_UNDERSCORE_UK = new LinkedHashMap<String,
Message>();
     public static final Map<String, Message> MESSAGES_DE_APPS = new LinkedHashMap<String,
Message>();
     public static final Map<String, Message> MESSAGES_DE_BASENAME = new LinkedHashMap<String,
Message>();
 
@@ -267,6 +269,9 @@ public class JcrResourceBundleTest exten
         // 5. not present in DE
         add(MESSAGES_EN, PARENT_MSG);
 
+        add(MESSAGES_EN_DASH_US, new Message("", "pigment", "color", false));
+        add(MESSAGES_EN_UNDERSCORE_UK, new Message("", "pigment", "colour", false));
+
         // 6. same as 1.-4., but different translations for overwriting into apps
         for (Message msg : MESSAGES_DE.values()) {
             add(MESSAGES_DE_APPS, new Message(msg.path, msg.key, "OTHER", msg.useNodeName));
@@ -298,6 +303,24 @@ public class JcrResourceBundleTest exten
             msg.add(en);
         }
         getSession().save();
+
+        // some EN US content
+        Node enDashUS = i18n.addNode("en-US", "nt:folder");
+        enDashUS.addMixin("mix:language");
+        enDashUS.setProperty("jcr:language", "en-US");
+        for (Message msg : MESSAGES_EN_DASH_US.values()) {
+            msg.add(enDashUS);
+        }
+        getSession().save();
+
+        // some EN US content
+        Node enUnderscoreUK = i18n.addNode("en_UK", "nt:folder");
+        enUnderscoreUK.addMixin("mix:language");
+        enUnderscoreUK.setProperty("jcr:language", "en_UK");
+        for (Message msg : MESSAGES_EN_UNDERSCORE_UK.values()) {
+            msg.add(enUnderscoreUK);
+        }
+        getSession().save();
     }
 
     // ---------------------------------------------------------------< tests >
@@ -307,6 +330,16 @@ public class JcrResourceBundleTest exten
         for (Message msg : MESSAGES_DE.values()) {
             assertEquals(msg.message, bundle.getString(msg.key));
         }
+        
+        bundle = new JcrResourceBundle(new Locale("en", "us"), null, resolver);
+        for (Message msg : MESSAGES_EN_DASH_US.values()) {
+            assertEquals(msg.message, bundle.getString(msg.key));
+        }
+        
+        bundle = new JcrResourceBundle(new Locale("en", "uk"), null, resolver);
+        for (Message msg : MESSAGES_EN_UNDERSCORE_UK.values()) {
+            assertEquals(msg.message, bundle.getString(msg.key));
+        }
     }
 
     public void test_getObject() {



Mime
View raw message