1
# Copyright (C) 2011, 2016 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 breezy.i18n"""
29
class ZzzTranslations(object):
30
"""Special Zzz translation for debugging i18n stuff.
32
This class can be used to confirm that the message is properly translated
33
during black box tests.
35
_null_translation = i18n._gettext.NullTranslations()
38
return u'zz\xe5{{%s}}' % s
41
return self.zzz(self._null_translation.gettext(s))
43
def ugettext(self, s):
44
return self.zzz(self._null_translation.ugettext(s))
46
def ungettext(self, s, p, n):
47
return self.zzz(self._null_translation.ungettext(s, p, n))
50
class TestZzzTranslation(tests.TestCase):
52
def _check_exact(self, expected, source):
53
self.assertEqual(expected, source)
54
self.assertEqual(type(expected), type(source))
56
def test_translation(self):
57
trans = ZzzTranslations()
60
self._check_exact(u'zz\xe5{{msg}}', t)
62
t = trans.ugettext('msg')
63
self._check_exact(u'zz\xe5{{msg}}', t)
65
t = trans.ungettext('msg1', 'msg2', 0)
66
self._check_exact(u'zz\xe5{{msg2}}', t)
67
t = trans.ungettext('msg1', 'msg2', 2)
68
self._check_exact(u'zz\xe5{{msg2}}', t)
70
t = trans.ungettext('msg1', 'msg2', 1)
71
self._check_exact(u'zz\xe5{{msg1}}', t)
74
class TestGetText(tests.TestCase):
77
super(TestGetText, self).setUp()
78
self.overrideAttr(i18n, '_translations', ZzzTranslations())
80
def test_oneline(self):
81
self.assertEqual(u"zz\xe5{{spam ham eggs}}",
82
i18n.gettext("spam ham eggs"))
84
def test_multiline(self):
85
self.assertEqual(u"zz\xe5{{spam\nham\n\neggs\n}}",
86
i18n.gettext("spam\nham\n\neggs\n"))
89
class TestGetTextPerParagraph(tests.TestCase):
92
super(TestGetTextPerParagraph, self).setUp()
93
self.overrideAttr(i18n, '_translations', ZzzTranslations())
95
def test_oneline(self):
96
self.assertEqual(u"zz\xe5{{spam ham eggs}}",
97
i18n.gettext_per_paragraph("spam ham eggs"))
99
def test_multiline(self):
100
self.assertEqual(u"zz\xe5{{spam\nham}}\n\nzz\xe5{{eggs\n}}",
101
i18n.gettext_per_paragraph("spam\nham\n\neggs\n"))
104
class TestInstall(tests.TestCase):
107
super(TestInstall, self).setUp()
108
# Restore a proper env to test translation installation
109
self.overrideAttr(i18n, '_translations', None)
111
def test_custom_languages(self):
112
i18n.install('nl:fy')
113
# Whether we found a valid tranlsation or not doesn't matter, we got
114
# one and _translations is not None anymore.
115
self.assertIsInstance(i18n._translations,
116
i18n._gettext.NullTranslations)
118
def test_no_env_variables(self):
119
self.overrideEnv('LANGUAGE', None)
120
self.overrideEnv('LC_ALL', None)
121
self.overrideEnv('LC_MESSAGES', None)
122
self.overrideEnv('LANG', None)
124
# Whether we found a valid tranlsation or not doesn't matter, we got
125
# one and _translations is not None anymore.
126
self.assertIsInstance(i18n._translations,
127
i18n._gettext.NullTranslations)
129
def test_disable_i18n(self):
132
# It's disabled, you can't install anything and we fallback to null
133
self.assertIsInstance(i18n._translations,
134
i18n._gettext.NullTranslations)
137
class TestTranslate(tests.TestCaseWithTransport):
140
super(TestTranslate, self).setUp()
141
self.overrideAttr(i18n, '_translations', ZzzTranslations())
143
def test_error_message_translation(self):
144
"""do errors get translated?"""
146
tree = self.make_branch_and_tree('.')
148
workingtree.WorkingTree.open('./foo')
149
except errors.NotBranchError as e:
151
self.assertContainsRe(err,
152
u"zz\xe5{{Not a branch: .*}}".encode("utf-8"))
154
def test_topic_help_translation(self):
155
"""does topic help get translated?"""
158
help.help("authentication", out)
159
self.assertContainsRe(out.getvalue(), "zz\xe5{{Authentication Settings")
162
class LoadPluginTranslations(tests.TestCase):
164
def test_does_not_exist(self):
165
translation = i18n.load_plugin_translations("doesnotexist")
166
self.assertEqual("foo", translation.gettext("foo"))