Edgewall Software

Ticket #137: support_domain_calls.patch

File support_domain_calls.patch, 3.6 KB (added by palgarvio, 4 years ago)
  • babel/support.py

    diff --git a/babel/support.py b/babel/support.py
    a b  
    1919 
    2020from datetime import date, datetime, time, timedelta 
    2121import gettext 
     22import os 
    2223 
    2324try: 
    2425    set 
     
    281282    def __setitem__(self, key, value): 
    282283        self.value[key] = value 
    283284 
    284      
    285285class Translations(gettext.GNUTranslations, object): 
    286286    """An extended translation catalog class.""" 
    287287 
    288288    DEFAULT_DOMAIN = 'messages' 
     289    _loaded_domains = {} # loaded domains cache 
    289290 
    290291    def __init__(self, fileobj=None): 
    291292        """Initialize the translations catalog. 
     
    294295                        from 
    295296        """ 
    296297        gettext.GNUTranslations.__init__(self, fp=fileobj) 
     298         
    297299        self.files = filter(None, [getattr(fileobj, 'name', None)]) 
     300        self.locale = self.files[0].split(os.sep)[-3] 
    298301 
    299302    def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): 
    300303        """Load translations from the given directory. 
     
    315318        filename = gettext.find(domain or cls.DEFAULT_DOMAIN, dirname, locales) 
    316319        if not filename: 
    317320            return gettext.NullTranslations() 
    318         return cls(fileobj=open(filename, 'rb')) 
     321        instance = cls(fileobj=open(filename, 'rb')) 
     322        instance.default_domain = domain or cls.DEFAULT_DOMAIN 
     323        return instance 
    319324    load = classmethod(load) 
    320325 
    321326    def merge(self, translations): 
     
    334339            self._catalog.update(translations._catalog) 
    335340            self.files.extend(translations.files) 
    336341        return self 
     342     
     343    def add_domain(self, domain, dirname): 
     344        """Add a new domain to the translations object so that domain gettext 
     345        calls can be used.""" 
     346        translation = Translations.load(dirname, self.locale, domain) 
     347        # Have the current domain as a fallback 
     348        translation.add_fallback(self) 
     349        # Store it 
     350        self._loaded_domains[domain] = translation 
     351         
     352    def dgettext(self, domain, message): 
     353        """Like gettext() but look the message up in the specified domain.""" 
     354        return self._domain_call('gettext', domain, message) 
     355     
     356    def ldgettext(self, domain, message): 
     357        """Like lgettext() but look the message up in the specified domain."""  
     358        return self._domain_call('lgettext', domain, message) 
     359     
     360    def dugettext(self, domain, message): 
     361        """Like ugettext() but look the message up in the specified domain.""" 
     362        return self._domain_call('ugettext', domain, message) 
     363     
     364    def dngettext(self, domain, singular, plural, num): 
     365        """Like ngettext() but look the message up in the specified domain.""" 
     366        return self._domain_call('ngettext', domain, singular, plural, num) 
     367     
     368    def ldngettext(self, domain, singular, plural, num): 
     369        """Like lngettext() but look the message up in the specified domain.""" 
     370        return self._domain_call('lngettext', domain, singular, plural, num) 
     371     
     372    def dungettext(self, domain, singular, plural, num): 
     373        """Like ungettext() but look the message up in the specified domain.""" 
     374        return self._domain_call('ungettext', domain, singular, plural, num) 
     375     
     376    def _domain_call(self, func, domain, *args, **kwargs): 
     377        return getattr(self._loaded_domains.get(domain, self), 
     378                       func)(*args, **kwargs) 
    337379 
    338380    def __repr__(self): 
    339         return '<%s: "%s">' % (type(self).__name__, 
    340                                self._info.get('project-id-version')) 
     381        return '<%s "%s": "%s">' % (type(self).__name__, 
     382                                    self.info().get('project-id-version'), 
     383                                    self.locale)