Edgewall Software

Ticket #567: test_genshi.py

File test_genshi.py, 1.4 KB (added by anonymous, 10 years ago)
Line 
1import genshi.filters.i18n
2from StringIO import StringIO
3from genshi.template import MarkupTemplate
4
5SHOW_DEBUG=True
6
7t = """
8<html xmlns="http://www.w3.org/1999/xhtml"
9      xmlns:py="http://genshi.edgewall.org/"
10      xmlns:xi="http://www.w3.org/2001/XInclude"
11      xmlns:i18n="http://genshi.edgewall.org/i18n">
12  <body>
13    <div i18n:msg="">
14      This message has some <strong>bold</strong> parts in it. It should be
15      <strong> extracted </strong> and used as a <strong> single string </strong>
16      and not as <strong> several different strings </strong>.
17    </div>
18  </body>
19</html>
20
21"""
22
23# Get the list of strings that Babel extracts from this template
24# Put in into available_strings
25available_strings = []
26f = StringIO(t)
27for a,b,text,d in genshi.filters.i18n.extract(f,[],{},{}):
28    available_strings.append(text)
29available_strings = set(available_strings)
30
31# Get the list of strings requested to gettext and put in into requested_strings
32requested_strings = set()
33def gettext(t):
34    requested_strings.add(t)
35    return t
36
37template = MarkupTemplate(t, lookup="lenient")
38filter = genshi.filters.i18n.Translator(gettext)
39template.filters.insert(0, filter)
40result = str(template.generate())
41
42if SHOW_DEBUG:
43    print "----Available strings----"
44    print "\n".join(available_strings)
45    print "----Requested strings----"
46    print "\n".join(requested_strings)
47    print "-------------------------"
48
49assert available_strings == requested_strings