summaryrefslogtreecommitdiffstats
path: root/Lib/gettext.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-06-20 15:06:35 (GMT)
committerGitHub <noreply@github.com>2017-06-20 15:06:35 (GMT)
commita1115e1a0454f0548f96cace6ee97b286dfa1c0d (patch)
tree6eabf055874cfa0b85cc44f128aedd753b9a0e51 /Lib/gettext.py
parent82acabd3c52508d9e3f83a41fe7c684619cbbe7b (diff)
downloadcpython-a1115e1a0454f0548f96cace6ee97b286dfa1c0d.zip
cpython-a1115e1a0454f0548f96cace6ee97b286dfa1c0d.tar.gz
cpython-a1115e1a0454f0548f96cace6ee97b286dfa1c0d.tar.bz2
[3.6] bpo-29755: Fixed the lgettext() family of functions in the gettext module. (GH-2266) (#2297)
They now always return bytes. Updated the gettext documentation.. (cherry picked from commit 26cb4657bcc9a7adffa95798ececb588dddfeadb)
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):