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

Florent Guillaume fg@nuxeo.com
Thu, 30 Jan 2003 13:19:17 -0500


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

Modified Files:
	DummyEngine.py TALInterpreter.py 
Log Message:
I18n interpolation now tries to deal with the case where there is a mix
of Unicode and non-ascii string that are incompatible (because the
encoding of the latter is unknown) by substituting a representation of
the non-ascii string.

I18n interpolation doesn't fail anymore if a i18n:name is not provided,
the ${string} in the translation is just left as is.

Collector #696: tal:replace of a non-string (a number for examlpe)
associated with a i18n:name failed to be interpolated properly.

Improved tests for i18n and added tests for interpolate().



=== Zope/lib/python/TAL/DummyEngine.py 1.35 => 1.36 ===
--- Zope/lib/python/TAL/DummyEngine.py:1.35	Wed Oct  9 10:40:33 2002
+++ Zope/lib/python/TAL/DummyEngine.py	Thu Jan 30 13:18:43 2003
@@ -240,7 +240,7 @@
         # substrings.  Then upcase everything but the placeholders, then glue
         # things back together.
         def repl(m, mapping=mapping):
-            return mapping[m.group(m.lastindex).lower()]
+            return ustr(mapping[m.group(m.lastindex).lower()])
         cre = re.compile(r'\$(?:([_A-Z]\w*)|\{([_A-Z]\w*)\})')
         return cre.sub(repl, msgid.upper())
 


=== Zope/lib/python/TAL/TALInterpreter.py 1.75 => 1.76 ===
--- Zope/lib/python/TAL/TALInterpreter.py:1.75	Wed Oct  9 10:40:33 2002
+++ Zope/lib/python/TAL/TALInterpreter.py	Thu Jan 30 13:18:44 2003
@@ -52,7 +52,11 @@
 _get_var_regex = re.compile(r'%(n)s' %({'n': NAME_RE}))
 
 def interpolate(text, mapping):
-    """Interpolate ${keyword} substitutions."""
+    """Interpolate ${keyword} substitutions.
+
+    This is called when no translation is provided by the translation
+    service.
+    """
     if not mapping:
         return text
     # Find all the spots we want to substitute.
@@ -60,7 +64,17 @@
     # Now substitute with the variables in mapping.
     for string in to_replace:
         var = _get_var_regex.findall(string)[0]
-        text = text.replace(string, mapping.get(var))
+        if mapping.has_key(var):
+            # Call ustr because we may have an integer for instance.
+            subst = ustr(mapping[var])
+            try:
+                text = text.replace(string, subst)
+            except UnicodeError:
+                # subst contains high-bit chars...
+                # As we have no way of knowing the correct encoding,
+                # substitue something instead of raising an exception.
+                subst = `subst`[1:-1]
+                text = text.replace(string, subst)
     return text