From 230d70ec7ed4fc4de5a368e87b4313d468a4c7f2 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Tue, 2 Sep 2014 11:44:54 +0200 Subject: [PATCH] Make templates compatible to pybabel parsing. --- src/i18n/Makefile.am | 6 ++-- src/i18n/helpers/checkHTML.py | 62 ++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/i18n/Makefile.am b/src/i18n/Makefile.am index c767b5d2..7602865d 100644 --- a/src/i18n/Makefile.am +++ b/src/i18n/Makefile.am @@ -31,20 +31,22 @@ build: update done $(PYTHON) "$(CURDIR)/helpers/languages.py" > "$(ROOT)/static/translation/languages.js" +extract: TEMPFOLDER := $(shell mktemp -t XXXXXXXXi18n -d) extract: @if [ "$(PYBABEL)" = "" ]; then echo "Command 'pybabel' not found, required when extracting i18n"; exit 1; fi + $(PYTHON) "$(CURDIR)/helpers/checkHTML.py" "$(ROOT)/static/partials" "$(TEMPFOLDER)" $(PYBABEL) extract -F "$(CURDIR)/babel.cfg" --no-default-keywords -k _n:1,2 -k _ \ --no-location --project="$(I18N_PROJECT)" --version="$(I18N_VERSION)" \ --copyright-holder="$(I18N_COPYRIGHT)" --msgid-bugs-address="$(I18N_BUG_ADDRESS)" \ -o "$(CURDIR)/messages.pot" \ "$(ROOT)/html" \ - "$(ROOT)/static/partials" \ - "$(ROOT)/static/partials/page" \ + "$(TEMPFOLDER)" \ "$(ROOT)/static/js" \ "$(ROOT)/static/js/controllers" \ "$(ROOT)/static/js/directives" \ "$(ROOT)/static/js/services" \ "$(ROOT)/static/js/filters" + rm -rf $(TEMPFOLDER) update: LANGUAGES = $(shell $(FIND) ./ -iname '*.po' -printf '%f\n' | $(SED) 's/\.po$$//' | $(SED) 's/\messages-//') update: diff --git a/src/i18n/helpers/checkHTML.py b/src/i18n/helpers/checkHTML.py index a93d913e..b1667e3d 100644 --- a/src/i18n/helpers/checkHTML.py +++ b/src/i18n/helpers/checkHTML.py @@ -1,41 +1,57 @@ #!/usr/bin/python +# +# Helper script to make Angular templates parse as jinja templates for translation. +# +# (c)2014 struktur AG +import re import sys -from jinja2 import Environment, FileSystemLoader +import os +import fnmatch +from jinja2 import Environment, FileSystemLoader, exceptions -def log(error): - print error +def main(templates, output_folder=None): -def main(templates): + files = [] + for root, dirnames, filenames in os.walk(templates): + for filename in fnmatch.filter(filenames, '*.html'): + files.append(os.path.join(root, filename)) - env = Environment(loader=FileSystemLoader(templates), extensions=['jinja2.ext.i18n']) - env.install_null_translations() + env = Environment(extensions=['jinja2.ext.i18n']) - print env.list_templates() - print dir(env) + for fn in files: - env.compile_templates("lala", log_function=log) + fp = file(fn, "rb") + html = fp.read() + fp.close() - #for t in env.list_templates(): - # template = env.get_template(t) - # output = template.render() + html = unicode(html, "UTF-8") + html = re.sub(r"\|(\w|:)+", "", html) - #template = env.get_template('test.html') - #output_from_parsed_template = template.render(foo='Hello World!') - #print output_from_parsed_template + if output_folder: + tf = os.path.join(output_folder, os.path.split(fn)[1]) + fp = file(tf, "wb") + fp.write(html.encode("UTF-8")) + fp.close() - # to save the results - #with open("my_new_file.html", "wb") as fh: - # fh.write(output_from_parsed_template) + try: + t = env.from_string(html) + except exceptions.TemplateSyntaxError, exc: + print >>sys.stderr, "Failed to parse: %s at line %d" % (fn, exc.lineno) + raise + + return 0 if __name__ == "__main__": args = sys.argv[1:] if not args: - print "Usage: %s templates-folder" % sys.argv[0] + print "Usage: %s templates-folder [output-folder]" % sys.argv[0] sys.exit(1) - status = 0 - for folder in args: - main(folder) + try: + status = main(*args) + except Exception, exc: + print >> sys.stderr, exc + status = 6 - sys.exit(status) \ No newline at end of file + sys.exit(status)