diff options
author | Hanno Schlichting <hanno@hannosch.eu> | 2009-08-07 12:22:51 +0000 |
---|---|---|
committer | Hanno Schlichting <hanno@hannosch.eu> | 2009-08-07 12:22:51 +0000 |
commit | 4677dfd9a485ef150916263670b50ea740616651 (patch) | |
tree | e7fa4ac37133c2556520f5c283b871393ef34a36 | |
parent | fd0a76d4a71367354fad6c13f4860f6f2509f419 (diff) | |
download | zope-i18n-4677dfd9a485ef150916263670b50ea740616651.tar.gz |
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.
-rw-r--r-- | CHANGES.txt | 6 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_zcml.py | 2 | ||||
-rw-r--r-- | src/zope/i18n/zcml.py | 44 |
3 files changed, 40 insertions, 12 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 76f42fe..d1be2f4 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,6 +5,12 @@ CHANGES 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 diff --git a/src/zope/i18n/tests/test_zcml.py b/src/zope/i18n/tests/test_zcml.py index 97b23a7..66a8ca2 100644 --- a/src/zope/i18n/tests/test_zcml.py +++ b/src/zope/i18n/tests/test_zcml.py @@ -84,7 +84,7 @@ class DirectivesTest(PlacelessSetup, unittest.TestCase): 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"> diff --git a/src/zope/i18n/zcml.py b/src/zope/i18n/zcml.py index 6f6947c..cd601df 100644 --- a/src/zope/i18n/zcml.py +++ b/src/zope/i18n/zcml.py @@ -20,8 +20,9 @@ __docformat__ = 'restructuredtext' 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 @@ class IRegisterTranslationsDirective(Interface): 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 @@ def registerTranslations(_context, directory): # 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)) - - # make sure we have a TEST catalog for each domain: - domain.addCatalog(TestMessageCatalog(name)) + 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)) - 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)) |