[Zope3-checkins] CVS: Zope3/src/zope/i18n - gettextmessagecatalog.py:1.5
Barry Warsaw
barry@wooz.org
Fri, 11 Apr 2003 15:15:55 -0400
Update of /cvs-repository/Zope3/src/zope/i18n
In directory cvs.zope.org:/tmp/cvs-serv13432/src/zope/i18n
Modified Files:
gettextmessagecatalog.py
Log Message:
Slightly modify the way errors are handled internally by using a
fallback that raises the expected KeyError. The old way could have
gotten a KeyError if the message id was translated but just happened
to be the same value as the original. This way really tests for a
missing translation.
Also, create the GNUTranslations w/ coerce=True so all message ids and
strings are Unicode-ified.
=== Zope3/src/zope/i18n/gettextmessagecatalog.py 1.4 => 1.5 ===
--- Zope3/src/zope/i18n/gettextmessagecatalog.py:1.4 Tue Mar 25 18:25:14 2003
+++ Zope3/src/zope/i18n/gettextmessagecatalog.py Fri Apr 11 15:15:54 2003
@@ -16,48 +16,42 @@
$Id$
"""
-from gettext import GNUTranslations
+from pythonlib.gettext import GNUTranslations
from zope.i18n.interfaces import IMessageCatalog
+class _KeyErrorRaisingFallback:
+ def ugettext(self, message):
+ raise KeyError, message
+
+
class GettextMessageCatalog:
- """ """
+ """A message catalog based on GNU gettext and Python's gettext module."""
__implements__ = IMessageCatalog
-
def __init__(self, language, domain, path_to_file):
"""Initialize the message catalog"""
self._language = language
self._domain = domain
self._path_to_file = path_to_file
- self.__translation_object = None
- self._prepareTranslations()
-
-
- def _prepareTranslations(self):
- """ """
- if self.__translation_object is None:
- file = open(self._path_to_file, 'r')
- self.__translation_object = GNUTranslations(file)
- file.close()
-
+ fp = open(self._path_to_file, 'r')
+ try:
+ self._catalog = GNUTranslations(fp, coerce=True)
+ finally:
+ fp.close()
+ self._catalog.add_fallback(_KeyErrorRaisingFallback())
def getMessage(self, id):
'See IMessageCatalog'
- self._prepareTranslations()
- msg = self.__translation_object.ugettext(id)
- if msg == id:
- raise KeyError
- return msg
+ return self._catalog.ugettext(id)
def queryMessage(self, id, default=None):
'See IMessageCatalog'
- self._prepareTranslations()
- text = self.__translation_object.ugettext(id)
- if text != id:
- return text
- return default
+ try:
+ return self._catalog.ugettext(id)
+ except KeyError:
+ return default
def getLanguage(self):
'See IMessageCatalog'