[Zope3-checkins] CVS: Zope3/src/zope/i18n - interfaces.py:1.8 translate.py:1.3

Barry Warsaw barry@wooz.org
Tue, 25 Mar 2003 15:21:58 -0500


Update of /cvs-repository/Zope3/src/zope/i18n
In directory cvs.zope.org:/tmp/cvs-serv25905/src/zope/i18n

Modified Files:
	interfaces.py translate.py 
Log Message:
Add an ITranslator interface and a Translator implementation of that
interface.  This is basically a glue class for specifying the domain,
context, and locale in one packet and then providing a translate()
method which just takes the message id and mapping.

Glue this into the Zope app's page template engine.

Also removed some unused code and fix some typos.


=== Zope3/src/zope/i18n/interfaces.py 1.7 => 1.8 ===
--- Zope3/src/zope/i18n/interfaces.py:1.7	Tue Mar 25 12:01:52 2003
+++ Zope3/src/zope/i18n/interfaces.py	Tue Mar 25 15:21:28 2003
@@ -197,7 +197,21 @@
     """
 
     def translate(msgid, mapping=None, context=None, target_language=None):
-        """Translate the the source to its appropriate language.
+        """Translate the source msgid to its appropriate language.
+
+        See ITranslationService for details.
+        """
+
+
+class ITranslator(Interface):
+    """A collaborative object which contains the domain, context, and locale.
+
+    It is expected that object be constructed with enough information to find
+    the domain, context, and target language.
+    """
+
+    def translate(msgid, mapping=None):
+        """Translate the source msgid using the given mapping.
 
         See ITranslationService for details.
         """
@@ -438,7 +452,7 @@
 
     This object is also used to uniquely identify a locale."""
 
-    language = TextLine(title=u"Lanaguage Id",
+    language = TextLine(title=u"Language Id",
                         constraint=re.compile(r'[a-z]{2}').match)
 
     country = TextLine(title=u"Country Id",
@@ -628,9 +642,9 @@
 
     currencies = Container(title=u"Currencies")
 
-    languages = Dict(title=u"Lanaguage id to translated name",
-                     key_types=(TextLine(title=u"Lanaguege Id"),),
-                     value_types=(TextLine(title=u"Lanaguege Name"),),
+    languages = Dict(title=u"Language id to translated name",
+                     key_types=(TextLine(title=u"Language Id"),),
+                     value_types=(TextLine(title=u"Language Name"),),
                      )
 
     countries = Dict(title=u"Country id to translated name",


=== Zope3/src/zope/i18n/translate.py 1.2 => 1.3 ===
--- Zope3/src/zope/i18n/translate.py:1.2	Wed Dec 25 09:13:39 2002
+++ Zope3/src/zope/i18n/translate.py	Tue Mar 25 15:21:28 2003
@@ -16,13 +16,33 @@
 $Id$
 """
 
+from zope.i18n.interfaces import ITranslator
+from zope.component import getService
 
-def translate(place, domain, source, mapping=None, context=None,
-              target_language=None):
-    """Translates a source text based on a location in the Zope architecture
-       and a domain."""
 
-    # Lookup service...
-    service = None
+class Translator:
+    """A collaborative object which contains the domain, context, and locale.
 
-    return service.translate(domain, source, mapping, context, target_language)
+    It is expected that object be constructed with enough information to find
+    the domain, context, and target language.
+    """
+
+    __implements__ = ITranslator
+
+    def __init__(self, locale, domain, context=None):
+        """locale comes from the request, domain specifies the application,
+        and context specifies the place (it may be none for global contexts).
+        """
+        self._locale = locale
+        self._domain = domain
+        self._context = context
+        self._translation_service = getService(context, 'Translation')
+        
+    def translate(self, msgid, mapping=None):
+        """Translate the source msgid using the given mapping.
+
+        See ITranslationService for details.
+        """
+        return self._translation_service.translate(
+            self._domain, msgid, mapping, self._context,
+            self._locale.id.language)