[Checkins] SVN: zope.i18n/trunk/ Fixed the interpackage translation domain merging feature to actually work. We need to defer the merging into the ZCML handler execution phase, as the utilities don't exist yet during the ZCML parsing phase. Thx to Andreas Zeidler for finding and fixing the issue in PlacelessTranslationService in the first place.

Hanno Schlichting hannosch at hannosch.eu
Fri Aug 7 08:22:51 EDT 2009


Log message for revision 102560:
  Fixed the interpackage translation domain merging feature to actually work. We need to defer the merging into the ZCML handler execution phase, as the utilities don't exist yet during the ZCML parsing phase. Thx to Andreas Zeidler for finding and fixing the issue in PlacelessTranslationService in the first place.
  

Changed:
  U   zope.i18n/trunk/CHANGES.txt
  U   zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py
  U   zope.i18n/trunk/src/zope/i18n/zcml.py

-=-
Modified: zope.i18n/trunk/CHANGES.txt
===================================================================
--- zope.i18n/trunk/CHANGES.txt	2009-08-07 09:28:40 UTC (rev 102559)
+++ zope.i18n/trunk/CHANGES.txt	2009-08-07 12:22:51 UTC (rev 102560)
@@ -5,6 +5,12 @@
 3.7.1 (unreleased)
 ------------------
 
+- Fixed the interpackage translation domain merging feature to actually work.
+  We need to defer the merging into the ZCML handler execution phase, as the
+  utilities don't exist yet during the ZCML parsing phase. Thx to Andreas
+  Zeidler for finding and fixing the issue in PlacelessTranslationService in
+  the first place.
+
 - Fix translation domains translating a message for a different domain. In the
   process, fix testMessageIDTranslateForDifferentDomain which seemed to work by
   mistake as the "other" and "default" domains used the same catalog. This is

Modified: zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py	2009-08-07 09:28:40 UTC (rev 102559)
+++ zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py	2009-08-07 12:22:51 UTC (rev 102560)
@@ -84,7 +84,7 @@
 
     def testRegisterDistributedTranslations(self):
         from zope.configuration import xmlconfig
-        self.assert_(queryUtility(ITranslationDomain) is None)
+        self.assert_(queryUtility(ITranslationDomain, 'zope-i18n') is None)
         xmlconfig.string(
             template % '''
             <configure package="zope.i18n.tests">

Modified: zope.i18n/trunk/src/zope/i18n/zcml.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/zcml.py	2009-08-07 09:28:40 UTC (rev 102559)
+++ zope.i18n/trunk/src/zope/i18n/zcml.py	2009-08-07 12:22:51 UTC (rev 102560)
@@ -20,8 +20,9 @@
 
 import os
 
+from zope.component import getGlobalSiteManager
 from zope.component import queryUtility
-from zope.component.zcml import utility
+from zope.component.interface import provideInterface
 from zope.configuration.fields import Path
 from zope.interface import Interface
 
@@ -42,11 +43,27 @@
         required=True
         )
 
+
 def allow_language(lang):
     if config.ALLOWED_LANGUAGES is None:
         return True
     return lang in config.ALLOWED_LANGUAGES
 
+
+def handler(catalogs, name):
+    """ special handler handling the merging of two message catalogs """
+    gsm = getGlobalSiteManager()
+    # Try to get an existing domain and add the given catalogs to it
+    domain = queryUtility(ITranslationDomain, name)
+    if domain is None:
+        domain = TranslationDomain(name)
+        gsm.registerUtility(domain, ITranslationDomain, name=name)
+    for catalog in catalogs:
+        domain.addCatalog(catalog)
+    # make sure we have a TEST catalog for each domain:
+    domain.addCatalog(TestMessageCatalog(name))
+
+
 def registerTranslations(_context, directory):
     path = os.path.normpath(directory)
     domains = {}
@@ -75,15 +92,20 @@
 
     # Now create TranslationDomain objects and add them as utilities
     for name, langs in domains.items():
-        # Try to get an existing domain and add catalogs to it
-        domain = queryUtility(ITranslationDomain, name)
-        if domain is None:
-            domain = TranslationDomain(name)
-
+        catalogs = []
         for lang, file in langs.items():
-            domain.addCatalog(GettextMessageCatalog(lang, name, file))
+            catalogs.append(GettextMessageCatalog(lang, name, file))
+        # register the necessary actions directly (as opposed to using
+        # `zope.component.zcml.utility`) since we need the actual utilities
+        # in place before the merging can be done...
+        _context.action(
+            discriminator = None,
+            callable = handler,
+            args = (catalogs, name))
 
-        # make sure we have a TEST catalog for each domain:
-        domain.addCatalog(TestMessageCatalog(name))
-
-        utility(_context, ITranslationDomain, domain, name=name)
+    # also register the interface for the translation utilities
+    provides = ITranslationDomain
+    _context.action(
+        discriminator = None,
+        callable = provideInterface,
+        args = (provides.__module__ + '.' + provides.getName(), provides))



More information about the Checkins mailing list