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,
26
class ZzzTranslations(object):
27
"""Special Zzz translation for debugging i18n stuff.
29
This class can be used to confirm that the message is properly translated
30
during black box tests.
32
_null_translation = i18n._gettext.NullTranslations()
35
return u'zz\xe5{{%s}}' % s
37
def ugettext(self, s):
38
return self.zzz(self._null_translation.ugettext(s))
40
def ungettext(self, s, p, n):
41
return self.zzz(self._null_translation.ungettext(s, p, n))
44
class TestZzzTranslation(tests.TestCase):
46
def _check_exact(self, expected, source):
47
self.assertEqual(expected, source)
48
self.assertEqual(type(expected), type(source))
50
def test_translation(self):
51
trans = ZzzTranslations()
54
self._check_exact(u'zz\xe5{{msg}}', t)
56
t = trans.ugettext('msg')
57
self._check_exact(u'zz\xe5{{msg}}', t)
59
t = trans.ungettext('msg1', 'msg2', 0)
60
self._check_exact(u'zz\xe5{{msg2}}', t)
61
t = trans.ungettext('msg1', 'msg2', 2)
62
self._check_exact(u'zz\xe5{{msg2}}', t)
64
t = trans.ungettext('msg1', 'msg2', 1)
65
self._check_exact(u'zz\xe5{{msg1}}', t)
68
class TestGetText(tests.TestCase):
71
super(TestGetText, self).setUp()
72
self.overrideAttr(i18n, '_translations', ZzzTranslations())
74
def test_oneline(self):
75
self.assertEqual(u"zz\xe5{{spam ham eggs}}",
76
i18n.gettext("spam ham eggs"))
78
def test_multiline(self):
79
self.assertEqual(u"zz\xe5{{spam\nham\n\neggs\n}}",
80
i18n.gettext("spam\nham\n\neggs\n"))
83
class TestGetTextPerParagraph(tests.TestCase):
86
super(TestGetTextPerParagraph, self).setUp()
87
self.overrideAttr(i18n, '_translations', ZzzTranslations())
89
def test_oneline(self):
90
self.assertEqual(u"zz\xe5{{spam ham eggs}}",
91
i18n.gettext_per_paragraph("spam ham eggs"))
93
def test_multiline(self):
94
self.assertEqual(u"zz\xe5{{spam\nham}}\n\nzz\xe5{{eggs\n}}",
95
i18n.gettext_per_paragraph("spam\nham\n\neggs\n"))
98
class TestInstall(tests.TestCase):
100
def test_custom_languages(self):
101
self.addCleanup(i18n.install)
102
i18n.install('nl:fy')
104
def test_no_env_variables(self):
105
self.addCleanup(i18n.install)
106
self.overrideEnv('LANGUAGE', None)
107
self.overrideEnv('LC_ALL', None)
108
self.overrideEnv('LC_MESSAGES', None)
109
self.overrideEnv('LANG', None)
112
class TestTranslate(tests.TestCaseWithTransport):
115
super(TestTranslate, self).setUp()
116
self.overrideAttr(i18n, '_translations', ZzzTranslations())
118
def test_error_message_translation(self):
119
"""do errors get translated?"""
121
tree = self.make_branch_and_tree('.')
122
self.overrideAttr(i18n, '_translations', ZzzTranslations())
124
workingtree.WorkingTree.open('./foo')
125
except errors.NotBranchError,e:
127
self.assertContainsRe(err,
128
u"zz\xe5{{Not a branch: .*}}".encode("utf-8"))
130
def test_topic_help_translation(self):
131
"""does topic help get translated?"""
132
from bzrlib import help
134
self.overrideAttr(i18n, '_translations', ZzzTranslations())
135
from StringIO import StringIO
137
help.help("authentication", out)
138
self.assertContainsRe(out.getvalue(), "zz\xe5{{Authentication Settings")