close_output = False
try:
+ print(outfile)
write_po(outfile, catalog, width=options.width,
no_location=options.no_location,
omit_header=options.omit_header,
.. note: the code in this module is not used by Babel itself
"""
-from datetime import date, datetime, timedelta
import gettext
import locale
format_timedelta
from babel.numbers import format_number, format_decimal, format_currency, \
format_percent, format_scientific
-from babel.util import UTC
+from babel._compat import PY2
__all__ = ['Format', 'LazyProxy', 'NullTranslations', 'Translations']
return self._domains.get(domain, self).lnpgettext(context, singular,
plural, num)
+ if not PY2:
+ ugettext = gettext.NullTranslations.gettext
+ ungettext = gettext.NullTranslations.ngettext
+ upgettext = gettext.NullTranslations.pgettext
+ upnpgettext = gettext.NullTranslations.npgettext
+
class Translations(NullTranslations, gettext.GNUTranslations):
"""An extended translation catalog class."""
super(Translations, self).__init__(fp=fp)
self.domain = domain or self.DEFAULT_DOMAIN
+ if not PY2:
+ ugettext = gettext.GNUTranslations.gettext
+ ungettext = gettext.GNUTranslations.ngettext
+ upgettext = gettext.GNUTranslations.pgettext
+ upnpgettext = gettext.GNUTranslations.npgettext
+
@classmethod
def load(cls, dirname=None, locales=None, domain=None):
"""Load translations from the given directory.
self.files.extend(translations.files)
return self
-
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
-import gettext
import os
import unittest
from babel.messages import mofile, Catalog
from babel._compat import BytesIO
+from babel.util import Translations
class ReadMoTestCase(unittest.TestCase):
buf = BytesIO()
mofile.write_mo(buf, catalog)
buf.seek(0)
- translations = gettext.GNUTranslations(fp=buf)
+ translations = Translations(fp=buf)
self.assertEqual(u'Voh', translations.ugettext('foo'))
assert isinstance(translations.ugettext('foo'), unicode)
self.assertEqual(u'Es gibt', translations.ungettext('There is', 'There are', 1))
self.assertEqual('mydomain', catalog.domain)
def test_applies_specified_encoding_during_read(self):
- buf = StringIO(u'''
+ buf = BytesIO(u'''
msgid ""
msgstr ""
"Project-Id-Version: 3.15\\n"
self.assertEqual(0, len(catalog.obsolete))
def test_with_context(self):
- buf = StringIO(r'''# Some string in the menu
+ buf = BytesIO(b'''# Some string in the menu
#: main.py:1
msgctxt "Menu"
msgid "foo"
out_buf.getvalue()
def test_with_context_two(self):
- buf = StringIO(r'''msgctxt "Menu"
+ buf = BytesIO(b'''msgctxt "Menu"
msgid "foo"
msgstr "Voh"
catalog.add(u'foo', locations=[('utils.py', 3)])
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=True)
- self.assertEqual('''#: main.py:1 utils.py:3
+ self.assertEqual(b'''#: main.py:1 utils.py:3
msgid "foo"
msgstr ""''', buf.getvalue().strip())
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=False)
po_file = buf.getvalue().strip()
- assert r'"Content-Type: text/plain; charset=iso-8859-1\n"' in po_file
+ assert b'"Content-Type: text/plain; charset=iso-8859-1\\n"' in po_file
assert u'msgstr "äöü"'.encode('iso-8859-1') in po_file
def test_duplicate_comments(self):
# AReallyReallyLongNameForAProject project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
-#, fuzzy''', '\n'.join(buf.getvalue().splitlines()[:7]))
+#, fuzzy''', b'\n'.join(buf.getvalue().splitlines()[:7]))
def test_wrap_locations_with_hyphens(self):
catalog = Catalog()
msgid_plural "foos"
msgstr[0] "Voh"
msgstr[1] "Voeh"''' in value
- assert value.find('msgid ""') < value.find('msgid "bar"') < value.find('msgid "foo"')
+ assert value.find(b'msgid ""') < value.find(b'msgid "bar"') < value.find(b'msgid "foo"')
def test_silent_location_fallback(self):
buf = BytesIO(b'''\