annotate genshi/filters/tests/i18n.py @ 724:8f2c7023af94 experimental-match-fastpaths

update to trunk to track r847, fixing python 2.4 compatibility issues in speedup (and fixing copyrights, apparently :))
author aflett
date Mon, 21 Apr 2008 19:36:53 +0000
parents 109fb7dfe95e
children 9e0ba5b9693c
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 #
724
8f2c7023af94 update to trunk to track r847, fixing python 2.4 compatibility issues in speedup (and fixing copyrights, apparently :))
aflett
parents: 718
diff changeset
3 # Copyright (C) 2007-2008 Edgewall Software
446
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
485
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
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
667
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
18 from genshi.core import Attrs
485
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
19 from genshi.template import MarkupTemplate
528
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
20 from genshi.filters.i18n import Translator, extract
667
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
21 from genshi.input import HTML
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
22
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
23
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
24 class TranslatorTestCase(unittest.TestCase):
485
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
25
667
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
26 def test_translate_included_attribute_text(self):
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
27 """
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
28 Verify that translated attributes end up in a proper `Attrs` instance.
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
29 """
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
30 html = HTML("""<html>
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
31 <span title="Foo"></span>
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
32 </html>""")
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
33 translator = Translator(lambda s: u"Voh")
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
34 stream = list(html.filter(translator))
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
35 kind, data, pos = stream[2]
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
36 assert isinstance(data[1], Attrs)
077c9142dca0 Fix case where attributes weren't properly wrapped in an `Attrs` instance if one or more of them were translated by the I18n filter, potentially breaking things further down the chain. Closes #162.
cmlenz
parents: 600
diff changeset
37
594
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
38 def test_extract_without_text(self):
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
39 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
40 <p title="Bar">Foo</p>
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
41 ${ngettext("Singular", "Plural", num)}
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
42 </html>""")
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
43 translator = Translator(extract_text=False)
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
44 messages = list(translator.extract(tmpl.stream))
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
45 self.assertEqual(1, len(messages))
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
46 self.assertEqual((3, 'ngettext', (u'Singular', u'Plural', None)),
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
47 messages[0])
0fb43dc2e165 Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents: 565
diff changeset
48
485
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
49 def test_extract_plural_form(self):
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
50 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
51 ${ngettext("Singular", "Plural", num)}
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
52 </html>""")
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
53 translator = Translator()
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
54 messages = list(translator.extract(tmpl.stream))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
55 self.assertEqual(1, len(messages))
565
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
56 self.assertEqual((2, 'ngettext', (u'Singular', u'Plural', None)),
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
57 messages[0])
485
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
58
600
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
59 def test_extract_funky_plural_form(self):
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
60 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
61 ${ngettext(len(items), *widget.display_names)}
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
62 </html>""")
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
63 translator = Translator()
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
64 messages = list(translator.extract(tmpl.stream))
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
65 self.assertEqual(1, len(messages))
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
66 self.assertEqual((2, 'ngettext', (None, None)), messages[0])
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
67
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
68 def test_extract_gettext_with_unicode_string(self):
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
69 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
70 ${gettext("Grüße")}
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
71 </html>""")
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
72 translator = Translator()
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
73 messages = list(translator.extract(tmpl.stream))
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
74 self.assertEqual(1, len(messages))
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
75 self.assertEqual((2, 'gettext', u'Gr\xfc\xdfe'), messages[0])
0d802a7e3630 Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents: 596
diff changeset
76
485
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
77 def test_extract_included_attribute_text(self):
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
78 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
79 <span title="Foo"></span>
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
80 </html>""")
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
81 translator = Translator()
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
82 messages = list(translator.extract(tmpl.stream))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
83 self.assertEqual(1, len(messages))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
84 self.assertEqual((2, None, u'Foo'), messages[0])
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
85
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
86 def test_extract_attribute_expr(self):
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
87 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
88 <input type="submit" value="${_('Save')}" />
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
89 </html>""")
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
90 translator = Translator()
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
91 messages = list(translator.extract(tmpl.stream))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
92 self.assertEqual(1, len(messages))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
93 self.assertEqual((2, '_', u'Save'), messages[0])
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
94
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
95 def test_extract_non_included_attribute_interpolated(self):
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
96 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
97 <a href="#anchor_${num}">Foo</a>
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
98 </html>""")
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
99 translator = Translator()
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
100 messages = list(translator.extract(tmpl.stream))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
101 self.assertEqual(1, len(messages))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
102 self.assertEqual((2, None, u'Foo'), messages[0])
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
103
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
104 def test_extract_text_from_sub(self):
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
105 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
106 <py:if test="foo">Foo</py:if>
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
107 </html>""")
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
108 translator = Translator()
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
109 messages = list(translator.extract(tmpl.stream))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
110 self.assertEqual(1, len(messages))
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
111 self.assertEqual((2, None, u'Foo'), messages[0])
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
112
522
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
113 def test_ignore_tag_with_fixed_xml_lang(self):
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
114 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
115 <p xml:lang="en">(c) 2007 Edgewall Software</p>
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
116 </html>""")
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
117 translator = Translator()
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
118 messages = list(translator.extract(tmpl.stream))
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
119 self.assertEqual(0, len(messages))
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
120
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
121 def test_extract_tag_with_variable_xml_lang(self):
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
122 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
123 <p xml:lang="${lang}">(c) 2007 Edgewall Software</p>
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
124 </html>""")
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
125 translator = Translator()
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
126 messages = list(translator.extract(tmpl.stream))
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
127 self.assertEqual(1, len(messages))
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
128 self.assertEqual((2, None, u'(c) 2007 Edgewall Software'), messages[0])
c1738dec04d9 The I18n filter now skips the content of elements that have an `xml:lang` attribute with a fixed string value. Basically, `xml:lang` can now be used as a flag to mark specific sections as not needing localization.
cmlenz
parents: 485
diff changeset
129
535
c8806216aec4 The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents: 528
diff changeset
130 def test_ignore_attribute_with_expression(self):
c8806216aec4 The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents: 528
diff changeset
131 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/">
c8806216aec4 The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents: 528
diff changeset
132 <input type="submit" value="Reply" title="Reply to comment $num" />
c8806216aec4 The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents: 528
diff changeset
133 </html>""")
c8806216aec4 The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents: 528
diff changeset
134 translator = Translator()
c8806216aec4 The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents: 528
diff changeset
135 messages = list(translator.extract(tmpl.stream))
c8806216aec4 The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents: 528
diff changeset
136 self.assertEqual(0, len(messages))
c8806216aec4 The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents: 528
diff changeset
137
560
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
138 def test_extract_i18n_msg(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
139 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
140 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
141 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
142 Please see <a href="help.html">Help</a> for details.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
143 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
144 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
145 translator = Translator()
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
146 messages = list(translator.extract(tmpl.stream))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
147 self.assertEqual(1, len(messages))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
148 self.assertEqual('Please see [1:Help] for details.', messages[0][2])
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
149
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
150 def test_translate_i18n_msg(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
151 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
152 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
153 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
154 Please see <a href="help.html">Help</a> for details.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
155 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
156 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
157 gettext = lambda s: u"Für Details siehe bitte [1:Hilfe]."
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
158 tmpl.filters.insert(0, Translator(gettext))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
159 self.assertEqual("""<html>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
160 <p>Für Details siehe bitte <a href="help.html">Hilfe</a>.</p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
161 </html>""", tmpl.generate().render())
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
162
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
163 def test_extract_i18n_msg_nested(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
164 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
165 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
166 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
167 Please see <a href="help.html"><em>Help</em> page</a> for details.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
168 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
169 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
170 translator = Translator()
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
171 messages = list(translator.extract(tmpl.stream))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
172 self.assertEqual(1, len(messages))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
173 self.assertEqual('Please see [1:[2:Help] page] for details.',
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
174 messages[0][2])
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
175
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
176 def test_translate_i18n_msg_nested(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
177 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
178 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
179 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
180 Please see <a href="help.html"><em>Help</em> page</a> for details.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
181 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
182 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
183 gettext = lambda s: u"Für Details siehe bitte [1:[2:Hilfeseite]]."
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
184 tmpl.filters.insert(0, Translator(gettext))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
185 self.assertEqual("""<html>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
186 <p>Für Details siehe bitte <a href="help.html"><em>Hilfeseite</em></a>.</p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
187 </html>""", tmpl.generate().render())
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
188
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
189 def test_extract_i18n_msg_empty(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
190 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
191 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
192 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
193 Show me <input type="text" name="num" /> entries per page.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
194 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
195 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
196 translator = Translator()
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
197 messages = list(translator.extract(tmpl.stream))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
198 self.assertEqual(1, len(messages))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
199 self.assertEqual('Show me [1:] entries per page.', messages[0][2])
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
200
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
201 def test_translate_i18n_msg_empty(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
202 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
203 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
204 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
205 Show me <input type="text" name="num" /> entries per page.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
206 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
207 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
208 gettext = lambda s: u"[1:] Einträge pro Seite anzeigen."
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
209 tmpl.filters.insert(0, Translator(gettext))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
210 self.assertEqual("""<html>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
211 <p><input type="text" name="num"/> Einträge pro Seite anzeigen.</p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
212 </html>""", tmpl.generate().render())
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
213
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
214 def test_extract_i18n_msg_multiple(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
215 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
216 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
217 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
218 Please see <a href="help.html">Help</a> for <em>details</em>.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
219 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
220 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
221 translator = Translator()
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
222 messages = list(translator.extract(tmpl.stream))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
223 self.assertEqual(1, len(messages))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
224 self.assertEqual('Please see [1:Help] for [2:details].', messages[0][2])
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
225
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
226 def test_translate_i18n_msg_multiple(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
227 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
228 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
229 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
230 Please see <a href="help.html">Help</a> for <em>details</em>.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
231 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
232 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
233 gettext = lambda s: u"Für [2:Details] siehe bitte [1:Hilfe]."
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
234 tmpl.filters.insert(0, Translator(gettext))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
235 self.assertEqual("""<html>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
236 <p>Für <em>Details</em> siehe bitte <a href="help.html">Hilfe</a>.</p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
237 </html>""", tmpl.generate().render())
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
238
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
239 def test_extract_i18n_msg_multiple_empty(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
240 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
241 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
242 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
243 Show me <input type="text" name="num" /> entries per page, starting at page <input type="text" name="num" />.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
244 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
245 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
246 translator = Translator()
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
247 messages = list(translator.extract(tmpl.stream))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
248 self.assertEqual(1, len(messages))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
249 self.assertEqual('Show me [1:] entries per page, starting at page [2:].',
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
250 messages[0][2])
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
251
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
252 def test_translate_i18n_msg_multiple_empty(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
253 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
254 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
255 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
256 Show me <input type="text" name="num" /> entries per page, starting at page <input type="text" name="num" />.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
257 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
258 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
259 gettext = lambda s: u"[1:] Einträge pro Seite, beginnend auf Seite [2:]."
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
260 tmpl.filters.insert(0, Translator(gettext))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
261 self.assertEqual("""<html>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
262 <p><input type="text" name="num"/> Eintr\xc3\xa4ge pro Seite, beginnend auf Seite <input type="text" name="num"/>.</p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
263 </html>""", tmpl.generate().render())
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
264
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
265 def test_extract_i18n_msg_with_directive(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
266 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
267 xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
268 <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
269 Show me <input type="text" name="num" py:attrs="{'value': x}" /> entries per page.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
270 </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
271 </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
272 translator = Translator()
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
273 messages = list(translator.extract(tmpl.stream))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
274 self.assertEqual(1, len(messages))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
275 self.assertEqual('Show me [1:] entries per page.', messages[0][2])
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
276
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
277 # FIXME: this currently fails :-/
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
278 # def test_translate_i18n_msg_with_directive(self):
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
279 # tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
280 # xmlns:i18n="http://genshi.edgewall.org/i18n">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
281 # <p i18n:msg="">
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
282 # Show me <input type="text" name="num" py:attrs="{'value': x}" /> entries per page.
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
283 # </p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
284 # </html>""")
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
285 # gettext = lambda s: u"[1:] Einträge pro Seite anzeigen."
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
286 # tmpl.filters.insert(0, Translator(gettext))
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
287 # self.assertEqual("""<html>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
288 # <p><input type="text" name="num" value="x"/> Einträge pro Seite anzeigen.</p>
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
289 # </html>""", tmpl.generate().render())
f227a2f12e5f Start implementation of advanced I18n as dicussed in #129 and the MailingList. This is not complete yet, but many simple cases work okay.
cmlenz
parents: 549
diff changeset
290
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
291
528
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
292 class ExtractTestCase(unittest.TestCase):
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
293
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
294 def test_markup_template_extraction(self):
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
295 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/">
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
296 <head>
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
297 <title>Example</title>
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
298 </head>
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
299 <body>
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
300 <h1>Example</h1>
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
301 <p>${_("Hello, %(name)s") % dict(name=username)}</p>
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
302 <p>${ngettext("You have %d item", "You have %d items", num)}</p>
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
303 </body>
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
304 </html>""")
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
305 results = list(extract(buf, ['_', 'ngettext'], [], {}))
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
306 self.assertEqual([
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
307 (3, None, u'Example', []),
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
308 (6, None, u'Example', []),
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
309 (7, '_', u'Hello, %(name)s', []),
565
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
310 (8, 'ngettext', (u'You have %d item', u'You have %d items', None),
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
311 []),
528
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
312 ], results)
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
313
596
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
314 def test_extraction_without_text(self):
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
315 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/">
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
316 <p title="Bar">Foo</p>
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
317 ${ngettext("Singular", "Plural", num)}
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
318 </html>""")
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
319 results = list(extract(buf, ['_', 'ngettext'], [], {
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
320 'extract_text': 'no'
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
321 }))
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
322 self.assertEqual([
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
323 (3, 'ngettext', (u'Singular', u'Plural', None), []),
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
324 ], results)
f436c7db99f5 Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents: 594
diff changeset
325
528
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
326 def test_text_template_extraction(self):
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
327 buf = StringIO("""${_("Dear %(name)s") % {'name': name}},
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
328
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
329 ${ngettext("Your item:", "Your items", len(items))}
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
330 #for item in items
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
331 * $item
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
332 #end
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
333
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
334 All the best,
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
335 Foobar""")
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
336 results = list(extract(buf, ['_', 'ngettext'], [], {
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
337 'template_class': 'genshi.template:TextTemplate'
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
338 }))
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
339 self.assertEqual([
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
340 (1, '_', u'Dear %(name)s', []),
565
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
341 (3, 'ngettext', (u'Your item:', u'Your items', None), []),
528
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
342 (7, None, u'All the best,\n Foobar', [])
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
343 ], results)
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
344
565
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
345 def test_extraction_with_keyword_arg(self):
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
346 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/">
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
347 ${gettext('Foobar', foo='bar')}
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
348 </html>""")
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
349 results = list(extract(buf, ['gettext'], [], {}))
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
350 self.assertEqual([
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
351 (2, 'gettext', (u'Foobar'), []),
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
352 ], results)
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
353
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
354 def test_extraction_with_nonstring_arg(self):
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
355 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/">
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
356 ${dgettext(curdomain, 'Foobar')}
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
357 </html>""")
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
358 results = list(extract(buf, ['dgettext'], [], {}))
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
359 self.assertEqual([
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
360 (2, 'dgettext', (None, u'Foobar'), []),
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
361 ], results)
aa8e85a4085e * The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents: 560
diff changeset
362
549
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
363 def test_extraction_inside_ignored_tags(self):
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
364 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/">
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
365 <script type="text/javascript">
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
366 $('#llist').tabs({
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
367 remote: true,
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
368 spinner: "${_('Please wait...')}"
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
369 });
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
370 </script>
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
371 </html>""")
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
372 results = list(extract(buf, ['_'], [], {}))
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
373 self.assertEqual([
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
374 (5, '_', u'Please wait...', []),
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
375 ], results)
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
376
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
377 def test_extraction_inside_ignored_tags_with_directives(self):
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
378 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/">
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
379 <script type="text/javascript">
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
380 <py:if test="foobar">
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
381 alert("This shouldn't be extracted");
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
382 </py:if>
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
383 </script>
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
384 </html>""")
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
385 self.assertEqual([], list(extract(buf, ['_'], [], {})))
da5cbf6d134d The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents: 535
diff changeset
386
528
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
387
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
388 def suite():
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
389 suite = unittest.TestSuite()
718
109fb7dfe95e update to trunk through r833
aflett
parents: 667
diff changeset
390 suite.addTest(doctest.DocTestSuite(Translator.__module__))
485
770ba8556940 Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents: 446
diff changeset
391 suite.addTest(unittest.makeSuite(TranslatorTestCase, 'test'))
528
f38ce008ab0a Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents: 522
diff changeset
392 suite.addTest(unittest.makeSuite(ExtractTestCase, 'test'))
446
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
393 return suite
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
394
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
395 if __name__ == '__main__':
90f5908cd10a Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff changeset
396 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software