annotate genshi/filters/tests/i18n.py @ 523:5b59e911e6c3 stable-0.4.x

Ported [626] to 0.4.x branch.
author cmlenz
date Fri, 15 Jun 2007 20:19:29 +0000
parents 6a48853a2e36
children f38ce008ab0a
rev   line source
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
2 #
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
4 # All rights reserved.
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
5 #
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
9 #
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
13
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
14 import doctest
486
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
15 from StringIO import StringIO
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
16 import unittest
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
17
486
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
18 from genshi.template import MarkupTemplate
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
19 from genshi.filters.i18n import Translator
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
20
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
21
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
22 class TranslatorTestCase(unittest.TestCase):
486
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
23
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
24 def test_extract_plural_form(self):
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
25 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
26 ${ngettext("Singular", "Plural", num)}
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
27 </html>""")
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
28 translator = Translator()
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
29 messages = list(translator.extract(tmpl.stream))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
30 self.assertEqual(1, len(messages))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
31 self.assertEqual((2, 'ngettext', (u'Singular', u'Plural')), messages[0])
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
32
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
33 def test_extract_included_attribute_text(self):
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
34 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
35 <span title="Foo"></span>
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
36 </html>""")
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
37 translator = Translator()
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
38 messages = list(translator.extract(tmpl.stream))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
39 self.assertEqual(1, len(messages))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
40 self.assertEqual((2, None, u'Foo'), messages[0])
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
41
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
42 def test_extract_attribute_expr(self):
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
43 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
44 <input type="submit" value="${_('Save')}" />
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
45 </html>""")
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
46 translator = Translator()
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
47 messages = list(translator.extract(tmpl.stream))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
48 self.assertEqual(1, len(messages))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
49 self.assertEqual((2, '_', u'Save'), messages[0])
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
50
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
51 def test_extract_non_included_attribute_interpolated(self):
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
52 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
53 <a href="#anchor_${num}">Foo</a>
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
54 </html>""")
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
55 translator = Translator()
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
56 messages = list(translator.extract(tmpl.stream))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
57 self.assertEqual(1, len(messages))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
58 self.assertEqual((2, None, u'Foo'), messages[0])
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
59
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
60 def test_extract_text_from_sub(self):
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
61 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
62 <py:if test="foo">Foo</py:if>
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
63 </html>""")
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
64 translator = Translator()
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
65 messages = list(translator.extract(tmpl.stream))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
66 self.assertEqual(1, len(messages))
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
67 self.assertEqual((2, None, u'Foo'), messages[0])
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
68
523
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
69 def test_ignore_tag_with_fixed_xml_lang(self):
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
70 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
71 <p xml:lang="en">(c) 2007 Edgewall Software</p>
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
72 </html>""")
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
73 translator = Translator()
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
74 messages = list(translator.extract(tmpl.stream))
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
75 self.assertEqual(0, len(messages))
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
76
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
77 def test_extract_tag_with_variable_xml_lang(self):
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
78 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
79 <p xml:lang="${lang}">(c) 2007 Edgewall Software</p>
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
80 </html>""")
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
81 translator = Translator()
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
82 messages = list(translator.extract(tmpl.stream))
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
83 self.assertEqual(1, len(messages))
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
84 self.assertEqual((2, None, u'(c) 2007 Edgewall Software'), messages[0])
5b59e911e6c3 Ported [626] to 0.4.x branch.
cmlenz
parents: 486
diff changeset
85
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
86
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
87 def suite():
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
88 suite = unittest.TestSuite()
486
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
89 suite.addTest(unittest.makeSuite(TranslatorTestCase, 'test'))
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
90 suite.addTests(doctest.DocTestSuite(Translator.__module__))
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
91 return suite
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
92
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
93 if __name__ == '__main__':
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
94 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software