diff options
Diffstat (limited to 'src/zope')
-rw-r--r-- | src/zope/i18n/compile.py | 28 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_zcml.py | 9 | ||||
-rw-r--r-- | src/zope/i18n/translationdomain.py | 21 | ||||
-rw-r--r-- | src/zope/i18n/zcml.py | 6 |
4 files changed, 51 insertions, 13 deletions
diff --git a/src/zope/i18n/compile.py b/src/zope/i18n/compile.py index b2a6ba3..028e716 100644 --- a/src/zope/i18n/compile.py +++ b/src/zope/i18n/compile.py @@ -13,29 +13,39 @@ except ImportError: 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 + mofile = str(base + '.mo') 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: diff --git a/src/zope/i18n/tests/test_zcml.py b/src/zope/i18n/tests/test_zcml.py index d4574c8..63d6ffe 100644 --- a/src/zope/i18n/tests/test_zcml.py +++ b/src/zope/i18n/tests/test_zcml.py @@ -43,6 +43,15 @@ class DirectivesTest(PlacelessSetup, unittest.TestCase): 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 diff --git a/src/zope/i18n/translationdomain.py b/src/zope/i18n/translationdomain.py index 2faa301..21690c3 100644 --- a/src/zope/i18n/translationdomain.py +++ b/src/zope/i18n/translationdomain.py @@ -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,6 +34,8 @@ from zope.i18n.gettextmessagecatalog import GettextMessageCatalog # 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 @@ class TranslationDomain(SimpleTranslationDomain): 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): diff --git a/src/zope/i18n/zcml.py b/src/zope/i18n/zcml.py index aa329ae..cb01d21 100644 --- a/src/zope/i18n/zcml.py +++ b/src/zope/i18n/zcml.py @@ -87,7 +87,11 @@ def registerTranslations(_context, directory, domain='*'): 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) |