[Yanel-commits] rev 22876 - public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation

josias at wyona.com josias at wyona.com
Thu Feb 22 17:36:36 CET 2007


Author: josias
Date: 2007-02-22 17:36:34 +0100 (Thu, 22 Feb 2007)
New Revision: 22876

Modified:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java
Log:
allow default text inside of the i18n element. the default text will be used when no translation exists for the given key

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java	2007-02-22 15:59:03 UTC (rev 22875)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java	2007-02-22 16:36:34 UTC (rev 22876)
@@ -15,6 +15,9 @@
     private static Category log = Category.getInstance(I18nTransformer2.class);
     private Locale currentLocale = null;
     private ResourceBundle messageBundle = null;
+    private boolean insideI18n;
+    private String key;
+    private StringBuffer textBuffer;
 
     public static final String NS_URI = "http://apache.org/cocoon/i18n/2.1";
     
@@ -26,22 +29,16 @@
     public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException {
         String eName = ("".equals(localName)) ? qName : localName;
         
+        if (this.insideI18n) {
+            throw new SAXException("no elements allowed inside of i18n element");
+        }
+        
         if (isI18nElement(namespaceURI, localName, qName)) {
-            String key = attrs.getValue("key");
+            this.insideI18n = true;
+            this.textBuffer = new StringBuffer(); 
+            this.key = attrs.getValue("key");
             //String i18nText = messageBundle.getString(key);
             
-            String i18nText;
-            try {
-                i18nText = messageBundle.getString(key);
-            } catch (Exception e) {
-                log.error("cannot find message for key: " + key);
-                i18nText = key;
-            }
-            
-            log.debug("TAG [key] " + key + " [message]" + i18nText);
-            char[] i18nMessage = i18nText.toCharArray(); 
-            characters(i18nMessage, 0, i18nMessage.length);
-            
         } else {
             // translate attributes:
             
@@ -111,7 +108,24 @@
     public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
         String eName = ("".equals(localName)) ? qName : localName;
         if (isI18nElement(namespaceURI, localName, qName)) {
-            //ignore these tags
+            String defaultText = this.textBuffer.toString();
+            if (this.key == null) {
+                this.key = defaultText;
+            }
+            String i18nText;
+            try {
+                i18nText = messageBundle.getString(this.key);
+            } catch (Exception e) {
+                log.error("cannot find message for key: " + this.key);
+                i18nText = defaultText.length() == 0 ? this.key : defaultText;
+            }
+            
+            if (log.isDebugEnabled()) {
+                log.debug("TAG [key] " + this.key + " [message]" + i18nText);
+            }
+            char[] i18nMessage = i18nText.toCharArray(); 
+            this.insideI18n = false;
+            characters(i18nMessage, 0, i18nMessage.length);
         } else {
             super.endElement(namespaceURI, localName, qName);
         }
@@ -127,4 +141,12 @@
         }
     }
 
+    public void characters(char[] buf, int offset, int len) throws SAXException {
+        if (this.insideI18n) {
+            this.textBuffer.append(buf, offset, len);
+        } else {
+            super.characters(buf, offset, len);
+        }
+    }
+
 }




More information about the Yanel-commits mailing list