1
# Copyright (C) 2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for bzrlib.i18n"""
19
from bzrlib import i18n, tests
22
class ZzzTranslations(object):
23
"""Special Zzz translation for debugging i18n stuff.
25
This class can be used to confirm that the message is properly translated
26
during black box tests.
28
_null_translation = i18n._gettext.NullTranslations()
31
return u'zz{{%s}}' % s
33
def ugettext(self, s):
34
return self.zzz(self._null_translation.ugettext(s))
36
def ungettext(self, s, p, n):
37
return self.zzz(self._null_translation.ungettext(s, p, n))
40
class TestZzzTranslation(tests.TestCase):
42
def _check_exact(self, expected, source):
43
self.assertEqual(expected, source)
44
self.assertEqual(type(expected), type(source))
46
def test_translation(self):
47
trans = ZzzTranslations()
50
self._check_exact(u'zz{{msg}}', t)
52
t = trans.ugettext('msg')
53
self._check_exact(u'zz{{msg}}', t)
55
t = trans.ungettext('msg1', 'msg2', 0)
56
self._check_exact(u'zz{{msg2}}', t)
57
t = trans.ungettext('msg1', 'msg2', 2)
58
self._check_exact(u'zz{{msg2}}', t)
60
t = trans.ungettext('msg1', 'msg2', 1)
61
self._check_exact(u'zz{{msg1}}', t)
64
class TestGetText(tests.TestCase):
67
super(TestGetText, self).setUp()
68
self.overrideAttr(i18n, '_translations', ZzzTranslations())
70
def test_oneline(self):
71
self.assertEqual(u"zz{{spam ham eggs}}",
72
i18n.gettext("spam ham eggs"))
74
def test_multiline(self):
75
self.assertEqual(u"zz{{spam\nham\n\neggs\n}}",
76
i18n.gettext("spam\nham\n\neggs\n"))
79
class TestGetTextPerParagraph(tests.TestCase):
82
super(TestGetTextPerParagraph, self).setUp()
83
self.overrideAttr(i18n, '_translations', ZzzTranslations())
85
def test_oneline(self):
86
self.assertEqual(u"zz{{spam ham eggs}}",
87
i18n.gettext_per_paragraph("spam ham eggs"))
89
def test_multiline(self):
90
self.assertEqual(u"zz{{spam\nham}}\n\nzz{{eggs\n}}",
91
i18n.gettext_per_paragraph("spam\nham\n\neggs\n"))
94
class TestInstall(tests.TestCase):
96
def test_custom_languages(self):
97
self.addCleanup(i18n.install)
100
def test_no_env_variables(self):
101
self.addCleanup(i18n.install)
102
self.overrideEnv('LANGUAGE', None)
103
self.overrideEnv('LC_ALL', None)
104
self.overrideEnv('LC_MESSAGES', None)
105
self.overrideEnv('LANG', None)