Skip to content

Commit

Permalink
Merge pull request #835 from akx/gettext-deprecations
Browse files Browse the repository at this point in the history
Gettext deprecation fixes (for Python 3.11 compatibility)
  • Loading branch information
akx committed Apr 8, 2022
2 parents 5aa000e + 248fa35 commit d938794
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions babel/support.py
Expand Up @@ -319,6 +319,9 @@ def ldgettext(self, domain, message):
"""Like ``lgettext()``, but look the message up in the specified
domain.
"""
import warnings
warnings.warn('ldgettext() is deprecated, use dgettext() instead',
DeprecationWarning, 2)
return self._domains.get(domain, self).lgettext(message)

def udgettext(self, domain, message):
Expand All @@ -339,6 +342,9 @@ def ldngettext(self, domain, singular, plural, num):
"""Like ``lngettext()``, but look the message up in the specified
domain.
"""
import warnings
warnings.warn('ldngettext() is deprecated, use dngettext() instead',
DeprecationWarning, 2)
return self._domains.get(domain, self).lngettext(singular, plural, num)

def udngettext(self, domain, singular, plural, num):
Expand Down Expand Up @@ -378,16 +384,12 @@ def lpgettext(self, context, message):
preferred system encoding, if no other encoding was explicitly set with
``bind_textdomain_codeset()``.
"""
ctxt_msg_id = self.CONTEXT_ENCODING % (context, message)
missing = object()
tmsg = self._catalog.get(ctxt_msg_id, missing)
if tmsg is missing:
if self._fallback:
return self._fallback.lpgettext(context, message)
return message
if self._output_charset:
return tmsg.encode(self._output_charset)
return tmsg.encode(locale.getpreferredencoding())
import warnings
warnings.warn('lpgettext() is deprecated, use pgettext() instead',
DeprecationWarning, 2)
tmsg = self.pgettext(context, message)
encoding = getattr(self, "_output_charset", None) or locale.getpreferredencoding()
return tmsg.encode(encoding)

def npgettext(self, context, singular, plural, num):
"""Do a plural-forms lookup of a message id. `singular` is used as the
Expand Down Expand Up @@ -417,12 +419,14 @@ def lnpgettext(self, context, singular, plural, num):
preferred system encoding, if no other encoding was explicitly set with
``bind_textdomain_codeset()``.
"""
import warnings
warnings.warn('lnpgettext() is deprecated, use npgettext() instead',
DeprecationWarning, 2)
ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular)
try:
tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
if self._output_charset:
return tmsg.encode(self._output_charset)
return tmsg.encode(locale.getpreferredencoding())
encoding = getattr(self, "_output_charset", None) or locale.getpreferredencoding()
return tmsg.encode(encoding)
except KeyError:
if self._fallback:
return self._fallback.lnpgettext(context, singular, plural, num)
Expand Down

0 comments on commit d938794

Please sign in to comment.