diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-06-20 14:13:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-20 14:13:29 (GMT) |
commit | 26cb4657bcc9a7adffa95798ececb588dddfeadb (patch) | |
tree | e1ebdeb6ee1f428a739a50933ffef3e6ee141aa8 /Lib/gettext.py | |
parent | 8457706ee308a621103e9b9c760ca9da3cc4e7c0 (diff) | |
download | cpython-26cb4657bcc9a7adffa95798ececb588dddfeadb.zip cpython-26cb4657bcc9a7adffa95798ececb588dddfeadb.tar.gz cpython-26cb4657bcc9a7adffa95798ececb588dddfeadb.tar.bz2 |
bpo-29755: Fixed the lgettext() family of functions in the gettext module. (#2266)
They now always return bytes.
Updated the gettext documentation.
Diffstat (limited to 'Lib/gettext.py')
-rw-r--r-- | Lib/gettext.py | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/Lib/gettext.py b/Lib/gettext.py index 08d051b..5ad7ff6 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -279,7 +279,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: @@ -293,9 +295,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 @@ -377,7 +382,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 @@ -425,7 +430,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()) @@ -433,16 +438,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() @@ -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): |