Skip to content

Commit

Permalink
[#2375] Extract messages in format for ckan trans tag
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Aug 31, 2012
1 parent 315acfc commit 8ec6e8a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions ckan/lib/extract.py
@@ -1,6 +1,7 @@
import re
from genshi.filters.i18n import extract as extract_genshi
from jinja2.ext import babel_extract as extract_jinja2
import lib.jinja_extensions

jinja_extensions = '''
jinja2.ext.do, jinja2.ext.with_,
Expand All @@ -12,6 +13,25 @@
ckan.lib.jinja_extensions.UrlForExtension
'''

def jinja2_cleaner(fileobj, *args, **kw):
# We want to format the messages correctly and intercepting here seems
# the best location
# add our custom tags
kw['options']['extensions'] = jinja_extensions

raw_extract = extract_jinja2(fileobj, *args, **kw)

for lineno, func, message, finder in raw_extract:

if isinstance(message, basestring):
message = lib.jinja_extensions.regularise_html(message)
elif message is not None:
message = (lib.jinja_extensions.regularise_html(message[0])
,lib.jinja_extensions.regularise_html(message[1]))

yield lineno, func, message, finder


def extract_ckan(fileobj, *args, **kw):
''' Determine the type of file (Genshi or Jinja2) and then call the
correct extractor function.
Expand All @@ -22,12 +42,10 @@ def extract_ckan(fileobj, *args, **kw):
source = fileobj.read()
if re.search('genshi\.edgewall\.org', source):
# genshi
extractor_function = extract_genshi
output = extract_genshi(fileobj, *args, **kw)
else:
# jinja2
extractor_function = extract_jinja2
# add our custom tags
kw['options']['extensions'] = jinja_extensions
output = jinja2_cleaner(fileobj, *args, **kw)
# we've eaten the file so we need to get back to the start
fileobj.seek(0)
return extractor_function(fileobj, *args, **kw)
return output

0 comments on commit 8ec6e8a

Please sign in to comment.