Mercurial > genshi > genshi-test
annotate genshi/filters/tests/i18n.py @ 595:6d51c8df5d5a stable-0.4.x
Ported [708] to 0.4.x branch.
author | cmlenz |
---|---|
date | Mon, 13 Aug 2007 23:04:50 +0000 |
parents | d8ccbef91504 |
children | 2c52ec2c9d7e |
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 | 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 | 18 from genshi.template import MarkupTemplate |
532 | 19 from genshi.filters.i18n import Translator, extract |
446
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 | 23 |
595 | 24 def test_extract_without_text(self): |
25 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
26 <p title="Bar">Foo</p> | |
27 ${ngettext("Singular", "Plural", num)} | |
28 </html>""") | |
29 translator = Translator(extract_text=False) | |
30 messages = list(translator.extract(tmpl.stream)) | |
31 self.assertEqual(1, len(messages)) | |
32 self.assertEqual((3, 'ngettext', (u'Singular', u'Plural')), messages[0]) | |
33 | |
486 | 34 def test_extract_plural_form(self): |
35 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
36 ${ngettext("Singular", "Plural", num)} | |
37 </html>""") | |
38 translator = Translator() | |
39 messages = list(translator.extract(tmpl.stream)) | |
40 self.assertEqual(1, len(messages)) | |
41 self.assertEqual((2, 'ngettext', (u'Singular', u'Plural')), messages[0]) | |
42 | |
43 def test_extract_included_attribute_text(self): | |
44 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
45 <span title="Foo"></span> | |
46 </html>""") | |
47 translator = Translator() | |
48 messages = list(translator.extract(tmpl.stream)) | |
49 self.assertEqual(1, len(messages)) | |
50 self.assertEqual((2, None, u'Foo'), messages[0]) | |
51 | |
52 def test_extract_attribute_expr(self): | |
53 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
54 <input type="submit" value="${_('Save')}" /> | |
55 </html>""") | |
56 translator = Translator() | |
57 messages = list(translator.extract(tmpl.stream)) | |
58 self.assertEqual(1, len(messages)) | |
59 self.assertEqual((2, '_', u'Save'), messages[0]) | |
60 | |
61 def test_extract_non_included_attribute_interpolated(self): | |
62 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
63 <a href="#anchor_${num}">Foo</a> | |
64 </html>""") | |
65 translator = Translator() | |
66 messages = list(translator.extract(tmpl.stream)) | |
67 self.assertEqual(1, len(messages)) | |
68 self.assertEqual((2, None, u'Foo'), messages[0]) | |
69 | |
70 def test_extract_text_from_sub(self): | |
71 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
72 <py:if test="foo">Foo</py:if> | |
73 </html>""") | |
74 translator = Translator() | |
75 messages = list(translator.extract(tmpl.stream)) | |
76 self.assertEqual(1, len(messages)) | |
77 self.assertEqual((2, None, u'Foo'), messages[0]) | |
446
90f5908cd10a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
78 |
523 | 79 def test_ignore_tag_with_fixed_xml_lang(self): |
80 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
81 <p xml:lang="en">(c) 2007 Edgewall Software</p> | |
82 </html>""") | |
83 translator = Translator() | |
84 messages = list(translator.extract(tmpl.stream)) | |
85 self.assertEqual(0, len(messages)) | |
86 | |
87 def test_extract_tag_with_variable_xml_lang(self): | |
88 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
89 <p xml:lang="${lang}">(c) 2007 Edgewall Software</p> | |
90 </html>""") | |
91 translator = Translator() | |
92 messages = list(translator.extract(tmpl.stream)) | |
93 self.assertEqual(1, len(messages)) | |
94 self.assertEqual((2, None, u'(c) 2007 Edgewall Software'), messages[0]) | |
95 | |
536 | 96 def test_ignore_attribute_with_expression(self): |
97 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> | |
98 <input type="submit" value="Reply" title="Reply to comment $num" /> | |
99 </html>""") | |
100 translator = Translator() | |
101 messages = list(translator.extract(tmpl.stream)) | |
102 self.assertEqual(0, len(messages)) | |
103 | |
446
90f5908cd10a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
104 |
532 | 105 class ExtractTestCase(unittest.TestCase): |
106 | |
107 def test_markup_template_extraction(self): | |
108 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> | |
109 <head> | |
110 <title>Example</title> | |
111 </head> | |
112 <body> | |
113 <h1>Example</h1> | |
114 <p>${_("Hello, %(name)s") % dict(name=username)}</p> | |
115 <p>${ngettext("You have %d item", "You have %d items", num)}</p> | |
116 </body> | |
117 </html>""") | |
118 results = list(extract(buf, ['_', 'ngettext'], [], {})) | |
119 self.assertEqual([ | |
120 (3, None, u'Example', []), | |
121 (6, None, u'Example', []), | |
122 (7, '_', u'Hello, %(name)s', []), | |
123 (8, 'ngettext', (u'You have %d item', u'You have %d items'), []), | |
124 ], results) | |
125 | |
126 def test_text_template_extraction(self): | |
127 buf = StringIO("""${_("Dear %(name)s") % {'name': name}}, | |
128 | |
129 ${ngettext("Your item:", "Your items", len(items))} | |
130 #for item in items | |
131 * $item | |
132 #end | |
133 | |
134 All the best, | |
135 Foobar""") | |
136 results = list(extract(buf, ['_', 'ngettext'], [], { | |
137 'template_class': 'genshi.template:TextTemplate' | |
138 })) | |
139 self.assertEqual([ | |
140 (1, '_', u'Dear %(name)s', []), | |
141 (3, 'ngettext', (u'Your item:', u'Your items'), []), | |
142 (7, None, u'All the best,\n Foobar', []) | |
143 ], results) | |
144 | |
551 | 145 def test_extraction_inside_ignored_tags(self): |
146 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> | |
147 <script type="text/javascript"> | |
148 $('#llist').tabs({ | |
149 remote: true, | |
150 spinner: "${_('Please wait...')}" | |
151 }); | |
152 </script> | |
153 </html>""") | |
154 results = list(extract(buf, ['_'], [], {})) | |
155 self.assertEqual([ | |
156 (5, '_', u'Please wait...', []), | |
157 ], results) | |
158 | |
159 def test_extraction_inside_ignored_tags_with_directives(self): | |
160 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> | |
161 <script type="text/javascript"> | |
162 <py:if test="foobar"> | |
163 alert("This shouldn't be extracted"); | |
164 </py:if> | |
165 </script> | |
166 </html>""") | |
167 self.assertEqual([], list(extract(buf, ['_'], [], {}))) | |
168 | |
532 | 169 |
446
90f5908cd10a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
170 def suite(): |
90f5908cd10a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
171 suite = unittest.TestSuite() |
532 | 172 suite.addTests(doctest.DocTestSuite(Translator.__module__)) |
486 | 173 suite.addTest(unittest.makeSuite(TranslatorTestCase, 'test')) |
532 | 174 suite.addTest(unittest.makeSuite(ExtractTestCase, 'test')) |
446
90f5908cd10a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
175 return suite |
90f5908cd10a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
176 |
90f5908cd10a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
177 if __name__ == '__main__': |
90f5908cd10a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
178 unittest.main(defaultTest='suite') |