mirror of
https://github.com/python/cpython.git
synced 2025-08-28 12:45:07 +00:00
Fixes for str/uni/bytes for gettext.py. test_gettext.py passes.
Fix by Christian Heimes, SF# 1751958, who writes: I tested the fixes with the Zope3 zope.app.locales packages. The mo files are loaded and parsed w/o any problem. The translation with gettext.gettext is working as expected.
This commit is contained in:
parent
076da0957b
commit
652f446d42
1 changed files with 13 additions and 33 deletions
|
@ -292,8 +292,8 @@ class GNUTranslations(NullTranslations):
|
||||||
if mlen == 0:
|
if mlen == 0:
|
||||||
# Catalog description
|
# Catalog description
|
||||||
lastk = k = None
|
lastk = k = None
|
||||||
for item in tmsg.splitlines():
|
for b_item in tmsg.split(os.linesep):
|
||||||
item = item.strip()
|
item = str(b_item).strip()
|
||||||
if not item:
|
if not item:
|
||||||
continue
|
continue
|
||||||
if ':' in item:
|
if ':' in item:
|
||||||
|
@ -319,38 +319,30 @@ class GNUTranslations(NullTranslations):
|
||||||
# cause no problems since us-ascii should always be a subset of
|
# cause no problems since us-ascii should always be a subset of
|
||||||
# the charset encoding. We may want to fall back to 8-bit msgids
|
# the charset encoding. We may want to fall back to 8-bit msgids
|
||||||
# if the Unicode conversion fails.
|
# if the Unicode conversion fails.
|
||||||
if '\x00' in msg:
|
if b'\x00' in msg:
|
||||||
# Plural forms
|
# Plural forms
|
||||||
msgid1, msgid2 = msg.split('\x00')
|
msgid1, msgid2 = msg.split('\x00')
|
||||||
tmsg = tmsg.split('\x00')
|
tmsg = tmsg.split('\x00')
|
||||||
if self._charset:
|
if self._charset:
|
||||||
msgid1 = str(msgid1, self._charset)
|
msgid1 = str(msgid1, self._charset)
|
||||||
tmsg = [str(x, self._charset) for x in tmsg]
|
tmsg = [str(x, self._charset) for x in tmsg]
|
||||||
|
else:
|
||||||
|
msgid1 = str(msgid1)
|
||||||
|
tmsg = [str(x) for x in tmsg]
|
||||||
for i in range(len(tmsg)):
|
for i in range(len(tmsg)):
|
||||||
catalog[(msgid1, i)] = tmsg[i]
|
catalog[(msgid1, i)] = tmsg[i]
|
||||||
else:
|
else:
|
||||||
if self._charset:
|
if self._charset:
|
||||||
msg = str(msg, self._charset)
|
msg = str(msg, self._charset)
|
||||||
tmsg = str(tmsg, self._charset)
|
tmsg = str(tmsg, self._charset)
|
||||||
|
else:
|
||||||
|
msg = str(msg)
|
||||||
|
tmsg = str(tmsg)
|
||||||
catalog[msg] = tmsg
|
catalog[msg] = tmsg
|
||||||
# advance to next entry in the seek tables
|
# advance to next entry in the seek tables
|
||||||
masteridx += 8
|
masteridx += 8
|
||||||
transidx += 8
|
transidx += 8
|
||||||
|
|
||||||
def gettext(self, message):
|
|
||||||
missing = object()
|
|
||||||
tmsg = self._catalog.get(message, missing)
|
|
||||||
if tmsg is missing:
|
|
||||||
if self._fallback:
|
|
||||||
return self._fallback.gettext(message)
|
|
||||||
return message
|
|
||||||
# Encode the Unicode tmsg back to an 8-bit string, if possible
|
|
||||||
if self._output_charset:
|
|
||||||
return tmsg.encode(self._output_charset)
|
|
||||||
elif self._charset:
|
|
||||||
return tmsg.encode(self._charset)
|
|
||||||
return tmsg
|
|
||||||
|
|
||||||
def lgettext(self, message):
|
def lgettext(self, message):
|
||||||
missing = object()
|
missing = object()
|
||||||
tmsg = self._catalog.get(message, missing)
|
tmsg = self._catalog.get(message, missing)
|
||||||
|
@ -362,22 +354,6 @@ class GNUTranslations(NullTranslations):
|
||||||
return tmsg.encode(self._output_charset)
|
return tmsg.encode(self._output_charset)
|
||||||
return tmsg.encode(locale.getpreferredencoding())
|
return tmsg.encode(locale.getpreferredencoding())
|
||||||
|
|
||||||
def ngettext(self, msgid1, msgid2, n):
|
|
||||||
try:
|
|
||||||
tmsg = self._catalog[(msgid1, self.plural(n))]
|
|
||||||
if self._output_charset:
|
|
||||||
return tmsg.encode(self._output_charset)
|
|
||||||
elif self._charset:
|
|
||||||
return tmsg.encode(self._charset)
|
|
||||||
return tmsg
|
|
||||||
except KeyError:
|
|
||||||
if self._fallback:
|
|
||||||
return self._fallback.ngettext(msgid1, msgid2, n)
|
|
||||||
if n == 1:
|
|
||||||
return msgid1
|
|
||||||
else:
|
|
||||||
return msgid2
|
|
||||||
|
|
||||||
def lngettext(self, msgid1, msgid2, n):
|
def lngettext(self, msgid1, msgid2, n):
|
||||||
try:
|
try:
|
||||||
tmsg = self._catalog[(msgid1, self.plural(n))]
|
tmsg = self._catalog[(msgid1, self.plural(n))]
|
||||||
|
@ -401,6 +377,8 @@ class GNUTranslations(NullTranslations):
|
||||||
return str(message)
|
return str(message)
|
||||||
return tmsg
|
return tmsg
|
||||||
|
|
||||||
|
gettext = ugettext
|
||||||
|
|
||||||
def ungettext(self, msgid1, msgid2, n):
|
def ungettext(self, msgid1, msgid2, n):
|
||||||
try:
|
try:
|
||||||
tmsg = self._catalog[(msgid1, self.plural(n))]
|
tmsg = self._catalog[(msgid1, self.plural(n))]
|
||||||
|
@ -413,6 +391,8 @@ class GNUTranslations(NullTranslations):
|
||||||
tmsg = str(msgid2)
|
tmsg = str(msgid2)
|
||||||
return tmsg
|
return tmsg
|
||||||
|
|
||||||
|
ngettext = ungettext
|
||||||
|
|
||||||
|
|
||||||
# Locate a .mo file using the gettext strategy
|
# Locate a .mo file using the gettext strategy
|
||||||
def find(domain, localedir=None, languages=None, all=0):
|
def find(domain, localedir=None, languages=None, all=0):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue