Edgewall Software

Ticket #137: translations-dgettext.diff

File translations-dgettext.diff, 5.1 KB (added by cmlenz, 3 years ago)

Slightly cleaned up patch

  • babel/support.py

     
    287287 
    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 
    295295        """ 
    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 
    304306                        this list can be either `Locale` objects or locale 
     
    312314            if not isinstance(locales, (list, tuple)): 
    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) 
    320324 
     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        if merge and translations.domain == self.domain: 
     346            return self.merge(translations) 
     347 
     348        existing = self._domains.get(translations.domain) 
     349        if merge and existing is not None: 
     350            existing.merge(translations) 
     351        else: 
     352            translations.set_fallback(self) 
     353            self._domains[translations.domain] = translations 
     354 
     355        return self 
     356 
    321357    def merge(self, translations): 
    322358        """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          
     359 
     360        Message translations in the specified catalog override any messages 
     361        with the same identifier in the existing catalog. 
     362 
    327363        :param translations: the `Translations` instance with the messages to 
    328364                             merge 
    329365        :return: the `Translations` instance (``self``) so that `merge` calls 
     
    335371            self.files.extend(translations.files) 
    336372        return self 
    337373 
    338     def __repr__(self): 
    339         return '<%s: "%s">' % (type(self).__name__, 
    340                                self._info.get('project-id-version')) 
     374    def dgettext(self, domain, message): 
     375        """Like ``gettext()``, but look the message up in the specified 
     376        domain. 
     377        """ 
     378        return self._domains.get(domain, self).gettext(message) 
     379     
     380    def ldgettext(self, domain, message): 
     381        """Like ``lgettext()``, but look the message up in the specified  
     382        domain. 
     383        """  
     384        return self._domains.get(domain, self).lgettext(message) 
     385     
     386    def dugettext(self, domain, message): 
     387        """Like ``ugettext()``, but look the message up in the specified 
     388        domain. 
     389        """ 
     390        return self._domains.get(domain, self).ugettext(message) 
     391     
     392    def dngettext(self, domain, singular, plural, num): 
     393        """Like ``ngettext()``, but look the message up in the specified 
     394        domain. 
     395        """ 
     396        return self._domains.get(domain, self).ngettext(singular, plural, num) 
     397     
     398    def ldngettext(self, domain, singular, plural, num): 
     399        """Like ``lngettext()``, but look the message up in the specified 
     400        domain. 
     401        """ 
     402        return self._domains.get(domain, self).lngettext(singular, plural, num) 
     403     
     404    def dungettext(self, domain, singular, plural, num): 
     405        """Like ``ungettext()`` but look the message up in the specified 
     406        domain. 
     407        """ 
     408        return self._domains.get(domain, self).ungettext(singular, plural, num)