string_types = (str,)
integer_types = (int, )
+ text_to_native = lambda s, enc: s
+
iterkeys = lambda d: iter(d.keys())
itervalues = lambda d: iter(d.values())
iteritems = lambda d: iter(d.items())
string_types = (str, unicode)
integer_types = (int, long)
+ text_to_native = lambda s, enc: s.encode(enc)
+
iterkeys = lambda d: d.iterkeys()
itervalues = lambda d: d.itervalues()
iteritems = lambda d: d.iteritems()
format_timedelta
from babel.numbers import format_number, format_decimal, format_currency, \
format_percent, format_scientific
-from babel._compat import PY2
+from babel._compat import PY2, text_type, text_to_native
__all__ = ['Format', 'LazyProxy', 'NullTranslations', 'Translations']
return message
# Encode the Unicode tmsg back to an 8-bit string, if possible
if self._output_charset:
- return tmsg.encode(self._output_charset)
+ return text_to_native(tmsg, self._output_charset)
elif self._charset:
- return tmsg.encode(self._charset)
+ return text_to_native(tmsg, self._charset)
return tmsg
def lpgettext(self, context, message):
try:
tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
if self._output_charset:
- return tmsg.encode(self._output_charset)
+ return text_to_native(tmsg, self._output_charset)
elif self._charset:
- return tmsg.encode(self._charset)
+ return text_to_native(tmsg, self._charset)
return tmsg
except KeyError:
if self._fallback:
if tmsg is missing:
if self._fallback:
return self._fallback.upgettext(context, message)
- return unicode(message)
+ return text_type(message)
return tmsg
def unpgettext(self, context, singular, plural, num):
if self._fallback:
return self._fallback.unpgettext(context, singular, plural, num)
if num == 1:
- tmsg = unicode(singular)
+ tmsg = text_type(singular)
else:
- tmsg = unicode(plural)
+ tmsg = text_type(plural)
return tmsg
def dpgettext(self, domain, context, message):
from babel import support
from babel.messages import Catalog
from babel.messages.mofile import write_mo
-from babel._compat import StringIO
+from babel._compat import BytesIO
@pytest.mark.usefixtures("os_environ")
catalog1.add(ids, **kwargs)
for ids, kwargs in messages2:
catalog2.add(ids, **kwargs)
- catalog1_fp = StringIO()
- catalog2_fp = StringIO()
+ catalog1_fp = BytesIO()
+ catalog2_fp = BytesIO()
write_mo(catalog1_fp, catalog1)
catalog1_fp.seek(0)
write_mo(catalog2_fp, catalog2)
class NullTranslationsTestCase(unittest.TestCase):
def setUp(self):
- fp = StringIO()
+ fp = BytesIO()
write_mo(fp, Catalog(locale='de'))
fp.seek(0)
self.translations = support.Translations(fp=fp)