[ZPT] CVS: Zope/lib/python/Products/PageTemplates - GlobalTranslationService.py:1.1.2.1 TALES.py:1.31.8.3

Florent Guillaume fg@nuxeo.com
Sun, 15 Sep 2002 20:44:01 -0400


Update of /cvs-repository/Zope/lib/python/Products/PageTemplates
In directory cvs.zope.org:/tmp/cvs-serv6172/lib/python/Products/PageTemplates

Modified Files:
      Tag: Zope-2_6-i18n-branch
	TALES.py 
Added Files:
      Tag: Zope-2_6-i18n-branch
	GlobalTranslationService.py 
Log Message:
Added a hookable global translation service to PageTemplates, with tests.

A third-party product wishing to add a translation service simply has to
do:
    from Products.PageTemplates.GlobalTranslationService import \
         setGlobalTranslationService
    setGlobalTranslationService(mytranslationservice)
A translation service only needs one method, whose signature is:
    def translate(self, domain, msgid, mapping=None,
                  context=None, target_language=None)
It must return a translated string based on msgid and the domain, and
optionally the other arguments. The translated string can be Unicode.


=== Added File Zope/lib/python/Products/PageTemplates/GlobalTranslationService.py ===
##############################################################################
#
# Copyright (c) 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.
#
##############################################################################
"""Global Translation Service for providing I18n to Page Templates.

$Id: GlobalTranslationService.py,v 1.1.2.1 2002/09/16 00:43:30 efge Exp $
"""

class DummyTranslationService:
    """Translation service that does nothing and returns the message id."""
    def translate(self, domain, msgid, mapping=None,
                  context=None, target_language=None):
        return msgid
    # XXX Not all of Zope.I18n.ITranslationService is implemented.

translationService = DummyTranslationService()

def setGlobalTranslationService(service):
    """Sets the global translation service, and returns the previous one."""
    global translationService
    old_service = translationService
    translationService = service
    return old_service

def getGlobalTranslationService():
    """Returns the global translation service."""
    return translationService


=== Zope/lib/python/Products/PageTemplates/TALES.py 1.31.8.2 => 1.31.8.3 ===
--- Zope/lib/python/Products/PageTemplates/TALES.py:1.31.8.2	Sun Sep 15 20:00:46 2002
+++ Zope/lib/python/Products/PageTemplates/TALES.py	Sun Sep 15 20:43:30 2002
@@ -20,6 +20,7 @@
 import re, sys, ZTUtils
 from MultiMapping import MultiMapping
 from DocumentTemplate.DT_Util import ustr
+from GlobalTranslationService import getGlobalTranslationService
 
 StringType = type('')
 
@@ -250,6 +251,11 @@
     def setPosition(self, position):
         self.position = position
 
+    def translate(self, domain, msgid, mapping=None,
+                  context=None, target_language=None):
+        return getGlobalTranslationService().translate(
+            domain, msgid, mapping=mapping,
+            context=context, target_language=target_language)
 
 
 class TALESTracebackSupplement: