[Zope-Checkins] CVS: Zope3/lib/python/Zope/I18n - GlobalTranslationService.py:1.2

Barry Warsaw barry@wooz.org
Wed, 12 Jun 2002 16:55:24 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/I18n
In directory cvs.zope.org:/tmp/cvs-serv10397/lib/python/Zope/I18n

Modified Files:
	GlobalTranslationService.py 
Log Message:
Clean up some code, get rid of empty docstrings.

__init__(): Squirrel away the default domain, even though we don't
currently specify that ITranslationServices have a default domain.
Comment what the internal dictionaries map.

_registerMessageCatalog(): Simply through the use of {}.setdefault()

translate(): Fix the calculation of target_language when context is
non-None, and simplify by using a list comprehension.


=== Zope3/lib/python/Zope/I18n/GlobalTranslationService.py 1.1 => 1.2 ===
 
 class GlobalTranslationService(SimpleTranslationService):
-    ''' '''
 
     __implements__ =  SimpleTranslationService.__implements__
 
-
     def __init__(self, default_domain='global'):
-        ''' '''
+        # XXX We haven't specified that ITranslationServices have a default
+        # domain.  So far, we've required the domain argument to .translate()
+        self._domain = default_domain
+        # _catalogs maps (language, domain) to IMessageCatalog instances
         self._catalogs = {}
+        # _data maps IMessageCatalog.getIdentifier() to IMessageCatalog
         self._data = {}
 
-
     def _registerMessageCatalog(self, language, domain, catalog_name):
-        ''' '''
-        if (language, domain) not in self._catalogs.keys():
-            self._catalogs[(language, domain)] = []
-
-        mc = self._catalogs[(language, domain)]
+        key = (language, domain)
+        mc = self._catalogs.setdefault(key, [])
         mc.append(catalog_name)
 
-
     def addCatalog(self, catalog):
-        ''' '''
         self._data[catalog.getIdentifier()] = catalog
         self._registerMessageCatalog(catalog.getLanguage(),
                                      catalog.getDomain(),
@@ -56,20 +52,12 @@
     def translate(self, domain, msgid, mapping=None, context=None,  
                   target_language=None):
         '''See interface ITranslationService'''
-
-        if domain is None:
-            domain = self.default_domain
-
         if target_language is None:
             if context is None:
                 raise TypeError, 'No destination language'
             else:
-                avail_langs = {}
-                for catalog in self._data.values():
-                    avail_langs[catalog] = None
-
-                target_language = negotiator.getLanguage(avail_langs.keys(),
-                                                         context)
+                langs = [m[0] for m in self._catalogs.keys()]
+                target_language = negotiator.getLanguage(langs, context)
 
         # Get the translation. Default is the msgid text itself.
         catalog_names = self._catalogs.get((target_language, domain), [])