Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 652f446

Browse files
committed
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.
1 parent 076da09 commit 652f446

1 file changed

Lines changed: 13 additions & 33 deletions

File tree

Lib/gettext.py

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ def _parse(self, fp):
292292
if mlen == 0:
293293
# Catalog description
294294
lastk = k = None
295-
for item in tmsg.splitlines():
296-
item = item.strip()
295+
for b_item in tmsg.split(os.linesep):
296+
item = str(b_item).strip()
297297
if not item:
298298
continue
299299
if ':' in item:
@@ -319,38 +319,30 @@ def _parse(self, fp):
319319
# cause no problems since us-ascii should always be a subset of
320320
# the charset encoding. We may want to fall back to 8-bit msgids
321321
# if the Unicode conversion fails.
322-
if '\x00' in msg:
322+
if b'\x00' in msg:
323323
# Plural forms
324324
msgid1, msgid2 = msg.split('\x00')
325325
tmsg = tmsg.split('\x00')
326326
if self._charset:
327327
msgid1 = str(msgid1, self._charset)
328328
tmsg = [str(x, self._charset) for x in tmsg]
329+
else:
330+
msgid1 = str(msgid1)
331+
tmsg = [str(x) for x in tmsg]
329332
for i in range(len(tmsg)):
330333
catalog[(msgid1, i)] = tmsg[i]
331334
else:
332335
if self._charset:
333336
msg = str(msg, self._charset)
334337
tmsg = str(tmsg, self._charset)
338+
else:
339+
msg = str(msg)
340+
tmsg = str(tmsg)
335341
catalog[msg] = tmsg
336342
# advance to next entry in the seek tables
337343
masteridx += 8
338344
transidx += 8
339345

340-
def gettext(self, message):
341-
missing = object()
342-
tmsg = self._catalog.get(message, missing)
343-
if tmsg is missing:
344-
if self._fallback:
345-
return self._fallback.gettext(message)
346-
return message
347-
# Encode the Unicode tmsg back to an 8-bit string, if possible
348-
if self._output_charset:
349-
return tmsg.encode(self._output_charset)
350-
elif self._charset:
351-
return tmsg.encode(self._charset)
352-
return tmsg
353-
354346
def lgettext(self, message):
355347
missing = object()
356348
tmsg = self._catalog.get(message, missing)
@@ -362,22 +354,6 @@ def lgettext(self, message):
362354
return tmsg.encode(self._output_charset)
363355
return tmsg.encode(locale.getpreferredencoding())
364356

365-
def ngettext(self, msgid1, msgid2, n):
366-
try:
367-
tmsg = self._catalog[(msgid1, self.plural(n))]
368-
if self._output_charset:
369-
return tmsg.encode(self._output_charset)
370-
elif self._charset:
371-
return tmsg.encode(self._charset)
372-
return tmsg
373-
except KeyError:
374-
if self._fallback:
375-
return self._fallback.ngettext(msgid1, msgid2, n)
376-
if n == 1:
377-
return msgid1
378-
else:
379-
return msgid2
380-
381357
def lngettext(self, msgid1, msgid2, n):
382358
try:
383359
tmsg = self._catalog[(msgid1, self.plural(n))]
@@ -401,6 +377,8 @@ def ugettext(self, message):
401377
return str(message)
402378
return tmsg
403379

380+
gettext = ugettext
381+
404382
def ungettext(self, msgid1, msgid2, n):
405383
try:
406384
tmsg = self._catalog[(msgid1, self.plural(n))]
@@ -413,6 +391,8 @@ def ungettext(self, msgid1, msgid2, n):
413391
tmsg = str(msgid2)
414392
return tmsg
415393

394+
ngettext = ungettext
395+
416396

417397
# Locate a .mo file using the gettext strategy
418398
def find(domain, localedir=None, languages=None, all=0):

0 commit comments

Comments
 (0)