| 1 | import genshi.filters.i18n |
|---|
| 2 | from StringIO import StringIO |
|---|
| 3 | from genshi.template import MarkupTemplate |
|---|
| 4 | |
|---|
| 5 | SHOW_DEBUG=True |
|---|
| 6 | |
|---|
| 7 | t = """ |
|---|
| 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 |
|---|
| 25 | available_strings = [] |
|---|
| 26 | f = StringIO(t) |
|---|
| 27 | for a,b,text,d in genshi.filters.i18n.extract(f,[],{},{}): |
|---|
| 28 | available_strings.append(text) |
|---|
| 29 | available_strings = set(available_strings) |
|---|
| 30 | |
|---|
| 31 | # Get the list of strings requested to gettext and put in into requested_strings |
|---|
| 32 | requested_strings = set() |
|---|
| 33 | def gettext(t): |
|---|
| 34 | requested_strings.add(t) |
|---|
| 35 | return t |
|---|
| 36 | |
|---|
| 37 | template = MarkupTemplate(t, lookup="lenient") |
|---|
| 38 | filter = genshi.filters.i18n.Translator(gettext) |
|---|
| 39 | template.filters.insert(0, filter) |
|---|
| 40 | result = str(template.generate()) |
|---|
| 41 | |
|---|
| 42 | if 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 | |
|---|
| 49 | assert available_strings == requested_strings |
|---|