[Checkins] SVN: zope.i18n/branches/lazy-tds/src/zope/i18n/ Fixed support for the ``zope_i18n_compile_mo_files`` environ flag.

Malthe Borch cvs-admin at zope.org
Thu Oct 11 09:25:37 UTC 2012


Log message for revision 127962:
  Fixed support for the ``zope_i18n_compile_mo_files`` environ flag.

Changed:
  U   zope.i18n/branches/lazy-tds/src/zope/i18n/compile.py
  U   zope.i18n/branches/lazy-tds/src/zope/i18n/tests/test_zcml.py
  U   zope.i18n/branches/lazy-tds/src/zope/i18n/translationdomain.py
  U   zope.i18n/branches/lazy-tds/src/zope/i18n/zcml.py

-=-
Modified: zope.i18n/branches/lazy-tds/src/zope/i18n/compile.py
===================================================================
--- zope.i18n/branches/lazy-tds/src/zope/i18n/compile.py	2012-10-10 14:32:57 UTC (rev 127961)
+++ zope.i18n/branches/lazy-tds/src/zope/i18n/compile.py	2012-10-11 09:25:33 UTC (rev 127962)
@@ -13,30 +13,40 @@
 logger = logging.getLogger('zope.i18n')
 
 
-def compile_mo_file(domain, lc_messages_path):
+def compile_mo_file(domain, lc_messages_path, msgfmt=True):
     """Creates or updates a mo file in the locales folder."""
     if not HAS_PYTHON_GETTEXT:
         logger.critical("Unable to compile messages: Python `gettext` library missing.")
         return
 
     base = join(lc_messages_path, domain)
-    pofile = str(base + '.po')
+
     mofile = str(base + '.mo')
 
-    po_mtime = 0
-    try:
-        po_mtime = os.stat(pofile)[ST_MTIME]
-    except (IOError, OSError):
-        return
+    mo_mtime = 0
 
-    mo_mtime = 0
     if os.path.exists(mofile):
         # Update mo file?
         try:
             mo_mtime = os.stat(mofile)[ST_MTIME]
         except (IOError, OSError):
-            return
+            pass
+        else:
+            if not msgfmt:
+                return mofile
+    elif not msgfmt:
+        return
 
+    pofile = str(base + '.po')
+    po_mtime = 0
+    try:
+        po_mtime = os.stat(pofile)[ST_MTIME]
+    except (IOError, OSError):
+        if mo_mtime:
+            return mofile
+
+        return
+
     if po_mtime > mo_mtime:
         try:
             mo = Msgfmt(pofile, domain).getAsFile()

Modified: zope.i18n/branches/lazy-tds/src/zope/i18n/tests/test_zcml.py
===================================================================
--- zope.i18n/branches/lazy-tds/src/zope/i18n/tests/test_zcml.py	2012-10-10 14:32:57 UTC (rev 127961)
+++ zope.i18n/branches/lazy-tds/src/zope/i18n/tests/test_zcml.py	2012-10-11 09:25:33 UTC (rev 127962)
@@ -43,7 +43,16 @@
         from zope.configuration import xmlconfig
         super(DirectivesTest, self).setUp()
         self.context = xmlconfig.file('meta.zcml', zope.i18n)
+        self.allowed = config.ALLOWED_LANGUAGES
+        self.compiled = config.COMPILE_MO_FILES
+        config.ALLOWED_LANGUAGES = None
+        config.COMPILE_MO_FILES = False
 
+    def tearDown(self):
+        super(DirectivesTest, self).tearDown()
+        config.ALLOWED_LANGUAGES = self.allowed
+        config.COMPILE_MO_FILES = self.compiled
+
     def testRegisterTranslations(self):
         from zope.configuration import xmlconfig
         self.assert_(queryUtility(ITranslationDomain) is None)

Modified: zope.i18n/branches/lazy-tds/src/zope/i18n/translationdomain.py
===================================================================
--- zope.i18n/branches/lazy-tds/src/zope/i18n/translationdomain.py	2012-10-10 14:32:57 UTC (rev 127961)
+++ zope.i18n/branches/lazy-tds/src/zope/i18n/translationdomain.py	2012-10-11 09:25:33 UTC (rev 127962)
@@ -13,12 +13,15 @@
 ##############################################################################
 """Global Translation Service for providing I18n to file-based code.
 """
+import logging
 import zope.component
+
 from zope.i18nmessageid import Message
 from zope.i18n import translate, interpolate
 from zope.i18n.simpletranslationdomain import SimpleTranslationDomain
 from zope.i18n.interfaces import INegotiator
 from zope.i18n.compile import compile_mo_file
+from zope.i18n import config
 from zope.i18n.gettextmessagecatalog import GettextMessageCatalog
 
 # The configuration should specify a list of fallback languages for the
@@ -31,7 +34,9 @@
 # message in a catalog is not translated, tough luck, you get the msgid.
 LANGUAGE_FALLBACKS = ['en']
 
+logger = logging.getLogger("zope.i18n")
 
+
 class TranslationDomain(SimpleTranslationDomain):
     languages = ()
 
@@ -70,9 +75,19 @@
         self._fallbacks = fallbacks
 
     def importCatalog(self, lang, lc_messages_path):
-        path = compile_mo_file(self.domain, lc_messages_path)
-        catalog = GettextMessageCatalog(lang, self.domain, path)
-        self.addCatalog(catalog)
+        path = compile_mo_file(
+            self.domain, lc_messages_path,
+            config.COMPILE_MO_FILES
+        )
+        if path is None:
+            logger.warn(
+                "could not load or compile message catalog for "
+                "language %r in %r." % (
+                    lang, lc_messages_path
+                ))
+        else:
+            catalog = GettextMessageCatalog(lang, self.domain, path)
+            self.addCatalog(catalog)
 
     def translate(self, msgid, mapping=None, context=None,
                   target_language=None, default=None):

Modified: zope.i18n/branches/lazy-tds/src/zope/i18n/zcml.py
===================================================================
--- zope.i18n/branches/lazy-tds/src/zope/i18n/zcml.py	2012-10-10 14:32:57 UTC (rev 127961)
+++ zope.i18n/branches/lazy-tds/src/zope/i18n/zcml.py	2012-10-11 09:25:33 UTC (rev 127962)
@@ -87,7 +87,11 @@
 
         lc_messages_path = os.path.join(path, language, 'LC_MESSAGES')
         if os.path.isdir(lc_messages_path):
-            query = os.path.join(lc_messages_path, '%s.[pm]o' % domain)
+            if config.COMPILE_MO_FILES:
+                glob_format = '%s.[pm]o'
+            else:
+                glob_format = '%s.mo'
+            query = os.path.join(lc_messages_path, glob_format % domain)
             for domain_path in glob(query):
                 loaded = True
                 base, ext = os.path.splitext(domain_path)



More information about the checkins mailing list