mirror of
https://github.com/python/cpython.git
synced 2025-11-01 14:11:41 +00:00
bpo-29755: Fixed the lgettext() family of functions in the gettext module. (#2266)
They now always return bytes. Updated the gettext documentation.
This commit is contained in:
parent
8457706ee3
commit
26cb4657bc
4 changed files with 235 additions and 113 deletions
|
|
@ -279,7 +279,9 @@ def gettext(self, message):
|
|||
def lgettext(self, message):
|
||||
if self._fallback:
|
||||
return self._fallback.lgettext(message)
|
||||
return message
|
||||
if self._output_charset:
|
||||
return message.encode(self._output_charset)
|
||||
return message.encode(locale.getpreferredencoding())
|
||||
|
||||
def ngettext(self, msgid1, msgid2, n):
|
||||
if self._fallback:
|
||||
|
|
@ -293,9 +295,12 @@ def lngettext(self, msgid1, msgid2, n):
|
|||
if self._fallback:
|
||||
return self._fallback.lngettext(msgid1, msgid2, n)
|
||||
if n == 1:
|
||||
return msgid1
|
||||
tmsg = msgid1
|
||||
else:
|
||||
return msgid2
|
||||
tmsg = msgid2
|
||||
if self._output_charset:
|
||||
return tmsg.encode(self._output_charset)
|
||||
return tmsg.encode(locale.getpreferredencoding())
|
||||
|
||||
def info(self):
|
||||
return self._info
|
||||
|
|
@ -377,7 +382,7 @@ def _parse(self, fp):
|
|||
if mlen == 0:
|
||||
# Catalog description
|
||||
lastk = None
|
||||
for b_item in tmsg.split('\n'.encode("ascii")):
|
||||
for b_item in tmsg.split(b'\n'):
|
||||
item = b_item.decode().strip()
|
||||
if not item:
|
||||
continue
|
||||
|
|
@ -425,7 +430,7 @@ def lgettext(self, message):
|
|||
if tmsg is missing:
|
||||
if self._fallback:
|
||||
return self._fallback.lgettext(message)
|
||||
return message
|
||||
tmsg = message
|
||||
if self._output_charset:
|
||||
return tmsg.encode(self._output_charset)
|
||||
return tmsg.encode(locale.getpreferredencoding())
|
||||
|
|
@ -433,16 +438,16 @@ def lgettext(self, message):
|
|||
def lngettext(self, msgid1, msgid2, n):
|
||||
try:
|
||||
tmsg = self._catalog[(msgid1, self.plural(n))]
|
||||
if self._output_charset:
|
||||
return tmsg.encode(self._output_charset)
|
||||
return tmsg.encode(locale.getpreferredencoding())
|
||||
except KeyError:
|
||||
if self._fallback:
|
||||
return self._fallback.lngettext(msgid1, msgid2, n)
|
||||
if n == 1:
|
||||
return msgid1
|
||||
tmsg = msgid1
|
||||
else:
|
||||
return msgid2
|
||||
tmsg = msgid2
|
||||
if self._output_charset:
|
||||
return tmsg.encode(self._output_charset)
|
||||
return tmsg.encode(locale.getpreferredencoding())
|
||||
|
||||
def gettext(self, message):
|
||||
missing = object()
|
||||
|
|
@ -582,11 +587,11 @@ def dgettext(domain, message):
|
|||
return t.gettext(message)
|
||||
|
||||
def ldgettext(domain, message):
|
||||
codeset = _localecodesets.get(domain)
|
||||
try:
|
||||
t = translation(domain, _localedirs.get(domain, None),
|
||||
codeset=_localecodesets.get(domain))
|
||||
t = translation(domain, _localedirs.get(domain, None), codeset=codeset)
|
||||
except OSError:
|
||||
return message
|
||||
return message.encode(codeset or locale.getpreferredencoding())
|
||||
return t.lgettext(message)
|
||||
|
||||
def dngettext(domain, msgid1, msgid2, n):
|
||||
|
|
@ -601,14 +606,15 @@ def dngettext(domain, msgid1, msgid2, n):
|
|||
return t.ngettext(msgid1, msgid2, n)
|
||||
|
||||
def ldngettext(domain, msgid1, msgid2, n):
|
||||
codeset = _localecodesets.get(domain)
|
||||
try:
|
||||
t = translation(domain, _localedirs.get(domain, None),
|
||||
codeset=_localecodesets.get(domain))
|
||||
t = translation(domain, _localedirs.get(domain, None), codeset=codeset)
|
||||
except OSError:
|
||||
if n == 1:
|
||||
return msgid1
|
||||
tmsg = msgid1
|
||||
else:
|
||||
return msgid2
|
||||
tmsg = msgid2
|
||||
return tmsg.encode(codeset or locale.getpreferredencoding())
|
||||
return t.lngettext(msgid1, msgid2, n)
|
||||
|
||||
def gettext(message):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue