[ZPT] CVS: Zope3/lib/python/Zope/TAL - TALGenerator.py:1.52.16.3.4.7

Barry Warsaw barry@wooz.org
Tue, 4 Jun 2002 18:59:42 -0400


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

Modified Files:
      Tag: fdrake-tal-i18n-branch
	TALGenerator.py 
Log Message:
First shot at implementing the i18n:translate attribute.  This is the
intermediate code generation part.

emitTranslation(): This method emits the new `insertTranslation'
opcode.  Like emitSubstitution() but simpler because we don't (yet?) 
have to evaluate the attribute's value; it's just a simple message id
or the empty string (for implicit message id).

replaceAttrs(): Remove the special cruft that tried to transform an
i18n action into a tal action (right now, I don't think we need
this).

emitStartElement(): Grab the msgid out of the i18ndict's `translate'
key.  All tests of msgid existing must use "is not None" because at
the code generation stage we're preserving the empty string if
implicit msgids are used.

Update sanity checks for new i18n namespace attributes.

Emit a `beginScope' opcode if there's a taldict or an i18ndict, and if
there's a msgid (even implicit), put the msgid in the todo dict and
push the program onto the stack.

emitEndElement(): If there's a msgid (even implicit), emit the
'insertTranslation' opcode.


=== Zope3/lib/python/Zope/TAL/TALGenerator.py 1.52.16.3.4.6 => 1.52.16.3.4.7 ===
             self.emit("insertStructure", cexpr, attrDict, program)
 
+    def emitTranslation(self, msgid):
+        program = self.popProgram()
+        self.emit('insertTranslation', msgid, program)
+
     def emitDefineMacro(self, macroName):
         program = self.popProgram()
         macroName = macroName.strip()
@@ -394,8 +398,6 @@
                 expr, xlat = repldict[key]
                 item = item[:2] + ("replace", expr, xlat)
                 del repldict[key]
-            elif len(item) == 3 and item[2] == 'i18n':
-                item = item[:2] + ('tal',)
             newlist.append(item)
         # Add dynamic-only attributes
         for key, (expr, xlat) in repldict.items():
@@ -447,10 +449,13 @@
         omitTag = taldict.get("omit-tag")
         TALtag = taldict.get("tal tag")
         i18nattrs = i18ndict.get("attributes")
-        i18nid = i18ndict.get("id")
+        # Preserve empty string if implicit msgids are used.  We'll generate
+        # code with the msgid='' and calculate the right implicit msgid during
+        # interpretation phase.
+        msgid = i18ndict.get("translate")
 
-        if i18ndict.has_key("data") and not i18ndict.has_key("id"):
-            raise I18NError("i18n:data must be accompanied by i18n:id",
+        if i18ndict.has_key("data") and not i18ndict.has_key("name"):
+            raise I18NError("i18n:data must be accompanied by i18n:name",
                             position)
         
         if len(metaldict) > 1 and (defineMacro or useMacro):
@@ -461,8 +466,8 @@
             if content:
                 raise TALError("content and replace are mutually exclusive",
                                position)
-            if i18nid:
-                raise I18NError("i18n:id and replace are mutually exclusive",
+            if msgid is not None:
+                raise I18NError("i18n:name and replace are mutually exclusive",
                                 position)
 
         repeatWhitespace = None
@@ -503,7 +508,7 @@
                 self.pushProgram()
                 todo["defineSlot"] = defineSlot
 
-        if taldict:
+        if taldict or i18ndict:
             dict = {}
             for item in attrlist:
                 key, value = item[:2]
@@ -531,6 +536,8 @@
         if replace:
             todo["replace"] = replace
             self.pushProgram()
+        if msgid is not None:
+            todo['msgid'] = msgid
         optTag = omitTag is not None or TALtag
         if optTag:
             todo["optional tag"] = omitTag, TALtag
@@ -561,6 +568,8 @@
             self.pushProgram()
         if content:
             self.pushProgram()
+        if msgid is not None:
+            self.pushProgram()
         if todo and position != (None, None):
             todo["position"] = position
         self.todoPush(todo)
@@ -589,6 +598,7 @@
         repldict = todo.get("repldict", {})
         scope = todo.get("scope")
         optTag = todo.get("optional tag")
+        msgid = todo.get('msgid')
 
         if implied > 0:
             if defineMacro or useMacro or defineSlot or fillSlot:
@@ -602,6 +612,8 @@
 
         if content:
             self.emitSubstitution(content, {})
+        if msgid is not None:
+            self.emitTranslation(msgid)
         if optTag:
             self.emitOptTag(name, optTag, isend)
         elif not isend: