diff options
author | Gediminas Paulauskas <menesis@pov.lt> | 2012-02-10 18:25:07 +0000 |
---|---|---|
committer | Gediminas Paulauskas <menesis@pov.lt> | 2012-02-10 18:25:07 +0000 |
commit | d207448a309a35a9076e9a3104c795bc8f0d40f2 (patch) | |
tree | 979585cb337755c8cb06dac49a02b5077f9bd710 | |
parent | b8fff2676a7575bf2d3248c562f778776e0603ce (diff) | |
download | zope-i18n-d207448a309a35a9076e9a3104c795bc8f0d40f2.tar.gz |
Added optional ``domain`` attribute to ``registerTranslations`` directive to
only load the specified translation domain. Allows to move catalogs to
`/usr/share/locale` and avoid loading hundreds of unrelated domains.
-rw-r--r-- | CHANGES.txt | 7 | ||||
-rw-r--r-- | setup.py | 2 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_zcml.py | 18 | ||||
-rw-r--r-- | src/zope/i18n/zcml.py | 34 |
4 files changed, 47 insertions, 14 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 2e56f39..10644ff 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -2,15 +2,20 @@ CHANGES ======= -3.7.5 (unreleased) +3.8.0 (unreleased) ------------------ +- Added optional ``domain`` attribute to ``registerTranslations`` directive to + only load the specified translation domain. Allows to move catalogs to + `/usr/share/locale` and avoid loading hundreds of unrelated domains. + - Include meta.zcml files in our own zcml configuration as needed, added a test for our configure.zcml. - Update zope.i18n.NAME_RE to be identical to zope.tal as required by the comment next to it. Fixes #611746. + 3.7.4 (2010-07-08) ------------------ @@ -27,7 +27,7 @@ def read(*rnames): setup( name='zope.i18n', - version='3.7.5dev', + version='3.8.0dev', author='Zope Foundation and Contributors', author_email='zope-dev@zope.org', description='Zope Internationalization Support', diff --git a/src/zope/i18n/tests/test_zcml.py b/src/zope/i18n/tests/test_zcml.py index 60fbf30..c32e087 100644 --- a/src/zope/i18n/tests/test_zcml.py +++ b/src/zope/i18n/tests/test_zcml.py @@ -153,6 +153,24 @@ class DirectivesTest(PlacelessSetup, unittest.TestCase): # Reset the mtime of the mo file os.utime(path, (path_atime, path_mtime)) + def testRegisterTranslationsForDomain(self): + from zope.configuration import xmlconfig + self.assert_(queryUtility(ITranslationDomain, 'zope-i18n') is None) + self.assert_(queryUtility(ITranslationDomain, 'zope-i18n2') is None) + xmlconfig.string( + template % ''' + <configure package="zope.i18n.tests"> + <i18n:registerTranslations directory="locale3" domain="zope-i18n" /> + </configure> + ''', self.context) + path = os.path.join(os.path.dirname(zope.i18n.tests.__file__), + 'locale3', 'en', 'LC_MESSAGES', 'zope-i18n.mo') + util = getUtility(ITranslationDomain, 'zope-i18n') + self.assertEquals(util._catalogs, + {'test': ['test'], 'en': [unicode(path)]}) + + self.assert_(queryUtility(ITranslationDomain, 'zope-i18n2') is None) + def test_suite(): return unittest.TestSuite(( diff --git a/src/zope/i18n/zcml.py b/src/zope/i18n/zcml.py index 30f9496..018f851 100644 --- a/src/zope/i18n/zcml.py +++ b/src/zope/i18n/zcml.py @@ -17,12 +17,14 @@ __docformat__ = 'restructuredtext' import os +from glob import glob from zope.component import getSiteManager from zope.component import queryUtility from zope.component.interface import provideInterface from zope.configuration.fields import Path from zope.interface import Interface +from zope.schema import TextLine from zope.i18n import config from zope.i18n.compile import compile_mo_file @@ -41,6 +43,13 @@ class IRegisterTranslationsDirective(Interface): required=True ) + domain = TextLine( + title=u"Domain", + description=u"Translation domain to register. If not specified, " + "all domains found in the directory are registered", + required=False + ) + def allow_language(lang): if config.ALLOWED_LANGUAGES is None: @@ -62,7 +71,7 @@ def handler(catalogs, name): domain.addCatalog(TestMessageCatalog(name)) -def registerTranslations(_context, directory): +def registerTranslations(_context, directory, domain='*'): path = os.path.normpath(directory) domains = {} @@ -76,17 +85,18 @@ def registerTranslations(_context, directory): if os.path.isdir(lc_messages_path): # Preprocess files and update or compile the mo files if config.COMPILE_MO_FILES: - for domain_file in os.listdir(lc_messages_path): - if domain_file.endswith('.po'): - domain = domain_file[:-3] - compile_mo_file(domain, lc_messages_path) - for domain_file in os.listdir(lc_messages_path): - if domain_file.endswith('.mo'): - domain_path = os.path.join(lc_messages_path, domain_file) - domain = domain_file[:-3] - if not domain in domains: - domains[domain] = {} - domains[domain][language] = domain_path + for domain_path in glob(os.path.join(lc_messages_path, + '%s.po' % domain)): + domain_file = os.path.basename(domain_path) + name = domain_file[:-3] + compile_mo_file(name, lc_messages_path) + for domain_path in glob(os.path.join(lc_messages_path, + '%s.mo' % domain)): + domain_file = os.path.basename(domain_path) + name = domain_file[:-3] + if not name in domains: + domains[name] = {} + domains[name][language] = domain_path # Now create TranslationDomain objects and add them as utilities for name, langs in domains.items(): |