500
|
1 # -*- coding: utf-8 -*-
|
|
2 #
|
|
3 # Copyright (C) 2007 Edgewall Software
|
|
4 # All rights reserved.
|
|
5 #
|
|
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://genshi.edgewall.org/wiki/License.
|
|
9 #
|
|
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://genshi.edgewall.org/log/.
|
|
13
|
|
14 import doctest
|
|
15 from StringIO import StringIO
|
|
16 import unittest
|
|
17
|
|
18 from genshi.template import MarkupTemplate
|
|
19 from genshi.filters.i18n import Translator
|
|
20
|
|
21
|
|
22 class TranslatorTestCase(unittest.TestCase):
|
|
23
|
|
24 def test_extract_plural_form(self):
|
|
25 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
|
|
26 ${ngettext("Singular", "Plural", num)}
|
|
27 </html>""")
|
|
28 translator = Translator()
|
|
29 messages = list(translator.extract(tmpl.stream))
|
|
30 self.assertEqual(1, len(messages))
|
|
31 self.assertEqual((2, 'ngettext', (u'Singular', u'Plural')), messages[0])
|
|
32
|
|
33 def test_extract_included_attribute_text(self):
|
|
34 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
|
|
35 <span title="Foo"></span>
|
|
36 </html>""")
|
|
37 translator = Translator()
|
|
38 messages = list(translator.extract(tmpl.stream))
|
|
39 self.assertEqual(1, len(messages))
|
|
40 self.assertEqual((2, None, u'Foo'), messages[0])
|
|
41
|
|
42 def test_extract_attribute_expr(self):
|
|
43 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
|
|
44 <input type="submit" value="${_('Save')}" />
|
|
45 </html>""")
|
|
46 translator = Translator()
|
|
47 messages = list(translator.extract(tmpl.stream))
|
|
48 self.assertEqual(1, len(messages))
|
|
49 self.assertEqual((2, '_', u'Save'), messages[0])
|
|
50
|
|
51 def test_extract_non_included_attribute_interpolated(self):
|
|
52 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
|
|
53 <a href="#anchor_${num}">Foo</a>
|
|
54 </html>""")
|
|
55 translator = Translator()
|
|
56 messages = list(translator.extract(tmpl.stream))
|
|
57 self.assertEqual(1, len(messages))
|
|
58 self.assertEqual((2, None, u'Foo'), messages[0])
|
|
59
|
|
60 def test_extract_text_from_sub(self):
|
|
61 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
|
|
62 <py:if test="foo">Foo</py:if>
|
|
63 </html>""")
|
|
64 translator = Translator()
|
|
65 messages = list(translator.extract(tmpl.stream))
|
|
66 self.assertEqual(1, len(messages))
|
|
67 self.assertEqual((2, None, u'Foo'), messages[0])
|
|
68
|
|
69
|
|
70 def suite():
|
|
71 suite = unittest.TestSuite()
|
|
72 suite.addTest(unittest.makeSuite(TranslatorTestCase, 'test'))
|
|
73 suite.addTests(doctest.DocTestSuite(Translator.__module__))
|
|
74 return suite
|
|
75
|
|
76 if __name__ == '__main__':
|
|
77 unittest.main(defaultTest='suite')
|