[Checkins] SVN: zope.i18n/trunk/ Added optional ``domain`` attribute to ``registerTranslations`` directive to

Gediminas Paulauskas menesis at pov.lt
Fri Feb 10 18:25:09 UTC 2012


Log message for revision 124371:
  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.
  
  
  

Changed:
  U   zope.i18n/trunk/CHANGES.txt
  U   zope.i18n/trunk/setup.py
  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	2012-02-10 17:22:15 UTC (rev 124370)
+++ zope.i18n/trunk/CHANGES.txt	2012-02-10 18:25:07 UTC (rev 124371)
@@ -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)
 ------------------
 

Modified: zope.i18n/trunk/setup.py
===================================================================
--- zope.i18n/trunk/setup.py	2012-02-10 17:22:15 UTC (rev 124370)
+++ zope.i18n/trunk/setup.py	2012-02-10 18:25:07 UTC (rev 124371)
@@ -27,7 +27,7 @@
 
 setup(
     name='zope.i18n',
-    version='3.7.5dev',
+    version='3.8.0dev',
     author='Zope Foundation and Contributors',
     author_email='zope-dev at zope.org',
     description='Zope Internationalization Support',

Modified: zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py	2012-02-10 17:22:15 UTC (rev 124370)
+++ zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py	2012-02-10 18:25:07 UTC (rev 124371)
@@ -153,7 +153,25 @@
         # 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((
             unittest.makeSuite(DirectivesTest),

Modified: zope.i18n/trunk/src/zope/i18n/zcml.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/zcml.py	2012-02-10 17:22:15 UTC (rev 124370)
+++ zope.i18n/trunk/src/zope/i18n/zcml.py	2012-02-10 18:25:07 UTC (rev 124371)
@@ -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,7 +43,14 @@
         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:
         return True
@@ -62,7 +71,7 @@
     domain.addCatalog(TestMessageCatalog(name))
 
 
-def registerTranslations(_context, directory):
+def registerTranslations(_context, directory, domain='*'):
     path = os.path.normpath(directory)
     domains = {}
 
@@ -76,17 +85,18 @@
         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():



More information about the checkins mailing list