1
# -*- coding: utf-8 -*-
3
# Copyright (C) 2007-2011 Edgewall Software
6
# This software is licensed as described in the file COPYING, which
7
# you should have received as part of this distribution. The terms
8
# are also available at http://babel.edgewall.org/wiki/License.
10
# This software consists of voluntary contributions made by many
11
# individuals. For the exact contribution history, see the revision
12
# history and logs, available at http://babel.edgewall.org/log/.
17
from babel.messages import mofile, Catalog
18
from babel._compat import BytesIO, text_type
19
from babel.support import Translations
22
class ReadMoTestCase(unittest.TestCase):
25
self.datadir = os.path.join(os.path.dirname(__file__), 'data')
27
def test_basics(self):
28
mo_path = os.path.join(self.datadir, 'project', 'i18n', 'de',
29
'LC_MESSAGES', 'messages.mo')
30
mo_file = open(mo_path, 'rb')
32
catalog = mofile.read_mo(mo_file)
33
self.assertEqual(2, len(catalog))
34
self.assertEqual('TestProject', catalog.project)
35
self.assertEqual('0.1', catalog.version)
36
self.assertEqual('Stange', catalog['bar'].string)
37
self.assertEqual(['Fuhstange', 'Fuhstangen'],
38
catalog['foobar'].string)
43
class WriteMoTestCase(unittest.TestCase):
45
def test_sorting(self):
46
# Ensure the header is sorted to the first entry so that its charset
47
# can be applied to all subsequent messages by GNUTranslations
48
# (ensuring all messages are safely converted to unicode)
49
catalog = Catalog(locale='en_US')
51
"Content-Type: text/plain; charset=utf-8\n"
52
"Content-Transfer-Encoding: 8bit\n''')
53
catalog.add(u'foo', 'Voh')
54
catalog.add((u'There is', u'There are'), (u'Es gibt', u'Es gibt'))
55
catalog.add(u'Fizz', '')
56
catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
58
mofile.write_mo(buf, catalog)
60
translations = Translations(fp=buf)
61
self.assertEqual(u'Voh', translations.ugettext('foo'))
62
assert isinstance(translations.ugettext('foo'), text_type)
63
self.assertEqual(u'Es gibt', translations.ungettext('There is', 'There are', 1))
64
assert isinstance(translations.ungettext('There is', 'There are', 1), text_type)
65
self.assertEqual(u'Fizz', translations.ugettext('Fizz'))
66
assert isinstance(translations.ugettext('Fizz'), text_type)
67
self.assertEqual(u'Fuzz', translations.ugettext('Fuzz'))
68
assert isinstance(translations.ugettext('Fuzz'), text_type)
69
self.assertEqual(u'Fuzzes', translations.ugettext('Fuzzes'))
70
assert isinstance(translations.ugettext('Fuzzes'), text_type)
72
def test_more_plural_forms(self):
73
catalog2 = Catalog(locale='ru_RU')
74
catalog2.add(('Fuzz', 'Fuzzes'), ('', '', ''))
76
mofile.write_mo(buf, catalog2)