Edgewall Software

Changeset 448

Show
Ignore:
Timestamp:
08/25/08 13:24:28 (10 months ago)
Author:
cmlenz
Message:

Add message domain support to the Translations class. Closes #137.

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/babel/support.py

    r442 r448  
    288288    DEFAULT_DOMAIN = 'messages' 
    289289 
    290     def __init__(self, fileobj=None): 
     290    def __init__(self, fileobj=None, domain=DEFAULT_DOMAIN): 
    291291        """Initialize the translations catalog. 
    292          
     292 
    293293        :param fileobj: the file-like object the translation should be read 
    294294                        from 
     
    296296        gettext.GNUTranslations.__init__(self, fp=fileobj) 
    297297        self.files = filter(None, [getattr(fileobj, 'name', None)]) 
     298        self.domain = domain 
     299        self._domains = {} 
    298300 
    299301    def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): 
    300302        """Load translations from the given directory. 
    301          
     303 
    302304        :param dirname: the directory containing the ``MO`` files 
    303305        :param locales: the list of locales in order of preference (items in 
     
    313315                locales = [locales] 
    314316            locales = [str(locale) for locale in locales] 
    315         filename = gettext.find(domain or cls.DEFAULT_DOMAIN, dirname, locales) 
     317        if not domain: 
     318            domain = cls.DEFAULT_DOMAIN 
     319        filename = gettext.find(domain, dirname, locales) 
    316320        if not filename: 
    317321            return gettext.NullTranslations() 
    318         return cls(fileobj=open(filename, 'rb')) 
     322        return cls(fileobj=open(filename, 'rb'), domain=domain) 
    319323    load = classmethod(load) 
     324 
     325    def __repr__(self): 
     326        return '<%s: "%s">' % (type(self).__name__, 
     327                               self._info.get('project-id-version')) 
     328 
     329    def add(self, translations, merge=True): 
     330        """Add the given translations to the catalog. 
     331 
     332        If the domain of the translations is different than that of the 
     333        current catalog, they are added as a catalog that is only accessible 
     334        by the various ``d*gettext`` functions. 
     335 
     336        :param translations: the `Translations` instance with the messages to 
     337                             add 
     338        :param merge: whether translations for message domains that have 
     339                      already been added should be merged with the existing 
     340                      translations 
     341        :return: the `Translations` instance (``self``) so that `merge` calls 
     342                 can be easily chained 
     343        :rtype: `Translations` 
     344        """ 
     345        domain = getattr(translations, 'domain', self.DEFAULT_DOMAIN) 
     346        if merge and domain == self.domain: 
     347            return self.merge(translations) 
     348 
     349        existing = self._domains.get(domain) 
     350        if merge and existing is not None: 
     351            existing.merge(translations) 
     352        else: 
     353            translations.add_fallback(self) 
     354            self._domains[domain] = translations 
     355 
     356        return self 
    320357 
    321358    def merge(self, translations): 
    322359        """Merge the given translations into the catalog. 
    323          
    324         Message translations in the specified catalog override any messages with 
    325         the same identifier in the existing catalog. 
    326          
     360 
     361        Message translations in the specified catalog override any messages 
     362        with the same identifier in the existing catalog. 
     363 
    327364        :param translations: the `Translations` instance with the messages to 
    328365                             merge 
     
    331368        :rtype: `Translations` 
    332369        """ 
    333         if isinstance(translations, Translations): 
     370        if isinstance(translations, gettext.GNUTranslations): 
    334371            self._catalog.update(translations._catalog) 
    335             self.files.extend(translations.files) 
     372            if isinstance(translations, Translations): 
     373                self.files.extend(translations.files) 
     374 
    336375        return self 
    337376 
    338     def __repr__(self): 
    339         return '<%s: "%s">' % (type(self).__name__, 
    340                                self._info.get('project-id-version')) 
     377    def dgettext(self, domain, message): 
     378        """Like ``gettext()``, but look the message up in the specified 
     379        domain. 
     380        """ 
     381        return self._domains.get(domain, self).gettext(message) 
     382     
     383    def ldgettext(self, domain, message): 
     384        """Like ``lgettext()``, but look the message up in the specified  
     385        domain. 
     386        """  
     387        return self._domains.get(domain, self).lgettext(message) 
     388     
     389    def dugettext(self, domain, message): 
     390        """Like ``ugettext()``, but look the message up in the specified 
     391        domain. 
     392        """ 
     393        return self._domains.get(domain, self).ugettext(message) 
     394     
     395    def dngettext(self, domain, singular, plural, num): 
     396        """Like ``ngettext()``, but look the message up in the specified 
     397        domain. 
     398        """ 
     399        return self._domains.get(domain, self).ngettext(singular, plural, num) 
     400     
     401    def ldngettext(self, domain, singular, plural, num): 
     402        """Like ``lngettext()``, but look the message up in the specified 
     403        domain. 
     404        """ 
     405        return self._domains.get(domain, self).lngettext(singular, plural, num) 
     406     
     407    def dungettext(self, domain, singular, plural, num): 
     408        """Like ``ungettext()`` but look the message up in the specified 
     409        domain. 
     410        """ 
     411        return self._domains.get(domain, self).ungettext(singular, plural, num) 
  • trunk/ChangeLog

    r446 r448  
    2424 * Fixed JavaScript extraction for regular expression literals (ticket #138) 
    2525   and concatenated strings. 
     26 * The `Translation` class in `babel.support` can now managed catalogs with 
     27   different message domains, and exposes the family of `d*gettext` functions 
     28   (ticket #137). 
    2629 
    2730