annotate genshi/filters/tests/i18n.py @ 486:6a48853a2e36 stable-0.4.x

Ported [585] to 0.4.x.
author cmlenz
date Mon, 21 May 2007 08:31:48 +0000
parents 90f5908cd10a
children c1738dec04d9
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
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
69
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
70 def suite():
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
71 suite = unittest.TestSuite()
486
6a48853a2e36 Ported [585] to 0.4.x.
cmlenz
parents: 446
diff changeset
72 suite.addTest(unittest.makeSuite(TranslatorTestCase, 'test'))
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
73 suite.addTests(doctest.DocTestSuite(Translator.__module__))
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
74 return suite
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
75
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
76 if __name__ == '__main__':
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
77 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software