[Zope-Checkins] CVS: Zope3/lib/python/Zope/TAL - TranslationContext.py:1.1.2.1 TALGenerator.py:1.52.16.3.4.9 TALInterpreter.py:1.63.10.3.4.4

Fred L. Drake, Jr. fdrake@acm.org
Mon, 10 Jun 2002 15:28:08 -0400


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

Modified Files:
      Tag: fdrake-tal-i18n-branch
	TALGenerator.py TALInterpreter.py 
Added Files:
      Tag: fdrake-tal-i18n-branch
	TranslationContext.py 
Log Message:
Track the "translation context", consisting of i18n:domain, i18n:target, and
i18n:source.  This can be used by TALInterpreter.translate() once we get a
translation service wired up.


=== Added File Zope3/lib/python/Zope/TAL/TranslationContext.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""Translation context object for the TALInterpreter's I18N support.

The translation context provides a container for the information
needed to perform translation of a marked string from a page template.

$Id: TranslationContext.py,v 1.1.2.1 2002/06/10 19:28:07 fdrake Exp $
"""

class TranslationContext:
    """Information about the I18N settings of a TAL processor."""

    def __init__(self, parent=None, domain=None, target=None, source=None):
        if parent:
            if not domain:
                domain = parent.domain
            if not target:
                target = parent.target
            if not source:
                source = parent.source

        self.parent = parent
        self.domain = domain
        self.target = target
        self.source = source


=== Zope3/lib/python/Zope/TAL/TALGenerator.py 1.52.16.3.4.8 => 1.52.16.3.4.9 ===
 
         if taldict or i18ndict:
+            if i18ndict:
+                if (  "domain" in i18ndict
+                      or "source" in i18ndict
+                      or "target" in i18ndict):
+                    self.emit("beginI18nContext", i18ndict)
+                    todo["i18ncontext"] = 1
             dict = {}
             for item in attrlist:
                 key, value = item[:2]
@@ -643,6 +649,7 @@
         scope = todo.get("scope")
         optTag = todo.get("optional tag")
         msgid = todo.get('msgid')
+        i18ncontext = todo.get("i18ncontext")
         varname = todo.get('i18nvar')
 
         if implied > 0:
@@ -684,6 +691,8 @@
             self.emitOnError(name, onError)
         if scope:
             self.emit("endScope")
+        if i18ncontext:
+            self.emit("endI18nContext")
         if defineSlot:
             self.emitDefineSlot(defineSlot)
         if fillSlot:


=== Zope3/lib/python/Zope/TAL/TALInterpreter.py 1.63.10.3.4.3 => 1.63.10.3.4.4 ===
 from TALDefs import isCurrentVersion, getProgramVersion, getProgramMode
 from TALGenerator import TALGenerator
+from TranslationContext import TranslationContext
 
 BOOLEAN_HTML_ATTRS = [
     # List of Boolean attributes in HTML that should be rendered in
@@ -114,22 +115,24 @@
         self.col = 0
         self.level = 0
         self.scopeLevel = 0
+        self.i18nContext = TranslationContext()
 
     def saveState(self):
         return (self.position, self.col, self.stream,
-                self.scopeLevel, self.level)
+                self.scopeLevel, self.level, self.i18nContext)
 
     def restoreState(self, state):
-        (self.position, self.col, self.stream, scopeLevel, level) = state
+        (self.position, self.col, self.stream, scopeLevel, level, i18n) = state
         self._stream_write = self.stream.write
         assert self.level == level
         while self.scopeLevel > scopeLevel:
             self.engine.endScope()
             self.scopeLevel = self.scopeLevel - 1
         self.engine.setPosition(self.position)
+        self.i18nContext = i18n
 
     def restoreOutputState(self, state):
-        (dummy, self.col, self.stream, scopeLevel, level) = state
+        (dummy, self.col, self.stream, scopeLevel, level, i18n) = state
         self._stream_write = self.stream.write
         assert self.level == level
         assert self.scopeLevel == scopeLevel
@@ -152,9 +155,11 @@
     def __call__(self):
         assert self.level == 0
         assert self.scopeLevel == 0
+        assert self.i18nContext.parent is None
         self.interpret(self.program)
         assert self.level == 0
         assert self.scopeLevel == 0
+        assert self.i18nContext.parent is None
         if self.col > 0:
             self._stream_write("\n")
             self.col = 0
@@ -416,6 +421,19 @@
     def do_setGlobal_tal(self, (name, expr)):
         self.engine.setGlobal(name, self.engine.evaluateValue(expr))
     bytecode_handlers["setGlobal"] = do_setLocal
+
+    def do_beginI18nContext(self, settings):
+        get = settings.get
+        self.i18nContext = TranslationContext(self.i18nContext,
+                                              domain=get("domain"),
+                                              source=get("source"),
+                                              target=get("target"))
+    bytecode_handlers["beginI18nContext"] = do_beginI18nContext
+
+    def do_endI18nContext(self):
+        self.i18nContext = self.i18nContext.parent
+        assert self.i18nContext is not None
+    bytecode_handlers["endI18nContext"] = do_endI18nContext
 
     def do_insertText(self, stuff):
         self.interpret(stuff[1])