Mercurial > genshi > mirror
annotate genshi/filters/tests/i18n.py @ 849:878c4313c7d5 trunk
Merged advanced-i18n branch back into trunk.
author | cmlenz |
---|---|
date | Tue, 10 Nov 2009 20:54:06 +0000 |
parents | da90cee22560 |
children | e8f596c726c4 |
rev | line source |
---|---|
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
2 # |
719 | 3 # Copyright (C) 2007-2008 Edgewall Software |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
5 # |
fd9c4f7a249a
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 |
fd9c4f7a249a
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 |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
8 # are also available at http://genshi.edgewall.org/wiki/License. |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
9 # |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://genshi.edgewall.org/log/. |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
13 |
776
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
14 from datetime import datetime |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
15 import doctest |
788
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
16 from gettext import NullTranslations |
485
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
17 from StringIO import StringIO |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
18 import unittest |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
19 |
667
c9a084ffaee6
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
|
20 from genshi.core import Attrs |
849 | 21 from genshi.template import MarkupTemplate, Context |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
22 from genshi.filters.i18n import Translator, extract |
667
c9a084ffaee6
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
|
23 from genshi.input import HTML |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
24 |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
25 |
788
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
26 class DummyTranslations(NullTranslations): |
849 | 27 _domains = {} |
788
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
28 |
849 | 29 def __init__(self, catalog=()): |
788
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
30 NullTranslations.__init__(self) |
849 | 31 self._catalog = catalog or {} |
32 self.plural = lambda n: n != 1 | |
33 | |
34 def add_domain(self, domain, catalog): | |
35 translation = DummyTranslations(catalog) | |
36 translation.add_fallback(self) | |
37 self._domains[domain] = translation | |
38 | |
39 def _domain_call(self, func, domain, *args, **kwargs): | |
40 return getattr(self._domains.get(domain, self), func)(*args, **kwargs) | |
788
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
41 |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
42 def ugettext(self, message): |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
43 missing = object() |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
44 tmsg = self._catalog.get(message, missing) |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
45 if tmsg is missing: |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
46 if self._fallback: |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
47 return self._fallback.ugettext(message) |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
48 return unicode(message) |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
49 return tmsg |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
50 |
849 | 51 def dugettext(self, domain, message): |
52 return self._domain_call('ugettext', domain, message) | |
53 | |
54 def ungettext(self, msgid1, msgid2, n): | |
55 try: | |
56 return self._catalog[(msgid1, self.plural(n))] | |
57 except KeyError: | |
58 if self._fallback: | |
59 return self._fallback.ngettext(msgid1, msgid2, n) | |
60 if n == 1: | |
61 return msgid1 | |
62 else: | |
63 return msgid2 | |
64 | |
65 def dungettext(self, domain, singular, plural, numeral): | |
66 return self._domain_call('ungettext', domain, singular, plural, numeral) | |
67 | |
788
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
68 |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
69 class TranslatorTestCase(unittest.TestCase): |
485
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
70 |
667
c9a084ffaee6
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
|
71 def test_translate_included_attribute_text(self): |
c9a084ffaee6
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
|
72 """ |
c9a084ffaee6
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
|
73 Verify that translated attributes end up in a proper `Attrs` instance. |
c9a084ffaee6
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
|
74 """ |
c9a084ffaee6
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
|
75 html = HTML("""<html> |
c9a084ffaee6
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
|
76 <span title="Foo"></span> |
c9a084ffaee6
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
|
77 </html>""") |
c9a084ffaee6
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
|
78 translator = Translator(lambda s: u"Voh") |
c9a084ffaee6
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
|
79 stream = list(html.filter(translator)) |
c9a084ffaee6
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
|
80 kind, data, pos = stream[2] |
c9a084ffaee6
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
|
81 assert isinstance(data[1], Attrs) |
c9a084ffaee6
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
|
82 |
594
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
83 def test_extract_without_text(self): |
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
84 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
85 <p title="Bar">Foo</p> |
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
86 ${ngettext("Singular", "Plural", num)} |
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
87 </html>""") |
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
88 translator = Translator(extract_text=False) |
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
89 messages = list(translator.extract(tmpl.stream)) |
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
90 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
91 self.assertEqual((3, 'ngettext', (u'Singular', u'Plural', None), []), |
594
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
92 messages[0]) |
2bbaa61b328f
Add option to I18n filter to only extract strings in gettext function calls.
cmlenz
parents:
565
diff
changeset
|
93 |
485
fb66fb3e4b49
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 def test_extract_plural_form(self): |
fb66fb3e4b49
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 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
fb66fb3e4b49
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 ${ngettext("Singular", "Plural", num)} |
fb66fb3e4b49
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 </html>""") |
fb66fb3e4b49
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 translator = Translator() |
fb66fb3e4b49
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 messages = list(translator.extract(tmpl.stream)) |
fb66fb3e4b49
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 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
101 self.assertEqual((2, 'ngettext', (u'Singular', u'Plural', None), []), |
565
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
102 messages[0]) |
485
fb66fb3e4b49
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 |
600
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
104 def test_extract_funky_plural_form(self): |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
105 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
106 ${ngettext(len(items), *widget.display_names)} |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
107 </html>""") |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
108 translator = Translator() |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
109 messages = list(translator.extract(tmpl.stream)) |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
110 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
111 self.assertEqual((2, 'ngettext', (None, None), []), messages[0]) |
600
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
112 |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
113 def test_extract_gettext_with_unicode_string(self): |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
114 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
115 ${gettext("Grüße")} |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
116 </html>""") |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
117 translator = Translator() |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
118 messages = list(translator.extract(tmpl.stream)) |
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
119 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
120 self.assertEqual((2, 'gettext', u'Gr\xfc\xdfe', []), messages[0]) |
600
23a4784203ae
Handle starargs and dstarargs in the I18n extraction code.
cmlenz
parents:
596
diff
changeset
|
121 |
485
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
122 def test_extract_included_attribute_text(self): |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
123 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
124 <span title="Foo"></span> |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
125 </html>""") |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
126 translator = Translator() |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
127 messages = list(translator.extract(tmpl.stream)) |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
128 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
129 self.assertEqual((2, None, u'Foo', []), messages[0]) |
485
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
130 |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
131 def test_extract_attribute_expr(self): |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
132 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
133 <input type="submit" value="${_('Save')}" /> |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
134 </html>""") |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
135 translator = Translator() |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
136 messages = list(translator.extract(tmpl.stream)) |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
137 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
138 self.assertEqual((2, '_', u'Save', []), messages[0]) |
485
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
139 |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
140 def test_extract_non_included_attribute_interpolated(self): |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
141 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
142 <a href="#anchor_${num}">Foo</a> |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
143 </html>""") |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
144 translator = Translator() |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
145 messages = list(translator.extract(tmpl.stream)) |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
146 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
147 self.assertEqual((2, None, u'Foo', []), messages[0]) |
485
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
148 |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
149 def test_extract_text_from_sub(self): |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
150 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
151 <py:if test="foo">Foo</py:if> |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
152 </html>""") |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
153 translator = Translator() |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
154 messages = list(translator.extract(tmpl.stream)) |
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
155 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
156 self.assertEqual((2, None, u'Foo', []), messages[0]) |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
157 |
522
082535e5087c
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
|
158 def test_ignore_tag_with_fixed_xml_lang(self): |
082535e5087c
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
|
159 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
082535e5087c
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
|
160 <p xml:lang="en">(c) 2007 Edgewall Software</p> |
082535e5087c
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
|
161 </html>""") |
082535e5087c
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
|
162 translator = Translator() |
082535e5087c
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
|
163 messages = list(translator.extract(tmpl.stream)) |
082535e5087c
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
|
164 self.assertEqual(0, len(messages)) |
082535e5087c
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
|
165 |
082535e5087c
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
|
166 def test_extract_tag_with_variable_xml_lang(self): |
082535e5087c
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
|
167 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
082535e5087c
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
|
168 <p xml:lang="${lang}">(c) 2007 Edgewall Software</p> |
082535e5087c
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
|
169 </html>""") |
082535e5087c
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
|
170 translator = Translator() |
082535e5087c
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
|
171 messages = list(translator.extract(tmpl.stream)) |
082535e5087c
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
|
172 self.assertEqual(1, len(messages)) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
173 self.assertEqual((2, None, u'(c) 2007 Edgewall Software', []), |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
174 messages[0]) |
522
082535e5087c
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
|
175 |
535
35a413f3f1dd
The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents:
528
diff
changeset
|
176 def test_ignore_attribute_with_expression(self): |
35a413f3f1dd
The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents:
528
diff
changeset
|
177 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"> |
35a413f3f1dd
The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents:
528
diff
changeset
|
178 <input type="submit" value="Reply" title="Reply to comment $num" /> |
35a413f3f1dd
The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents:
528
diff
changeset
|
179 </html>""") |
35a413f3f1dd
The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents:
528
diff
changeset
|
180 translator = Translator() |
35a413f3f1dd
The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents:
528
diff
changeset
|
181 messages = list(translator.extract(tmpl.stream)) |
35a413f3f1dd
The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents:
528
diff
changeset
|
182 self.assertEqual(0, len(messages)) |
35a413f3f1dd
The I18n filter no longer extracts or translates literal strings in attribute values that also contain expressions.
cmlenz
parents:
528
diff
changeset
|
183 |
560
7e83be231f96
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 def test_extract_i18n_msg(self): |
7e83be231f96
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 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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 <p i18n:msg=""> |
7e83be231f96
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 Please see <a href="help.html">Help</a> for details. |
7e83be231f96
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 </p> |
7e83be231f96
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 </html>""") |
7e83be231f96
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 translator = Translator() |
849 | 192 tmpl.add_directives(Translator.NAMESPACE, translator) |
560
7e83be231f96
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 messages = list(translator.extract(tmpl.stream)) |
7e83be231f96
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 self.assertEqual(1, len(messages)) |
7e83be231f96
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 self.assertEqual('Please see [1:Help] for details.', messages[0][2]) |
7e83be231f96
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 |
7e83be231f96
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 def test_translate_i18n_msg(self): |
7e83be231f96
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 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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 <p i18n:msg=""> |
7e83be231f96
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 Please see <a href="help.html">Help</a> for details. |
7e83be231f96
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 </p> |
7e83be231f96
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 </html>""") |
7e83be231f96
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 gettext = lambda s: u"Für Details siehe bitte [1:Hilfe]." |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
205 translator = Translator(gettext) |
849 | 206 translator.setup(tmpl) |
560
7e83be231f96
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 self.assertEqual("""<html> |
7e83be231f96
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 <p>Für Details siehe bitte <a href="help.html">Hilfe</a>.</p> |
7e83be231f96
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 </html>""", tmpl.generate().render()) |
7e83be231f96
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 |
7e83be231f96
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 def test_extract_i18n_msg_nested(self): |
7e83be231f96
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 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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 <p i18n:msg=""> |
7e83be231f96
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 Please see <a href="help.html"><em>Help</em> page</a> for details. |
7e83be231f96
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 </p> |
7e83be231f96
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 </html>""") |
7e83be231f96
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 translator = Translator() |
849 | 219 tmpl.add_directives(Translator.NAMESPACE, translator) |
560
7e83be231f96
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 messages = list(translator.extract(tmpl.stream)) |
7e83be231f96
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 self.assertEqual(1, len(messages)) |
7e83be231f96
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 self.assertEqual('Please see [1:[2:Help] page] for details.', |
7e83be231f96
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 messages[0][2]) |
7e83be231f96
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 |
7e83be231f96
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 def test_translate_i18n_msg_nested(self): |
7e83be231f96
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 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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 <p i18n:msg=""> |
7e83be231f96
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 Please see <a href="help.html"><em>Help</em> page</a> for details. |
7e83be231f96
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 </p> |
7e83be231f96
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 </html>""") |
7e83be231f96
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 gettext = lambda s: u"Für Details siehe bitte [1:[2:Hilfeseite]]." |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
233 translator = Translator(gettext) |
849 | 234 translator.setup(tmpl) |
560
7e83be231f96
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> |
7e83be231f96
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 Details siehe bitte <a href="help.html"><em>Hilfeseite</em></a>.</p> |
7e83be231f96
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()) |
7e83be231f96
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 |
849 | 239 def test_extract_i18n_msg_label_with_nested_input(self): |
240 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
241 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
242 <div i18n:msg=""> | |
243 <label><input type="text" size="3" name="daysback" value="30" /> days back</label> | |
244 </div> | |
245 </html>""") | |
246 translator = Translator() | |
247 tmpl.add_directives(Translator.NAMESPACE, translator) | |
248 messages = list(translator.extract(tmpl.stream)) | |
249 self.assertEqual(1, len(messages)) | |
250 self.assertEqual('[1:[2:] days back]', | |
251 messages[0][2]) | |
252 | |
253 def test_translate_i18n_msg_label_with_nested_input(self): | |
254 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
255 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
256 <div i18n:msg=""> | |
257 <label><input type="text" size="3" name="daysback" value="30" /> foo bar</label> | |
258 </div> | |
259 </html>""") | |
260 gettext = lambda s: "[1:[2:] foo bar]" | |
261 translator = Translator(gettext) | |
262 translator.setup(tmpl) | |
263 self.assertEqual("""<html> | |
264 <div><label><input type="text" size="3" name="daysback" value="30"/> foo bar</label></div> | |
265 </html>""", tmpl.generate().render()) | |
266 | |
560
7e83be231f96
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 def test_extract_i18n_msg_empty(self): |
7e83be231f96
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 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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 i18n:msg=""> |
7e83be231f96
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 Show me <input type="text" name="num" /> entries per page. |
7e83be231f96
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 </p> |
7e83be231f96
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 </html>""") |
7e83be231f96
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 translator = Translator() |
849 | 275 tmpl.add_directives(Translator.NAMESPACE, translator) |
560
7e83be231f96
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 messages = list(translator.extract(tmpl.stream)) |
7e83be231f96
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 self.assertEqual(1, len(messages)) |
7e83be231f96
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 self.assertEqual('Show me [1:] entries per page.', messages[0][2]) |
7e83be231f96
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 |
7e83be231f96
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 def test_translate_i18n_msg_empty(self): |
7e83be231f96
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 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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 i18n:msg=""> |
7e83be231f96
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 Show me <input type="text" name="num" /> entries per page. |
7e83be231f96
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 </p> |
7e83be231f96
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 </html>""") |
7e83be231f96
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 gettext = lambda s: u"[1:] Einträge pro Seite anzeigen." |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
288 translator = Translator(gettext) |
849 | 289 translator.setup(tmpl) |
560
7e83be231f96
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 self.assertEqual("""<html> |
7e83be231f96
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
|
291 <p><input type="text" name="num"/> Einträge pro Seite anzeigen.</p> |
7e83be231f96
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
|
292 </html>""", tmpl.generate().render()) |
7e83be231f96
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
|
293 |
7e83be231f96
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
|
294 def test_extract_i18n_msg_multiple(self): |
7e83be231f96
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
|
295 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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
|
296 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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
|
297 <p i18n:msg=""> |
7e83be231f96
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
|
298 Please see <a href="help.html">Help</a> for <em>details</em>. |
7e83be231f96
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
|
299 </p> |
7e83be231f96
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
|
300 </html>""") |
7e83be231f96
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
|
301 translator = Translator() |
849 | 302 tmpl.add_directives(Translator.NAMESPACE, translator) |
560
7e83be231f96
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
|
303 messages = list(translator.extract(tmpl.stream)) |
7e83be231f96
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
|
304 self.assertEqual(1, len(messages)) |
7e83be231f96
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
|
305 self.assertEqual('Please see [1:Help] for [2:details].', messages[0][2]) |
7e83be231f96
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
|
306 |
7e83be231f96
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
|
307 def test_translate_i18n_msg_multiple(self): |
7e83be231f96
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
|
308 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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
|
309 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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
|
310 <p i18n:msg=""> |
7e83be231f96
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
|
311 Please see <a href="help.html">Help</a> for <em>details</em>. |
7e83be231f96
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
|
312 </p> |
7e83be231f96
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
|
313 </html>""") |
7e83be231f96
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
|
314 gettext = lambda s: u"Für [2:Details] siehe bitte [1:Hilfe]." |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
315 translator = Translator(gettext) |
849 | 316 translator.setup(tmpl) |
560
7e83be231f96
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
|
317 self.assertEqual("""<html> |
7e83be231f96
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
|
318 <p>Für <em>Details</em> siehe bitte <a href="help.html">Hilfe</a>.</p> |
7e83be231f96
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
|
319 </html>""", tmpl.generate().render()) |
7e83be231f96
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
|
320 |
7e83be231f96
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
|
321 def test_extract_i18n_msg_multiple_empty(self): |
7e83be231f96
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
|
322 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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
|
323 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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
|
324 <p i18n:msg=""> |
7e83be231f96
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
|
325 Show me <input type="text" name="num" /> entries per page, starting at page <input type="text" name="num" />. |
7e83be231f96
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
|
326 </p> |
7e83be231f96
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
|
327 </html>""") |
7e83be231f96
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
|
328 translator = Translator() |
849 | 329 tmpl.add_directives(Translator.NAMESPACE, translator) |
560
7e83be231f96
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
|
330 messages = list(translator.extract(tmpl.stream)) |
7e83be231f96
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
|
331 self.assertEqual(1, len(messages)) |
7e83be231f96
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
|
332 self.assertEqual('Show me [1:] entries per page, starting at page [2:].', |
7e83be231f96
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
|
333 messages[0][2]) |
7e83be231f96
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
|
334 |
7e83be231f96
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
|
335 def test_translate_i18n_msg_multiple_empty(self): |
7e83be231f96
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
|
336 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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
|
337 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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
|
338 <p i18n:msg=""> |
7e83be231f96
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
|
339 Show me <input type="text" name="num" /> entries per page, starting at page <input type="text" name="num" />. |
7e83be231f96
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
|
340 </p> |
7e83be231f96
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
|
341 </html>""") |
7e83be231f96
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
|
342 gettext = lambda s: u"[1:] Einträge pro Seite, beginnend auf Seite [2:]." |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
343 translator = Translator(gettext) |
849 | 344 translator.setup(tmpl) |
560
7e83be231f96
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
|
345 self.assertEqual("""<html> |
7e83be231f96
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
|
346 <p><input type="text" name="num"/> Eintr\xc3\xa4ge pro Seite, beginnend auf Seite <input type="text" name="num"/>.</p> |
7e83be231f96
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
|
347 </html>""", tmpl.generate().render()) |
7e83be231f96
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
|
348 |
776
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
349 def test_extract_i18n_msg_with_param(self): |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
350 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
351 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
352 <p i18n:msg="name"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
353 Hello, ${user.name}! |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
354 </p> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
355 </html>""") |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
356 translator = Translator() |
849 | 357 tmpl.add_directives(Translator.NAMESPACE, translator) |
776
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
358 messages = list(translator.extract(tmpl.stream)) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
359 self.assertEqual(1, len(messages)) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
360 self.assertEqual('Hello, %(name)s!', messages[0][2]) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
361 |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
362 def test_translate_i18n_msg_with_param(self): |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
363 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
364 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
365 <p i18n:msg="name"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
366 Hello, ${user.name}! |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
367 </p> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
368 </html>""") |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
369 gettext = lambda s: u"Hallo, %(name)s!" |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
370 translator = Translator(gettext) |
849 | 371 translator.setup(tmpl) |
776
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
372 self.assertEqual("""<html> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
373 <p>Hallo, Jim!</p> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
374 </html>""", tmpl.generate(user=dict(name='Jim')).render()) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
375 |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
376 def test_translate_i18n_msg_with_param_reordered(self): |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
377 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
378 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
379 <p i18n:msg="name"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
380 Hello, ${user.name}! |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
381 </p> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
382 </html>""") |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
383 gettext = lambda s: u"%(name)s, sei gegrüßt!" |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
384 translator = Translator(gettext) |
849 | 385 translator.setup(tmpl) |
776
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
386 self.assertEqual("""<html> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
387 <p>Jim, sei gegrüßt!</p> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
388 </html>""", tmpl.generate(user=dict(name='Jim')).render()) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
389 |
785
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
390 def test_translate_i18n_msg_with_attribute_param(self): |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
391 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
392 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
393 <p i18n:msg=""> |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
394 Hello, <a href="#${anchor}">dude</a>! |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
395 </p> |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
396 </html>""") |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
397 gettext = lambda s: u"Sei gegrüßt, [1:Alter]!" |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
398 translator = Translator(gettext) |
849 | 399 translator.setup(tmpl) |
785
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
400 self.assertEqual("""<html> |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
401 <p>Sei gegrüßt, <a href="#42">Alter</a>!</p> |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
402 </html>""", tmpl.generate(anchor='42').render()) |
36fb0a57fe74
Fix for #250: ignore expressions in attribute values when inside `i18n:msg` elements.
cmlenz
parents:
776
diff
changeset
|
403 |
776
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
404 def test_extract_i18n_msg_with_two_params(self): |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
405 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
406 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
407 <p i18n:msg="name, time"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
408 Posted by ${post.author} at ${entry.time.strftime('%H:%m')} |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
409 </p> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
410 </html>""") |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
411 translator = Translator() |
849 | 412 tmpl.add_directives(Translator.NAMESPACE, translator) |
776
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
413 messages = list(translator.extract(tmpl.stream)) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
414 self.assertEqual(1, len(messages)) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
415 self.assertEqual('Posted by %(name)s at %(time)s', messages[0][2]) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
416 |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
417 def test_translate_i18n_msg_with_two_params(self): |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
418 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
419 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
420 <p i18n:msg="name, time"> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
421 Written by ${entry.author} at ${entry.time.strftime('%H:%M')} |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
422 </p> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
423 </html>""") |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
424 gettext = lambda s: u"%(name)s schrieb dies um %(time)s" |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
425 translator = Translator(gettext) |
849 | 426 translator.setup(tmpl) |
776
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
427 entry = { |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
428 'author': 'Jim', |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
429 'time': datetime(2008, 4, 1, 14, 30) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
430 } |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
431 self.assertEqual("""<html> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
432 <p>Jim schrieb dies um 14:30</p> |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
433 </html>""", tmpl.generate(entry=entry).render()) |
ddb58d74c8ee
Added tests for the parameter support added to advanced internationalization in [901]. See #129.
cmlenz
parents:
719
diff
changeset
|
434 |
560
7e83be231f96
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
|
435 def test_extract_i18n_msg_with_directive(self): |
7e83be231f96
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
|
436 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
7e83be231f96
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
|
437 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
7e83be231f96
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
|
438 <p i18n:msg=""> |
7e83be231f96
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
|
439 Show me <input type="text" name="num" py:attrs="{'value': x}" /> entries per page. |
7e83be231f96
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
|
440 </p> |
7e83be231f96
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
|
441 </html>""") |
7e83be231f96
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
|
442 translator = Translator() |
849 | 443 tmpl.add_directives(Translator.NAMESPACE, translator) |
560
7e83be231f96
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
|
444 messages = list(translator.extract(tmpl.stream)) |
7e83be231f96
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
|
445 self.assertEqual(1, len(messages)) |
7e83be231f96
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
|
446 self.assertEqual('Show me [1:] entries per page.', messages[0][2]) |
7e83be231f96
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
|
447 |
849 | 448 def test_translate_i18n_msg_with_directive(self): |
449 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
450 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
451 <p i18n:msg=""> | |
452 Show me <input type="text" name="num" py:attrs="{'value': 'x'}" /> entries per page. | |
453 </p> | |
454 </html>""") | |
455 gettext = lambda s: u"[1:] Einträge pro Seite anzeigen." | |
456 translator = Translator(gettext) | |
457 translator.setup(tmpl) | |
458 self.assertEqual("""<html> | |
459 <p><input type="text" name="num" value="x"/> Einträge pro Seite anzeigen.</p> | |
460 </html>""", tmpl.generate().render()) | |
560
7e83be231f96
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
|
461 |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
462 def test_extract_i18n_msg_with_comment(self): |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
463 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
464 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
849 | 465 <p i18n:comment="As in foo bar" i18n:msg="">Foo</p> |
466 </html>""") | |
467 translator = Translator() | |
468 tmpl.add_directives(Translator.NAMESPACE, translator) | |
469 messages = list(translator.extract(tmpl.stream)) | |
470 self.assertEqual(1, len(messages)) | |
471 self.assertEqual((3, None, u'Foo', ['As in foo bar']), messages[0]) | |
472 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
473 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
474 <p i18n:msg="" i18n:comment="As in foo bar">Foo</p> |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
475 </html>""") |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
476 translator = Translator() |
849 | 477 tmpl.add_directives(Translator.NAMESPACE, translator) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
478 messages = list(translator.extract(tmpl.stream)) |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
479 self.assertEqual(1, len(messages)) |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
480 self.assertEqual((3, None, u'Foo', ['As in foo bar']), messages[0]) |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
481 |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
482 def test_translate_i18n_msg_with_comment(self): |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
483 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
484 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
485 <p i18n:msg="" i18n:comment="As in foo bar">Foo</p> |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
486 </html>""") |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
487 gettext = lambda s: u"Voh" |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
488 translator = Translator(gettext) |
849 | 489 translator.setup(tmpl) |
787
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
490 self.assertEqual("""<html> |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
491 <p>Voh</p> |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
492 </html>""", tmpl.generate().render()) |
d7366797440f
Add support for supplying comments on localizable messages in the i18n filter. Based on patch by Pedro Algarvio on #129.
cmlenz
parents:
785
diff
changeset
|
493 |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
494 def test_extract_i18n_msg_with_attr(self): |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
495 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
496 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
497 <p i18n:msg="" title="Foo bar">Foo</p> |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
498 </html>""") |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
499 translator = Translator() |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
500 messages = list(translator.extract(tmpl.stream)) |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
501 self.assertEqual(2, len(messages)) |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
502 self.assertEqual((3, None, u'Foo bar', []), messages[0]) |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
503 self.assertEqual((3, None, u'Foo', []), messages[1]) |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
504 |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
505 def test_translate_i18n_msg_with_attr(self): |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
506 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
507 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
508 <p i18n:msg="" title="Foo bar">Foo</p> |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
509 </html>""") |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
510 gettext = lambda s: u"Voh" |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
511 translator = Translator(DummyTranslations({ |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
512 'Foo': u'Voh', |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
513 'Foo bar': u'Voh bär' |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
514 })) |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
515 tmpl.filters.insert(0, translator) |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
516 tmpl.add_directives(Translator.NAMESPACE, translator) |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
517 self.assertEqual("""<html> |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
518 <p title="Voh bär">Voh</p> |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
519 </html>""", tmpl.generate().render()) |
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
520 |
788
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
521 def test_translate_with_translations_object(self): |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
522 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
523 xmlns:i18n="http://genshi.edgewall.org/i18n"> |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
524 <p i18n:msg="" i18n:comment="As in foo bar">Foo</p> |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
525 </html>""") |
790
da90cee22560
Merged the custom-directives branch back into trunk.
cmlenz
parents:
788
diff
changeset
|
526 translator = Translator(DummyTranslations({'Foo': 'Voh'})) |
849 | 527 translator.setup(tmpl) |
788
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
528 self.assertEqual("""<html> |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
529 <p>Voh</p> |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
530 </html>""", tmpl.generate().render()) |
31432f30a6fb
Change `Translator` class to accept either a `gettext`-style function, or an object compatible with the `NullTranslations` / `GNUTranslations` interface.
cmlenz
parents:
787
diff
changeset
|
531 |
849 | 532 def test_translate_i18n_msg_and_py_strip_directives(self): |
533 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
534 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
535 <p i18n:msg="" py:strip="">Foo</p> | |
536 <p py:strip="" i18n:msg="">Foo</p> | |
537 </html>""") | |
538 translator = Translator(DummyTranslations({'Foo': 'Voh'})) | |
539 translator.setup(tmpl) | |
540 self.assertEqual("""<html> | |
541 Voh | |
542 Voh | |
543 </html>""", tmpl.generate().render()) | |
544 | |
545 def test_i18n_msg_ticket_300_extract(self): | |
546 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
547 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
548 <i18n:msg params="date, author"> | |
549 Changed ${ '10/12/2008' } ago by ${ 'me, the author' } | |
550 </i18n:msg> | |
551 </html>""") | |
552 translator = Translator() | |
553 tmpl.add_directives(Translator.NAMESPACE, translator) | |
554 messages = list(translator.extract(tmpl.stream)) | |
555 self.assertEqual(1, len(messages)) | |
556 self.assertEqual( | |
557 (3, None, u'Changed %(date)s ago by %(author)s', []), messages[0] | |
558 ) | |
559 | |
560 def test_i18n_msg_ticket_300_translate(self): | |
561 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
562 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
563 <i18n:msg params="date, author"> | |
564 Changed ${ date } ago by ${ author } | |
565 </i18n:msg> | |
566 </html>""") | |
567 translations = DummyTranslations({ | |
568 u'Changed %(date)s ago by %(author)s': u'Modificado à %(date)s por %(author)s' | |
569 }) | |
570 translator = Translator(translations) | |
571 translator.setup(tmpl) | |
572 self.assertEqual("""<html> | |
573 Modificado à um dia por Pedro | |
574 </html>""", tmpl.generate(date='um dia', author="Pedro").render()) | |
575 | |
576 | |
577 def test_i18n_msg_ticket_251_extract(self): | |
578 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
579 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
580 <p i18n:msg=""><tt><b>Translation[ 0 ]</b>: <em>One coin</em></tt></p> | |
581 </html>""") | |
582 translator = Translator() | |
583 tmpl.add_directives(Translator.NAMESPACE, translator) | |
584 messages = list(translator.extract(tmpl.stream)) | |
585 self.assertEqual(1, len(messages)) | |
586 self.assertEqual( | |
587 (3, None, u'[1:[2:Translation\\[\xa00\xa0\\]]: [3:One coin]]', []), messages[0] | |
588 ) | |
589 | |
590 def test_i18n_msg_ticket_251_translate(self): | |
591 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
592 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
593 <p i18n:msg=""><tt><b>Translation[ 0 ]</b>: <em>One coin</em></tt></p> | |
594 </html>""") | |
595 translations = DummyTranslations({ | |
596 u'[1:[2:Translation\\[\xa00\xa0\\]]: [3:One coin]]': | |
597 u'[1:[2:Trandução\\[\xa00\xa0\\]]: [3:Uma moeda]]' | |
598 }) | |
599 translator = Translator(translations) | |
600 translator.setup(tmpl) | |
601 self.assertEqual("""<html> | |
602 <p><tt><b>Trandução[ 0 ]</b>: <em>Uma moeda</em></tt></p> | |
603 </html>""", tmpl.generate().render()) | |
604 | |
605 def test_extract_i18n_msg_with_other_directives_nested(self): | |
606 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
607 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
608 <p i18n:msg="" py:with="q = quote_plus(message[:80])">Before you do that, though, please first try | |
609 <strong><a href="${trac.homepage}search?ticket=yes&noquickjump=1&q=$q">searching</a> | |
610 for similar issues</strong>, as it is quite likely that this problem | |
611 has been reported before. For questions about installation | |
612 and configuration of Trac, please try the | |
613 <a href="${trac.homepage}wiki/MailingList">mailing list</a> | |
614 instead of filing a ticket. | |
615 </p> | |
616 </html>""") | |
617 translator = Translator() | |
618 translator.setup(tmpl) | |
619 messages = list(translator.extract(tmpl.stream)) | |
620 self.assertEqual(1, len(messages)) | |
621 self.assertEqual( | |
622 u'Before you do that, though, please first try\n ' | |
623 u'[1:[2:searching]\n for similar issues], as it is ' | |
624 u'quite likely that this problem\n has been reported ' | |
625 u'before. For questions about installation\n and ' | |
626 u'configuration of Trac, please try the\n ' | |
627 u'[3:mailing list]\n instead of filing a ticket.', | |
628 messages[0][2] | |
629 ) | |
630 | |
631 def test_translate_i18n_msg_with_other_directives_nested(self): | |
632 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
633 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
634 <p i18n:msg="">Before you do that, though, please first try | |
635 <strong><a href="${trac.homepage}search?ticket=yes&noquickjump=1&q=q">searching</a> | |
636 for similar issues</strong>, as it is quite likely that this problem | |
637 has been reported before. For questions about installation | |
638 and configuration of Trac, please try the | |
639 <a href="${trac.homepage}wiki/MailingList">mailing list</a> | |
640 instead of filing a ticket. | |
641 </p> | |
642 </html>""") | |
643 translations = DummyTranslations({ | |
644 u'Before you do that, though, please first try\n ' | |
645 u'[1:[2:searching]\n for similar issues], as it is ' | |
646 u'quite likely that this problem\n has been reported ' | |
647 u'before. For questions about installation\n and ' | |
648 u'configuration of Trac, please try the\n ' | |
649 u'[3:mailing list]\n instead of filing a ticket.': | |
650 u'Antes de o fazer, porém,\n ' | |
651 u'[1:por favor tente [2:procurar]\n por problemas semelhantes], uma vez que ' | |
652 u'é muito provável que este problema\n já tenha sido reportado ' | |
653 u'anteriormente. Para questões relativas à instalação\n e ' | |
654 u'configuração do Trac, por favor tente a\n ' | |
655 u'[3:mailing list]\n em vez de criar um assunto.' | |
656 }) | |
657 translator = Translator(translations) | |
658 translator.setup(tmpl) | |
659 messages = list(translator.extract(tmpl.stream)) | |
660 self.assertEqual(1, len(messages)) | |
661 ctx = Context() | |
662 ctx.push({'trac': {'homepage': 'http://trac.edgewall.org/'}}) | |
663 self.assertEqual("""<html> | |
664 <p>Antes de o fazer, porém, | |
665 <strong>por favor tente <a href="http://trac.edgewall.org/search?ticket=yes&noquickjump=1&q=q">procurar</a> | |
666 por problemas semelhantes</strong>, uma vez que é muito provável que este problema | |
667 já tenha sido reportado anteriormente. Para questões relativas à instalação | |
668 e configuração do Trac, por favor tente a | |
669 <a href="http://trac.edgewall.org/wiki/MailingList">mailing list</a> | |
670 em vez de criar um assunto.</p> | |
671 </html>""", tmpl.generate(ctx).render()) | |
672 | |
673 def test_i18n_msg_with_other_nested_directives_with_reordered_content(self): | |
674 # See: http://genshi.edgewall.org/ticket/300#comment:10 | |
675 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
676 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
677 <p py:if="not editable" class="hint" i18n:msg=""> | |
678 <strong>Note:</strong> This repository is defined in | |
679 <code><a href="${ 'href.wiki(TracIni)' }">trac.ini</a></code> | |
680 and cannot be edited on this page. | |
681 </p> | |
682 </html>""") | |
683 translations = DummyTranslations({ | |
684 u'[1:Note:] This repository is defined in\n ' | |
685 u'[2:[3:trac.ini]]\n and cannot be edited on this page.': | |
686 u'[1:Nota:] Este repositório está definido em \n ' | |
687 u'[2:[3:trac.ini]]\n e não pode ser editado nesta página.', | |
688 }) | |
689 translator = Translator(translations) | |
690 translator.setup(tmpl) | |
691 messages = list(translator.extract(tmpl.stream)) | |
692 self.assertEqual(1, len(messages)) | |
693 self.assertEqual( | |
694 u'[1:Note:] This repository is defined in\n ' | |
695 u'[2:[3:trac.ini]]\n and cannot be edited on this page.', | |
696 messages[0][2] | |
697 ) | |
698 self.assertEqual("""<html> | |
699 <p class="hint"><strong>Nota:</strong> Este repositório está definido em | |
700 <code><a href="href.wiki(TracIni)">trac.ini</a></code> | |
701 e não pode ser editado nesta página.</p> | |
702 </html>""", tmpl.generate(editable=False).render()) | |
703 | |
704 def test_translate_i18n_domain_with_msg_directives(self): | |
705 #"""translate with i18n:domain and nested i18n:msg directives """ | |
706 | |
707 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
708 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
709 <div i18n:domain="foo"> | |
710 <p i18n:msg="">FooBar</p> | |
711 <p i18n:msg="">Bar</p> | |
712 </div> | |
713 </html>""") | |
714 translations = DummyTranslations({'Bar': 'Voh'}) | |
715 translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'}) | |
716 translator = Translator(translations) | |
717 translator.setup(tmpl) | |
718 self.assertEqual("""<html> | |
719 <div> | |
720 <p>BarFoo</p> | |
721 <p>PT_Foo</p> | |
722 </div> | |
723 </html>""", tmpl.generate().render()) | |
724 | |
725 def test_translate_i18n_domain_with_inline_directives(self): | |
726 #"""translate with inlined i18n:domain and i18n:msg directives""" | |
727 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
728 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
729 <p i18n:msg="" i18n:domain="foo">FooBar</p> | |
730 </html>""") | |
731 translations = DummyTranslations({'Bar': 'Voh'}) | |
732 translations.add_domain('foo', {'FooBar': 'BarFoo'}) | |
733 translator = Translator(translations) | |
734 translator.setup(tmpl) | |
735 self.assertEqual("""<html> | |
736 <p>BarFoo</p> | |
737 </html>""", tmpl.generate().render()) | |
738 | |
739 def test_translate_i18n_domain_without_msg_directives(self): | |
740 #"""translate domain call without i18n:msg directives still uses current domain""" | |
741 | |
742 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
743 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
744 <p i18n:msg="">Bar</p> | |
745 <div i18n:domain="foo"> | |
746 <p i18n:msg="">FooBar</p> | |
747 <p i18n:msg="">Bar</p> | |
748 <p>Bar</p> | |
749 </div> | |
750 <p>Bar</p> | |
751 </html>""") | |
752 translations = DummyTranslations({'Bar': 'Voh'}) | |
753 translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'}) | |
754 translator = Translator(translations) | |
755 translator.setup(tmpl) | |
756 self.assertEqual("""<html> | |
757 <p>Voh</p> | |
758 <div> | |
759 <p>BarFoo</p> | |
760 <p>PT_Foo</p> | |
761 <p>PT_Foo</p> | |
762 </div> | |
763 <p>Voh</p> | |
764 </html>""", tmpl.generate().render()) | |
765 | |
766 def test_translate_i18n_domain_as_directive_not_attribute(self): | |
767 #"""translate with domain as directive""" | |
768 | |
769 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
770 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
771 <i18n:domain name="foo"> | |
772 <p i18n:msg="">FooBar</p> | |
773 <p i18n:msg="">Bar</p> | |
774 <p>Bar</p> | |
775 </i18n:domain> | |
776 <p>Bar</p> | |
777 </html>""") | |
778 translations = DummyTranslations({'Bar': 'Voh'}) | |
779 translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'}) | |
780 translator = Translator(translations) | |
781 translator.setup(tmpl) | |
782 self.assertEqual("""<html> | |
783 <p>BarFoo</p> | |
784 <p>PT_Foo</p> | |
785 <p>PT_Foo</p> | |
786 <p>Voh</p> | |
787 </html>""", tmpl.generate().render()) | |
788 | |
789 def test_translate_i18n_domain_nested_directives(self): | |
790 #"""translate with nested i18n:domain directives""" | |
791 | |
792 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
793 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
794 <p i18n:msg="">Bar</p> | |
795 <div i18n:domain="foo"> | |
796 <p i18n:msg="">FooBar</p> | |
797 <p i18n:domain="bar" i18n:msg="">Bar</p> | |
798 <p>Bar</p> | |
799 </div> | |
800 <p>Bar</p> | |
801 </html>""") | |
802 translations = DummyTranslations({'Bar': 'Voh'}) | |
803 translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'foo_Bar'}) | |
804 translations.add_domain('bar', {'Bar': 'bar_Bar'}) | |
805 translator = Translator(translations) | |
806 translator.setup(tmpl) | |
807 self.assertEqual("""<html> | |
808 <p>Voh</p> | |
809 <div> | |
810 <p>BarFoo</p> | |
811 <p>bar_Bar</p> | |
812 <p>foo_Bar</p> | |
813 </div> | |
814 <p>Voh</p> | |
815 </html>""", tmpl.generate().render()) | |
816 | |
817 def test_translate_i18n_domain_with_empty_nested_domain_directive(self): | |
818 #"""translate with empty nested i18n:domain directive does not use dngettext""" | |
819 | |
820 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
821 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
822 <p i18n:msg="">Bar</p> | |
823 <div i18n:domain="foo"> | |
824 <p i18n:msg="">FooBar</p> | |
825 <p i18n:domain="" i18n:msg="">Bar</p> | |
826 <p>Bar</p> | |
827 </div> | |
828 <p>Bar</p> | |
829 </html>""") | |
830 translations = DummyTranslations({'Bar': 'Voh'}) | |
831 translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'foo_Bar'}) | |
832 translations.add_domain('bar', {'Bar': 'bar_Bar'}) | |
833 translator = Translator(translations) | |
834 translator.setup(tmpl) | |
835 self.assertEqual("""<html> | |
836 <p>Voh</p> | |
837 <div> | |
838 <p>BarFoo</p> | |
839 <p>Voh</p> | |
840 <p>foo_Bar</p> | |
841 </div> | |
842 <p>Voh</p> | |
843 </html>""", tmpl.generate().render()) | |
844 | |
845 def test_translate_i18n_choose_as_attribute(self): | |
846 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
847 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
848 <div i18n:choose="one"> | |
849 <p i18n:singular="">FooBar</p> | |
850 <p i18n:plural="">FooBars</p> | |
851 </div> | |
852 <div i18n:choose="two"> | |
853 <p i18n:singular="">FooBar</p> | |
854 <p i18n:plural="">FooBars</p> | |
855 </div> | |
856 </html>""") | |
857 translations = DummyTranslations() | |
858 translator = Translator(translations) | |
859 translator.setup(tmpl) | |
860 self.assertEqual("""<html> | |
861 <div> | |
862 <p>FooBar</p> | |
863 </div> | |
864 <div> | |
865 <p>FooBars</p> | |
866 </div> | |
867 </html>""", tmpl.generate(one=1, two=2).render()) | |
868 | |
869 def test_translate_i18n_choose_as_directive(self): | |
870 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
871 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
872 <i18n:choose numeral="two"> | |
873 <p i18n:singular="">FooBar</p> | |
874 <p i18n:plural="">FooBars</p> | |
875 </i18n:choose> | |
876 <i18n:choose numeral="one"> | |
877 <p i18n:singular="">FooBar</p> | |
878 <p i18n:plural="">FooBars</p> | |
879 </i18n:choose> | |
880 </html>""") | |
881 translations = DummyTranslations() | |
882 translator = Translator(translations) | |
883 translator.setup(tmpl) | |
884 self.assertEqual("""<html> | |
885 <p>FooBars</p> | |
886 <p>FooBar</p> | |
887 </html>""", tmpl.generate(one=1, two=2).render()) | |
888 | |
889 def test_translate_i18n_choose_as_attribute_with_params(self): | |
890 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
891 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
892 <div i18n:choose="two; fname, lname"> | |
893 <p i18n:singular="">Foo $fname $lname</p> | |
894 <p i18n:plural="">Foos $fname $lname</p> | |
895 </div> | |
896 </html>""") | |
897 translations = DummyTranslations({ | |
898 ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s', | |
899 ('Foo %(fname)s %(lname)s', 1): 'Vohs %(fname)s %(lname)s', | |
900 'Foo %(fname)s %(lname)s': 'Voh %(fname)s %(lname)s', | |
901 'Foos %(fname)s %(lname)s': 'Vohs %(fname)s %(lname)s', | |
902 }) | |
903 translator = Translator(translations) | |
904 translator.setup(tmpl) | |
905 self.assertEqual("""<html> | |
906 <div> | |
907 <p>Vohs John Doe</p> | |
908 </div> | |
909 </html>""", tmpl.generate(two=2, fname='John', lname='Doe').render()) | |
910 | |
911 def test_translate_i18n_choose_as_attribute_with_params_and_domain_as_param(self): | |
912 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
913 xmlns:i18n="http://genshi.edgewall.org/i18n" | |
914 i18n:domain="foo"> | |
915 <div i18n:choose="two; fname, lname"> | |
916 <p i18n:singular="">Foo $fname $lname</p> | |
917 <p i18n:plural="">Foos $fname $lname</p> | |
918 </div> | |
919 </html>""") | |
920 translations = DummyTranslations() | |
921 translations.add_domain('foo', { | |
922 ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s', | |
923 ('Foo %(fname)s %(lname)s', 1): 'Vohs %(fname)s %(lname)s', | |
924 'Foo %(fname)s %(lname)s': 'Voh %(fname)s %(lname)s', | |
925 'Foos %(fname)s %(lname)s': 'Vohs %(fname)s %(lname)s', | |
926 }) | |
927 translator = Translator(translations) | |
928 translator.setup(tmpl) | |
929 self.assertEqual("""<html> | |
930 <div> | |
931 <p>Vohs John Doe</p> | |
932 </div> | |
933 </html>""", tmpl.generate(two=2, fname='John', lname='Doe').render()) | |
934 | |
935 def test_translate_i18n_choose_as_directive_with_params(self): | |
936 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
937 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
938 <i18n:choose numeral="two" params="fname, lname"> | |
939 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
940 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
941 </i18n:choose> | |
942 <i18n:choose numeral="one" params="fname, lname"> | |
943 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
944 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
945 </i18n:choose> | |
946 </html>""") | |
947 translations = DummyTranslations({ | |
948 ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s', | |
949 ('Foo %(fname)s %(lname)s', 1): 'Vohs %(fname)s %(lname)s', | |
950 'Foo %(fname)s %(lname)s': 'Voh %(fname)s %(lname)s', | |
951 'Foos %(fname)s %(lname)s': 'Vohs %(fname)s %(lname)s', | |
952 }) | |
953 translator = Translator(translations) | |
954 translator.setup(tmpl) | |
955 self.assertEqual("""<html> | |
956 <p>Vohs John Doe</p> | |
957 <p>Voh John Doe</p> | |
958 </html>""", tmpl.generate(one=1, two=2, | |
959 fname='John', lname='Doe').render()) | |
960 | |
961 def test_translate_i18n_choose_as_directive_with_params_and_domain_as_directive(self): | |
962 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
963 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
964 <i18n:domain name="foo"> | |
965 <i18n:choose numeral="two" params="fname, lname"> | |
966 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
967 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
968 </i18n:choose> | |
969 </i18n:domain> | |
970 <i18n:choose numeral="one" params="fname, lname"> | |
971 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
972 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
973 </i18n:choose> | |
974 </html>""") | |
975 translations = DummyTranslations() | |
976 translations.add_domain('foo', { | |
977 ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s', | |
978 ('Foo %(fname)s %(lname)s', 1): 'Vohs %(fname)s %(lname)s', | |
979 'Foo %(fname)s %(lname)s': 'Voh %(fname)s %(lname)s', | |
980 'Foos %(fname)s %(lname)s': 'Vohs %(fname)s %(lname)s', | |
981 }) | |
982 translator = Translator(translations) | |
983 translator.setup(tmpl) | |
984 self.assertEqual("""<html> | |
985 <p>Vohs John Doe</p> | |
986 <p>Foo John Doe</p> | |
987 </html>""", tmpl.generate(one=1, two=2, | |
988 fname='John', lname='Doe').render()) | |
989 | |
990 def test_extract_i18n_choose_as_attribute(self): | |
991 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
992 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
993 <div i18n:choose="one"> | |
994 <p i18n:singular="">FooBar</p> | |
995 <p i18n:plural="">FooBars</p> | |
996 </div> | |
997 <div i18n:choose="two"> | |
998 <p i18n:singular="">FooBar</p> | |
999 <p i18n:plural="">FooBars</p> | |
1000 </div> | |
1001 </html>""") | |
1002 translator = Translator() | |
1003 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1004 messages = list(translator.extract(tmpl.stream)) | |
1005 self.assertEqual(2, len(messages)) | |
1006 self.assertEqual((3, 'ngettext', (u'FooBar', u'FooBars'), []), messages[0]) | |
1007 self.assertEqual((7, 'ngettext', (u'FooBar', u'FooBars'), []), messages[1]) | |
1008 | |
1009 def test_extract_i18n_choose_as_directive(self): | |
1010 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1011 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1012 <i18n:choose numeral="two"> | |
1013 <p i18n:singular="">FooBar</p> | |
1014 <p i18n:plural="">FooBars</p> | |
1015 </i18n:choose> | |
1016 <i18n:choose numeral="one"> | |
1017 <p i18n:singular="">FooBar</p> | |
1018 <p i18n:plural="">FooBars</p> | |
1019 </i18n:choose> | |
1020 </html>""") | |
1021 translator = Translator() | |
1022 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1023 messages = list(translator.extract(tmpl.stream)) | |
1024 self.assertEqual(2, len(messages)) | |
1025 self.assertEqual((3, 'ngettext', (u'FooBar', u'FooBars'), []), messages[0]) | |
1026 self.assertEqual((7, 'ngettext', (u'FooBar', u'FooBars'), []), messages[1]) | |
1027 | |
1028 def test_extract_i18n_choose_as_attribute_with_params(self): | |
1029 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1030 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1031 <div i18n:choose="two; fname, lname"> | |
1032 <p i18n:singular="">Foo $fname $lname</p> | |
1033 <p i18n:plural="">Foos $fname $lname</p> | |
1034 </div> | |
1035 </html>""") | |
1036 translator = Translator() | |
1037 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1038 messages = list(translator.extract(tmpl.stream)) | |
1039 self.assertEqual(1, len(messages)) | |
1040 self.assertEqual((3, 'ngettext', (u'Foo %(fname)s %(lname)s', | |
1041 u'Foos %(fname)s %(lname)s'), []), | |
1042 messages[0]) | |
1043 | |
1044 def test_extract_i18n_choose_as_attribute_with_params_and_domain_as_param(self): | |
1045 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1046 xmlns:i18n="http://genshi.edgewall.org/i18n" | |
1047 i18n:domain="foo"> | |
1048 <div i18n:choose="two; fname, lname"> | |
1049 <p i18n:singular="">Foo $fname $lname</p> | |
1050 <p i18n:plural="">Foos $fname $lname</p> | |
1051 </div> | |
1052 </html>""") | |
1053 translator = Translator() | |
1054 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1055 messages = list(translator.extract(tmpl.stream)) | |
1056 self.assertEqual(1, len(messages)) | |
1057 self.assertEqual((4, 'ngettext', (u'Foo %(fname)s %(lname)s', | |
1058 u'Foos %(fname)s %(lname)s'), []), | |
1059 messages[0]) | |
1060 | |
1061 def test_extract_i18n_choose_as_directive_with_params(self): | |
1062 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1063 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1064 <i18n:choose numeral="two" params="fname, lname"> | |
1065 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
1066 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
1067 </i18n:choose> | |
1068 <i18n:choose numeral="one" params="fname, lname"> | |
1069 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
1070 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
1071 </i18n:choose> | |
1072 </html>""") | |
1073 translator = Translator() | |
1074 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1075 messages = list(translator.extract(tmpl.stream)) | |
1076 self.assertEqual(2, len(messages)) | |
1077 self.assertEqual((3, 'ngettext', (u'Foo %(fname)s %(lname)s', | |
1078 u'Foos %(fname)s %(lname)s'), []), | |
1079 messages[0]) | |
1080 self.assertEqual((7, 'ngettext', (u'Foo %(fname)s %(lname)s', | |
1081 u'Foos %(fname)s %(lname)s'), []), | |
1082 messages[1]) | |
1083 | |
1084 def test_extract_i18n_choose_as_directive_with_params_and_domain_as_directive(self): | |
1085 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1086 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1087 <i18n:domain name="foo"> | |
1088 <i18n:choose numeral="two" params="fname, lname"> | |
1089 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
1090 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
1091 </i18n:choose> | |
1092 </i18n:domain> | |
1093 <i18n:choose numeral="one" params="fname, lname"> | |
1094 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
1095 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
1096 </i18n:choose> | |
1097 </html>""") | |
1098 translator = Translator() | |
1099 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1100 messages = list(translator.extract(tmpl.stream)) | |
1101 self.assertEqual(2, len(messages)) | |
1102 self.assertEqual((4, 'ngettext', (u'Foo %(fname)s %(lname)s', | |
1103 u'Foos %(fname)s %(lname)s'), []), | |
1104 messages[0]) | |
1105 self.assertEqual((9, 'ngettext', (u'Foo %(fname)s %(lname)s', | |
1106 u'Foos %(fname)s %(lname)s'), []), | |
1107 messages[1]) | |
1108 | |
1109 def test_extract_i18n_choose_as_attribute_with_params_and_comment(self): | |
1110 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1111 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1112 <div i18n:choose="two; fname, lname" i18n:comment="As in Foo Bar"> | |
1113 <p i18n:singular="">Foo $fname $lname</p> | |
1114 <p i18n:plural="">Foos $fname $lname</p> | |
1115 </div> | |
1116 </html>""") | |
1117 translator = Translator() | |
1118 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1119 messages = list(translator.extract(tmpl.stream)) | |
1120 self.assertEqual(1, len(messages)) | |
1121 self.assertEqual((3, 'ngettext', (u'Foo %(fname)s %(lname)s', | |
1122 u'Foos %(fname)s %(lname)s'), | |
1123 [u'As in Foo Bar']), | |
1124 messages[0]) | |
1125 | |
1126 def test_extract_i18n_choose_as_directive_with_params_and_comment(self): | |
1127 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1128 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1129 <i18n:choose numeral="two" params="fname, lname" i18n:comment="As in Foo Bar"> | |
1130 <p i18n:singular="">Foo ${fname} ${lname}</p> | |
1131 <p i18n:plural="">Foos ${fname} ${lname}</p> | |
1132 </i18n:choose> | |
1133 </html>""") | |
1134 translator = Translator() | |
1135 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1136 messages = list(translator.extract(tmpl.stream)) | |
1137 self.assertEqual(1, len(messages)) | |
1138 self.assertEqual((3, 'ngettext', (u'Foo %(fname)s %(lname)s', | |
1139 u'Foos %(fname)s %(lname)s'), | |
1140 [u'As in Foo Bar']), | |
1141 messages[0]) | |
1142 | |
1143 def test_translate_i18n_domain_with_nested_inlcudes(self): | |
1144 import os, shutil, tempfile | |
1145 from genshi.template.loader import TemplateLoader | |
1146 dirname = tempfile.mkdtemp(suffix='genshi_test') | |
1147 try: | |
1148 for idx in range(7): | |
1149 file1 = open(os.path.join(dirname, 'tmpl%d.html' % idx), 'w') | |
1150 try: | |
1151 file1.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude" | |
1152 xmlns:py="http://genshi.edgewall.org/" | |
1153 xmlns:i18n="http://genshi.edgewall.org/i18n" py:strip=""> | |
1154 <div>Included tmpl$idx</div> | |
1155 <p i18n:msg="idx">Bar $idx</p> | |
1156 <p i18n:domain="bar">Bar</p> | |
1157 <p i18n:msg="idx" i18n:domain="">Bar $idx</p> | |
1158 <p i18n:domain="" i18n:msg="idx">Bar $idx</p> | |
1159 <py:if test="idx < 6"> | |
1160 <xi:include href="tmpl${idx}.html" py:with="idx = idx+1"/> | |
1161 </py:if> | |
1162 </html>""") | |
1163 finally: | |
1164 file1.close() | |
1165 | |
1166 file2 = open(os.path.join(dirname, 'tmpl10.html'), 'w') | |
1167 try: | |
1168 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude" | |
1169 xmlns:py="http://genshi.edgewall.org/" | |
1170 xmlns:i18n="http://genshi.edgewall.org/i18n" | |
1171 i18n:domain="foo"> | |
1172 <xi:include href="tmpl${idx}.html" py:with="idx = idx+1"/> | |
1173 </html>""") | |
1174 finally: | |
1175 file2.close() | |
1176 | |
1177 def callback(template): | |
1178 translations = DummyTranslations({'Bar %(idx)s': 'Voh %(idx)s'}) | |
1179 translations.add_domain('foo', {'Bar %(idx)s': 'foo_Bar %(idx)s'}) | |
1180 translations.add_domain('bar', {'Bar': 'bar_Bar'}) | |
1181 translator = Translator(translations) | |
1182 translator.setup(template) | |
1183 loader = TemplateLoader([dirname], callback=callback) | |
1184 tmpl = loader.load('tmpl10.html') | |
1185 | |
1186 self.assertEqual("""<html> | |
1187 <div>Included tmpl0</div> | |
1188 <p>foo_Bar 0</p> | |
1189 <p>bar_Bar</p> | |
1190 <p>Voh 0</p> | |
1191 <p>Voh 0</p> | |
1192 <div>Included tmpl1</div> | |
1193 <p>foo_Bar 1</p> | |
1194 <p>bar_Bar</p> | |
1195 <p>Voh 1</p> | |
1196 <p>Voh 1</p> | |
1197 <div>Included tmpl2</div> | |
1198 <p>foo_Bar 2</p> | |
1199 <p>bar_Bar</p> | |
1200 <p>Voh 2</p> | |
1201 <p>Voh 2</p> | |
1202 <div>Included tmpl3</div> | |
1203 <p>foo_Bar 3</p> | |
1204 <p>bar_Bar</p> | |
1205 <p>Voh 3</p> | |
1206 <p>Voh 3</p> | |
1207 <div>Included tmpl4</div> | |
1208 <p>foo_Bar 4</p> | |
1209 <p>bar_Bar</p> | |
1210 <p>Voh 4</p> | |
1211 <p>Voh 4</p> | |
1212 <div>Included tmpl5</div> | |
1213 <p>foo_Bar 5</p> | |
1214 <p>bar_Bar</p> | |
1215 <p>Voh 5</p> | |
1216 <p>Voh 5</p> | |
1217 <div>Included tmpl6</div> | |
1218 <p>foo_Bar 6</p> | |
1219 <p>bar_Bar</p> | |
1220 <p>Voh 6</p> | |
1221 <p>Voh 6</p> | |
1222 </html>""", tmpl.generate(idx=-1).render()) | |
1223 finally: | |
1224 shutil.rmtree(dirname) | |
1225 | |
1226 def test_translate_i18n_domain_with_nested_inlcudes_with_translatable_attrs(self): | |
1227 import os, shutil, tempfile | |
1228 from genshi.template.loader import TemplateLoader | |
1229 dirname = tempfile.mkdtemp(suffix='genshi_test') | |
1230 try: | |
1231 for idx in range(4): | |
1232 file1 = open(os.path.join(dirname, 'tmpl%d.html' % idx), 'w') | |
1233 try: | |
1234 file1.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude" | |
1235 xmlns:py="http://genshi.edgewall.org/" | |
1236 xmlns:i18n="http://genshi.edgewall.org/i18n" py:strip=""> | |
1237 <div>Included tmpl$idx</div> | |
1238 <p title="${dg('foo', 'Bar %(idx)s') % dict(idx=idx)}" i18n:msg="idx">Bar $idx</p> | |
1239 <p title="Bar" i18n:domain="bar">Bar</p> | |
1240 <p title="Bar" i18n:msg="idx" i18n:domain="">Bar $idx</p> | |
1241 <p i18n:msg="idx" i18n:domain="" title="Bar">Bar $idx</p> | |
1242 <p i18n:domain="" i18n:msg="idx" title="Bar">Bar $idx</p> | |
1243 <py:if test="idx < 3"> | |
1244 <xi:include href="tmpl${idx}.html" py:with="idx = idx+1"/> | |
1245 </py:if> | |
1246 </html>""") | |
1247 finally: | |
1248 file1.close() | |
1249 | |
1250 file2 = open(os.path.join(dirname, 'tmpl10.html'), 'w') | |
1251 try: | |
1252 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude" | |
1253 xmlns:py="http://genshi.edgewall.org/" | |
1254 xmlns:i18n="http://genshi.edgewall.org/i18n" | |
1255 i18n:domain="foo"> | |
1256 <xi:include href="tmpl${idx}.html" py:with="idx = idx+1"/> | |
1257 </html>""") | |
1258 finally: | |
1259 file2.close() | |
1260 | |
1261 translations = DummyTranslations({'Bar %(idx)s': 'Voh %(idx)s', | |
1262 'Bar': 'Voh'}) | |
1263 translations.add_domain('foo', {'Bar %(idx)s': 'foo_Bar %(idx)s'}) | |
1264 translations.add_domain('bar', {'Bar': 'bar_Bar'}) | |
1265 translator = Translator(translations) | |
1266 | |
1267 def callback(template): | |
1268 translator.setup(template) | |
1269 loader = TemplateLoader([dirname], callback=callback) | |
1270 tmpl = loader.load('tmpl10.html') | |
1271 | |
1272 self.assertEqual("""<html> | |
1273 <div>Included tmpl0</div> | |
1274 <p title="foo_Bar 0">foo_Bar 0</p> | |
1275 <p title="bar_Bar">bar_Bar</p> | |
1276 <p title="Voh">Voh 0</p> | |
1277 <p title="Voh">Voh 0</p> | |
1278 <p title="Voh">Voh 0</p> | |
1279 <div>Included tmpl1</div> | |
1280 <p title="foo_Bar 1">foo_Bar 1</p> | |
1281 <p title="bar_Bar">bar_Bar</p> | |
1282 <p title="Voh">Voh 1</p> | |
1283 <p title="Voh">Voh 1</p> | |
1284 <p title="Voh">Voh 1</p> | |
1285 <div>Included tmpl2</div> | |
1286 <p title="foo_Bar 2">foo_Bar 2</p> | |
1287 <p title="bar_Bar">bar_Bar</p> | |
1288 <p title="Voh">Voh 2</p> | |
1289 <p title="Voh">Voh 2</p> | |
1290 <p title="Voh">Voh 2</p> | |
1291 <div>Included tmpl3</div> | |
1292 <p title="foo_Bar 3">foo_Bar 3</p> | |
1293 <p title="bar_Bar">bar_Bar</p> | |
1294 <p title="Voh">Voh 3</p> | |
1295 <p title="Voh">Voh 3</p> | |
1296 <p title="Voh">Voh 3</p> | |
1297 </html>""", tmpl.generate(idx=-1, | |
1298 dg=translations.dugettext).render()) | |
1299 finally: | |
1300 shutil.rmtree(dirname) | |
1301 | |
1302 def test_translate_i18n_msg_and_comment_with_py_strip_directives(self): | |
1303 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1304 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1305 <p i18n:msg="" i18n:comment="As in foo bar" py:strip="">Foo</p> | |
1306 <p py:strip="" i18n:msg="" i18n:comment="As in foo bar">Foo</p> | |
1307 </html>""") | |
1308 translator = Translator(DummyTranslations({'Foo': 'Voh'})) | |
1309 translator.setup(tmpl) | |
1310 self.assertEqual("""<html> | |
1311 Voh | |
1312 Voh | |
1313 </html>""", tmpl.generate().render()) | |
1314 | |
1315 def test_translate_i18n_choose_and_py_strip(self): | |
1316 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1317 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1318 <div i18n:choose="two; fname, lname"> | |
1319 <p i18n:singular="">Foo $fname $lname</p> | |
1320 <p i18n:plural="">Foos $fname $lname</p> | |
1321 </div> | |
1322 </html>""") | |
1323 translations = DummyTranslations({ | |
1324 ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s', | |
1325 ('Foo %(fname)s %(lname)s', 1): 'Vohs %(fname)s %(lname)s', | |
1326 'Foo %(fname)s %(lname)s': 'Voh %(fname)s %(lname)s', | |
1327 'Foos %(fname)s %(lname)s': 'Vohs %(fname)s %(lname)s', | |
1328 }) | |
1329 translator = Translator(translations) | |
1330 translator.setup(tmpl) | |
1331 self.assertEqual("""<html> | |
1332 <div> | |
1333 <p>Vohs John Doe</p> | |
1334 </div> | |
1335 </html>""", tmpl.generate(two=2, fname='John', lname='Doe').render()) | |
1336 | |
1337 def test_translate_i18n_choose_and_domain_and_py_strip(self): | |
1338 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1339 xmlns:i18n="http://genshi.edgewall.org/i18n" | |
1340 i18n:domain="foo"> | |
1341 <div i18n:choose="two; fname, lname"> | |
1342 <p i18n:singular="">Foo $fname $lname</p> | |
1343 <p i18n:plural="">Foos $fname $lname</p> | |
1344 </div> | |
1345 </html>""") | |
1346 translations = DummyTranslations() | |
1347 translations.add_domain('foo', { | |
1348 ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s', | |
1349 ('Foo %(fname)s %(lname)s', 1): 'Vohs %(fname)s %(lname)s', | |
1350 'Foo %(fname)s %(lname)s': 'Voh %(fname)s %(lname)s', | |
1351 'Foos %(fname)s %(lname)s': 'Vohs %(fname)s %(lname)s', | |
1352 }) | |
1353 translator = Translator(translations) | |
1354 translator.setup(tmpl) | |
1355 self.assertEqual("""<html> | |
1356 <div> | |
1357 <p>Vohs John Doe</p> | |
1358 </div> | |
1359 </html>""", tmpl.generate(two=2, fname='John', lname='Doe').render()) | |
1360 | |
1361 def test_extract_i18n_msg_with_py_strip(self): | |
1362 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1363 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1364 <p i18n:msg="" py:strip=""> | |
1365 Please see <a href="help.html">Help</a> for details. | |
1366 </p> | |
1367 </html>""") | |
1368 translator = Translator() | |
1369 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1370 messages = list(translator.extract(tmpl.stream)) | |
1371 self.assertEqual(1, len(messages)) | |
1372 self.assertEqual((3, None, u'Please see [1:Help] for details.', []), | |
1373 messages[0]) | |
1374 | |
1375 def test_extract_i18n_msg_with_py_strip_and_comment(self): | |
1376 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1377 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1378 <p i18n:msg="" py:strip="" i18n:comment="Foo"> | |
1379 Please see <a href="help.html">Help</a> for details. | |
1380 </p> | |
1381 </html>""") | |
1382 translator = Translator() | |
1383 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1384 messages = list(translator.extract(tmpl.stream)) | |
1385 self.assertEqual(1, len(messages)) | |
1386 self.assertEqual((3, None, u'Please see [1:Help] for details.', | |
1387 ['Foo']), messages[0]) | |
1388 | |
1389 def test_extract_i18n_choose_as_attribute_and_py_strip(self): | |
1390 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1391 xmlns:i18n="http://genshi.edgewall.org/i18n"> | |
1392 <div i18n:choose="one" py:strip=""> | |
1393 <p i18n:singular="" py:strip="">FooBar</p> | |
1394 <p i18n:plural="" py:strip="">FooBars</p> | |
1395 </div> | |
1396 </html>""") | |
1397 translator = Translator() | |
1398 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1399 messages = list(translator.extract(tmpl.stream)) | |
1400 self.assertEqual(1, len(messages)) | |
1401 self.assertEqual((3, 'ngettext', (u'FooBar', u'FooBars'), []), messages[0]) | |
1402 | |
1403 def test_translate_i18n_domain_with_inline_directive_on_START_NS(self): | |
1404 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1405 xmlns:i18n="http://genshi.edgewall.org/i18n" i18n:domain="foo"> | |
1406 <p i18n:msg="">FooBar</p> | |
1407 </html>""") | |
1408 translations = DummyTranslations({'Bar': 'Voh'}) | |
1409 translations.add_domain('foo', {'FooBar': 'BarFoo'}) | |
1410 translator = Translator(translations) | |
1411 translator.setup(tmpl) | |
1412 self.assertEqual("""<html> | |
1413 <p>BarFoo</p> | |
1414 </html>""", tmpl.generate().render()) | |
1415 | |
1416 def test_translate_i18n_domain_with_inline_directive_on_START_NS_with_py_strip(self): | |
1417 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" | |
1418 xmlns:i18n="http://genshi.edgewall.org/i18n" | |
1419 i18n:domain="foo" py:strip=""> | |
1420 <p i18n:msg="">FooBar</p> | |
1421 </html>""") | |
1422 translations = DummyTranslations({'Bar': 'Voh'}) | |
1423 translations.add_domain('foo', {'FooBar': 'BarFoo'}) | |
1424 translator = Translator(translations) | |
1425 translator.setup(tmpl) | |
1426 self.assertEqual(""" | |
1427 <p>BarFoo</p> | |
1428 """, tmpl.generate().render()) | |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
1429 |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1430 class ExtractTestCase(unittest.TestCase): |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1431 |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1432 def test_markup_template_extraction(self): |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1433 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1434 <head> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1435 <title>Example</title> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1436 </head> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1437 <body> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1438 <h1>Example</h1> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1439 <p>${_("Hello, %(name)s") % dict(name=username)}</p> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1440 <p>${ngettext("You have %d item", "You have %d items", num)}</p> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1441 </body> |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1442 </html>""") |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1443 results = list(extract(buf, ['_', 'ngettext'], [], {})) |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1444 self.assertEqual([ |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1445 (3, None, u'Example', []), |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1446 (6, None, u'Example', []), |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1447 (7, '_', u'Hello, %(name)s', []), |
565
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1448 (8, 'ngettext', (u'You have %d item', u'You have %d items', None), |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1449 []), |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1450 ], results) |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1451 |
596
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1452 def test_extraction_without_text(self): |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1453 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1454 <p title="Bar">Foo</p> |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1455 ${ngettext("Singular", "Plural", num)} |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1456 </html>""") |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1457 results = list(extract(buf, ['_', 'ngettext'], [], { |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1458 'extract_text': 'no' |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1459 })) |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1460 self.assertEqual([ |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1461 (3, 'ngettext', (u'Singular', u'Plural', None), []), |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1462 ], results) |
badb5e5b7bb9
Follow-up to [708]. The added `extract_text` option wasn't actually being handled by the Babel extraction plugin.
cmlenz
parents:
594
diff
changeset
|
1463 |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1464 def test_text_template_extraction(self): |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1465 buf = StringIO("""${_("Dear %(name)s") % {'name': name}}, |
849 | 1466 |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1467 ${ngettext("Your item:", "Your items", len(items))} |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1468 #for item in items |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1469 * $item |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1470 #end |
849 | 1471 |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1472 All the best, |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1473 Foobar""") |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1474 results = list(extract(buf, ['_', 'ngettext'], [], { |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1475 'template_class': 'genshi.template:TextTemplate' |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1476 })) |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1477 self.assertEqual([ |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1478 (1, '_', u'Dear %(name)s', []), |
565
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1479 (3, 'ngettext', (u'Your item:', u'Your items', None), []), |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1480 (7, None, u'All the best,\n Foobar', []) |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1481 ], results) |
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1482 |
565
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1483 def test_extraction_with_keyword_arg(self): |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1484 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1485 ${gettext('Foobar', foo='bar')} |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1486 </html>""") |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1487 results = list(extract(buf, ['gettext'], [], {})) |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1488 self.assertEqual([ |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1489 (2, 'gettext', (u'Foobar'), []), |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1490 ], results) |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1491 |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1492 def test_extraction_with_nonstring_arg(self): |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1493 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1494 ${dgettext(curdomain, 'Foobar')} |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1495 </html>""") |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1496 results = list(extract(buf, ['dgettext'], [], {})) |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1497 self.assertEqual([ |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1498 (2, 'dgettext', (None, u'Foobar'), []), |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1499 ], results) |
53b37e4f2921
* The I18n extractor now handles gettext function calls that use non-string parameters as well as keyword arguments.
cmlenz
parents:
560
diff
changeset
|
1500 |
549
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1501 def test_extraction_inside_ignored_tags(self): |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1502 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1503 <script type="text/javascript"> |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1504 $('#llist').tabs({ |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1505 remote: true, |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1506 spinner: "${_('Please wait...')}" |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1507 }); |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1508 </script> |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1509 </html>""") |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1510 results = list(extract(buf, ['_'], [], {})) |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1511 self.assertEqual([ |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1512 (5, '_', u'Please wait...', []), |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1513 ], results) |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1514 |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1515 def test_extraction_inside_ignored_tags_with_directives(self): |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1516 buf = StringIO("""<html xmlns:py="http://genshi.edgewall.org/"> |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1517 <script type="text/javascript"> |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1518 <py:if test="foobar"> |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1519 alert("This shouldn't be extracted"); |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1520 </py:if> |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1521 </script> |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1522 </html>""") |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1523 self.assertEqual([], list(extract(buf, ['_'], [], {}))) |
7214c1bdb383
The I18n filter now extracts text from translation functions in ignored tags. Fixes #132.
cmlenz
parents:
535
diff
changeset
|
1524 |
849 | 1525 def test_extract_py_def_directive_with_py_strip(self): |
1526 # Failed extraction from Trac | |
1527 tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" py:strip=""> | |
1528 <py:def function="diff_options_fields(diff)"> | |
1529 <label for="style">View differences</label> | |
1530 <select id="style" name="style"> | |
1531 <option selected="${diff.style == 'inline' or None}" | |
1532 value="inline">inline</option> | |
1533 <option selected="${diff.style == 'sidebyside' or None}" | |
1534 value="sidebyside">side by side</option> | |
1535 </select> | |
1536 <div class="field"> | |
1537 Show <input type="text" name="contextlines" id="contextlines" size="2" | |
1538 maxlength="3" value="${diff.options.contextlines < 0 and 'all' or diff.options.contextlines}" /> | |
1539 <label for="contextlines">lines around each change</label> | |
1540 </div> | |
1541 <fieldset id="ignore" py:with="options = diff.options"> | |
1542 <legend>Ignore:</legend> | |
1543 <div class="field"> | |
1544 <input type="checkbox" id="ignoreblanklines" name="ignoreblanklines" | |
1545 checked="${options.ignoreblanklines or None}" /> | |
1546 <label for="ignoreblanklines">Blank lines</label> | |
1547 </div> | |
1548 <div class="field"> | |
1549 <input type="checkbox" id="ignorecase" name="ignorecase" | |
1550 checked="${options.ignorecase or None}" /> | |
1551 <label for="ignorecase">Case changes</label> | |
1552 </div> | |
1553 <div class="field"> | |
1554 <input type="checkbox" id="ignorewhitespace" name="ignorewhitespace" | |
1555 checked="${options.ignorewhitespace or None}" /> | |
1556 <label for="ignorewhitespace">White space changes</label> | |
1557 </div> | |
1558 </fieldset> | |
1559 <div class="buttons"> | |
1560 <input type="submit" name="update" value="${_('Update')}" /> | |
1561 </div> | |
1562 </py:def></html>""") | |
1563 translator = Translator() | |
1564 tmpl.add_directives(Translator.NAMESPACE, translator) | |
1565 messages = list(translator.extract(tmpl.stream)) | |
1566 self.assertEqual(10, len(messages)) | |
1567 self.assertEqual([ | |
1568 (3, None, u'View differences', []), | |
1569 (6, None, u'inline', []), | |
1570 (8, None, u'side by side', []), | |
1571 (10, None, u'Show', []), | |
1572 (13, None, u'lines around each change', []), | |
1573 (16, None, u'Ignore:', []), | |
1574 (20, None, u'Blank lines', []), | |
1575 (25, None, u'Case changes',[]), | |
1576 (30, None, u'White space changes', []), | |
1577 (34, '_', u'Update', [])], messages) | |
1578 | |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1579 |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
1580 def suite(): |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
1581 suite = unittest.TestSuite() |
709
9dd5f370a70e
Python 2.3 compatibility fixes for transformer and (specifically for 2.3.1) i18n.
athomas
parents:
667
diff
changeset
|
1582 suite.addTest(doctest.DocTestSuite(Translator.__module__)) |
485
fb66fb3e4b49
Follow-up to [583]: Don't extract strings from interpolated attribute values for attributes that shouldn't be included.
cmlenz
parents:
446
diff
changeset
|
1583 suite.addTest(unittest.makeSuite(TranslatorTestCase, 'test')) |
528
24df908da22d
Integrated [http://babel.edgewall.org/ Babel] message extraction plugin, and added I18n doc page.
cmlenz
parents:
522
diff
changeset
|
1584 suite.addTest(unittest.makeSuite(ExtractTestCase, 'test')) |
446
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
1585 return suite |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
1586 |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
1587 if __name__ == '__main__': |
fd9c4f7a249a
Add basic I18n/L10n functionality, based on GenshiRecipes/Localization.
cmlenz
parents:
diff
changeset
|
1588 unittest.main(defaultTest='suite') |