Commit 652f446d authored by Guido van Rossum's avatar Guido van Rossum

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.
parent 076da095
...@@ -292,8 +292,8 @@ class GNUTranslations(NullTranslations): ...@@ -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): ...@@ -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): ...@@ -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): ...@@ -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): ...@@ -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):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment