diff options
Diffstat (limited to 'template.py')
-rwxr-xr-x | template.py | 60 |
1 files changed, 34 insertions, 26 deletions
diff --git a/template.py b/template.py index bd67d3f..c8153cc 100755 --- a/template.py +++ b/template.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # This file is in the public domain. # -# This script runs the jinga2 templating engine on an input template-file +# This script runs the jinja2 templating engine on an input template-file # using the specified locale for gettext translations, and outputs # the resulting (HTML) ouptut-file. # @@ -12,19 +12,19 @@ import os.path import sys import re import gettext +import subprocess import jinja2 import glob import codecs import os -import os.path -import subprocess +sys.path.append(os.getcwd()) +import i18nfix env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), - extensions=["jinja2.ext.i18n"], - lstrip_blocks=True, - trim_blocks=True, - - undefined=jinja2.StrictUndefined, + extensions=["jinja2.ext.i18n"], + lstrip_blocks=True, + trim_blocks=True, + undefined=jinja2.StrictUndefined, autoescape=False) default_ctx = {} @@ -38,6 +38,8 @@ auditor_priv_file = os.path.expanduser("~/.local/share/taler/auditor/offline-key default_ctx["auditor_pub"] = subprocess.check_output(["gnunet-ecc", "-p", auditor_priv_file]).decode("utf-8").strip() +langs_full = {"en": "English" } + for in_file in glob.glob("*.j2"): name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() tmpl = env.get_template(in_file) @@ -51,34 +53,40 @@ for in_file in glob.glob("*.j2"): def url_localized(filename): return "../" + locale + "/" + filename + def svg_localized(filename): + lf = filename + "." + locale + ".svg" + if "en" == locale or not os.path.isfile (lf): + return "../" + filename + ".svg" + else: + return "../" + lf + def url(x): # TODO: look at the app root environment variable # TODO: check if file exists return "../" + x - for l in ("en", "de", "it", "es"): - locale = os.path.basename(l) + for l in glob.glob("locale/*/"): + locale = os.path.basename(l[:-1]) - if os.path.isdir(os.path.join("./locale/", locale)): - tr = gettext.translation("messages", - localedir="locale", - languages=[locale]) + tr = gettext.translation("messages", + localedir="locale", + languages=[locale]) - env.install_gettext_translations(tr, newstyle=True) - else: - print("warning: locale {} not found".format(locale)) - - ctx = dict( - lang=locale, - url=url, - self_localized=self_localized, - url_localized=url_localized, - filename=name + "." + ext) - ctx.update(default_ctx) + tr.gettext = i18nfix.wrap_gettext(tr.gettext) + + env.install_gettext_translations(tr, newstyle=True) + ctx = dict(lang=locale, + lang_full=langs_full[locale], + url=url, + self_localized=self_localized, + url_localized=url_localized, + svg_localized=svg_localized, + filename=name + "." + ext) + ctx.update(default_ctx) content = tmpl.render(**ctx) + out_name = "./" + locale + "/" + in_file.rstrip(".j2") os.makedirs("./" + locale, exist_ok=True) - with codecs.open(out_name, "w", "utf-8") as f: f.write(content) |