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 19 19 20 20 from datetime import date, datetime, time, timedelta 21 21 import gettext 22 import os 22 23 23 24 try: 24 25 set … … 281 282 def __setitem__(self, key, value): 282 283 self.value[key] = value 283 284 284 285 285 class Translations(gettext.GNUTranslations, object): 286 286 """An extended translation catalog class.""" 287 287 288 288 DEFAULT_DOMAIN = 'messages' 289 _loaded_domains = {} # loaded domains cache 289 290 290 291 def __init__(self, fileobj=None): 291 292 """Initialize the translations catalog. … … 294 295 from 295 296 """ 296 297 gettext.GNUTranslations.__init__(self, fp=fileobj) 298 297 299 self.files = filter(None, [getattr(fileobj, 'name', None)]) 300 self.locale = self.files[0].split(os.sep)[-3] 298 301 299 302 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN): 300 303 """Load translations from the given directory. … … 315 318 filename = gettext.find(domain or cls.DEFAULT_DOMAIN, dirname, locales) 316 319 if not filename: 317 320 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 319 324 load = classmethod(load) 320 325 321 326 def merge(self, translations): … … 334 339 self._catalog.update(translations._catalog) 335 340 self.files.extend(translations.files) 336 341 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) 337 379 338 380 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)
