[Zope3-checkins] CVS: Zope3/src/zope/i18n - message_id.txt:1.1 __init__.py:1.3 globaltranslationservice.py:1.3 interfaces.py:1.7

Paul Winkler pw_lists@slinkp.com
Tue, 25 Mar 2003 12:02:23 -0500


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

Modified Files:
	__init__.py globaltranslationservice.py interfaces.py 
Added Files:
	message_id.txt 
Log Message:
Modified TranslationService to get the domain from msg id if possible.



=== Added File Zope3/src/zope/i18n/message_id.txt ===
=================
Translatable Text
=================

Rationale
---------

To translate any text, we must be able to discover the source
domain of the text. A source domain is an identifier that identifies a
project that produces program source strings. Source strings include
literals in python programs, text in templates, and some text in XML
data. The project implies a source language and an application
context.

We can think of a source domain as a collection of message IDs
and associated translation strings.

We often need to create strings that will be displayed by separate
views. The view cannot translate the string without knowing its source
domain. A string literal carries no domain information, so we use
message IDs. Message IDs are strings which carry a translation source
domain. These are created by a message ID factory. The message ID
factory is created by calling zope.i18n.messageIDFactory with the
source domain::

  from zope import i18n
  _ = i18n.MessageIDFactory("mydomain")

  class IContact(Interface):
      "Provides access to basic contact information."

      first = TextLine(title=_(u"First name"))
      last = TextLine(title=_(u"Last name"))
      email = TextLine(title=_(u"Electronic mail address"))
      address = Text(title=_(u"Postal address"))
      postal_code = TextLine(title=_(u"Postal code"),
			    constraint=re.compile("\d{5,5}(-\d{4,4})?$").match)

      def name():
	  """Gets the contact name.

	  The contact name is the first and last name."""

In this example, we create a message ID factory and assign it to
_. By convention, we use _ as the name of our factory to be compatible
with translatable string extraction tools such as xgettext. We then
call _ with each string that needs to be translatable.  The resulting
message IDs can be used by a translation service.



=== Zope3/src/zope/i18n/__init__.py 1.2 => 1.3 ===
--- Zope3/src/zope/i18n/__init__.py:1.2	Wed Dec 25 09:13:39 2002
+++ Zope3/src/zope/i18n/__init__.py	Tue Mar 25 12:01:52 2003
@@ -1,2 +1,20 @@
+##############################################################################
 #
-# This file is necessary to make this directory a package.
+# Copyright (c) 2003 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.
+#
+##############################################################################
+"""i18n support.
+
+
+$Id$
+"""
+
+from zope.i18n.messageid import MessageIDFactory


=== Zope3/src/zope/i18n/globaltranslationservice.py 1.2 => 1.3 ===
--- Zope3/src/zope/i18n/globaltranslationservice.py:1.2	Wed Dec 25 09:13:39 2002
+++ Zope3/src/zope/i18n/globaltranslationservice.py	Tue Mar 25 12:01:52 2003
@@ -18,6 +18,7 @@
 
 from zope.i18n.negotiator import negotiator
 from zope.i18n.simpletranslationservice import SimpleTranslationService
+from zope.i18n.messageid import MessageID
 
 # The configure.zcml file should specify a list of fallback languages for the
 # site.  If a particular catalog for a negotiated language is not available,
@@ -74,6 +75,10 @@
                 langs = [m[0] for m in self._catalogs.keys()]
                 target_language = negotiator.getLanguage(langs, context)
 
+        # Try to get domain from msgid.
+        if isinstance(msgid, MessageID):
+            domain = msgid.domain
+            
         # Get the translation. Use the specified fallbacks if this fails
         catalog_names = self._catalogs.get((target_language, domain))
         if catalog_names is None:


=== Zope3/src/zope/i18n/interfaces.py 1.6 => 1.7 ===
--- Zope3/src/zope/i18n/interfaces.py:1.6	Tue Mar 25 09:48:01 2003
+++ Zope3/src/zope/i18n/interfaces.py	Tue Mar 25 12:01:52 2003
@@ -183,6 +183,26 @@
         """
 
 
+class IDomain(Interface):
+    """A translation domain.
+
+    Since it is often tedious to always specify a domain and a place for a
+    particular translation, the idea of a Domain object was created, which
+    allows to save the place and domain for a set of translations.
+
+    Usage:
+
+        domain = translationService.getDomain('domain')
+        domain.translate('MyProductTitle', context)
+    """
+
+    def translate(msgid, mapping=None, context=None, target_language=None):
+        """Translate the the source to its appropriate language.
+
+        See ITranslationService for details.
+        """
+
+
 class IWriteTranslationService(Interface):
     """This interface describes the methods that are necessary for an editable
     Translation Service to work.
@@ -278,7 +298,8 @@
         """
 
 
-class ITranslationService(IReadTranslationService, IWriteTranslationService,
+class ITranslationService(IReadTranslationService,
+                          IWriteTranslationService,
                           ISyncTranslationService):
     """This is the common and full-features translation service. Almost all
     translation service implementations will use this interface.
@@ -320,26 +341,6 @@
         """
 
 
-class IDomain(Interface):
-    """A translation domain.
-
-    Since it is often tedious to always specify a domain and a place for a
-    particular translation, the idea of a Domain object was created, which
-    allows to save the place and domain for a set of translations.
-
-    Usage:
-
-        domain = translationService.getDomain('domain')
-        domain.translate('MyProductTitle', context)
-    """
-
-    def translate(msgid, mapping=None, context=None, target_language=None):
-        """Translate the the source to its appropriate language.
-
-        See ITranslationService for details.
-        """
-
-
 class IMessageExportFilter(Interface):
     """The Export Filter for Translation Service Messages.
 
@@ -823,3 +824,5 @@
 
     calendar = Attribute("""This object must implement ILocaleCalendar. See
                             this interface's documentation for details.""")
+
+