summaryrefslogtreecommitdiffstats
path: root/Lib/gettext.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-06-20 15:06:45 (GMT)
committerGitHub <noreply@github.com>2017-06-20 15:06:45 (GMT)
commit29c89d00bf4b57c5ee2aafe660002ce1b8cea176 (patch)
treea7c667cc5e963260cf61f3d1faa5c2d6ca14aa8d /Lib/gettext.py
parent410860662f53945cddf5886801c5a88a84801fec (diff)
downloadcpython-29c89d00bf4b57c5ee2aafe660002ce1b8cea176.zip
cpython-29c89d00bf4b57c5ee2aafe660002ce1b8cea176.tar.gz
cpython-29c89d00bf4b57c5ee2aafe660002ce1b8cea176.tar.bz2
[3.5] bpo-29755: Fixed the lgettext() family of functions in the gettext module. (GH-2266) (#2298)
They now always return bytes. Updated the gettext documentation. (cherry picked from commit 26cb465)
Diffstat (limited to 'Lib/gettext.py')
-rw-r--r--Lib/gettext.py40
1 files changed, 23 insertions, 17 deletions
diff --git a/Lib/gettext.py b/Lib/gettext.py
index 57d2c74..6b215af 100644
--- a/Lib/gettext.py
+++ b/Lib/gettext.py
@@ -270,7 +270,9 @@ class NullTranslations:
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:
@@ -284,9 +286,12 @@ class NullTranslations:
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
@@ -368,7 +373,7 @@ class GNUTranslations(NullTranslations):
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
@@ -416,7 +421,7 @@ class GNUTranslations(NullTranslations):
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())
@@ -424,16 +429,16 @@ class GNUTranslations(NullTranslations):
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()
@@ -573,11 +578,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):
@@ -592,14 +597,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):