[Zope-Checkins] CVS: Zope3/lib/python/Zope/TAL - DummyEngine.py:1.36

Barry Warsaw barry@wooz.org
Mon, 1 Jul 2002 13:39:21 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/TAL
In directory cvs.zope.org:/tmp/cvs-serv10319

Modified Files:
	DummyEngine.py 
Log Message:
DummyDomain.translate(): Use a slightly more obscure but more efficent
regexp (based on a suggestion by Frederick Lundh) for transforming and
interpolating a msgid.


=== Zope3/lib/python/Zope/TAL/DummyEngine.py 1.35 => 1.36 ===
         # substrings.  Then upcase everything but the placeholders, then glue
         # things back together.
-        s = msgid.replace('%', '%%')
-        parts = re.split(r'(\${2})|\$([_a-z]\w*)|\${([_a-z]\w*)}',
-                         s, re.IGNORECASE)
-        if len(parts) == 1:
-            # There are no ${name} placeholders in the source string.  We use
-            # msgid here so we don't end up with doubled %'s.
-            return msgid.upper()
-        # Transform ${name} into %(name)s so we can use Python's built-in
-        # string interpolation feature.
-        xlateParts = []
-        for i in range(1, len(parts), 4):
-            if parts[i] is not None:
-                p = parts[i] = '$'
-            elif parts[i+1] is not None:
-                p = parts[i+1] = '%(' + parts[i+1] + ')s'
-            else:
-                p = parts[i+2] = '%(' + parts[i+2] + ')s'
-            xlateParts.append(p)
-            xlateParts.append(parts[i+3].upper())
-        if mapping is None:
-            mapping = {}
-        return ''.join(xlateParts) % mapping
-        
+        def repl(m):
+            return mapping[m.group(m.lastindex).lower()]
+        cre = re.compile(r'\$(?:([_a-z]\w*)|\{([_a-z]\w*)\})', re.IGNORECASE)
+        return cre.sub(repl, msgid.upper())
 
 class DummyTranslationService:
     __implements__ = ITranslationService