blob: f872ad3e54cd9adb9e4861564fa8e7c1e77f2428 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/usr/bin/env python
# This file is in the public domain.
#
# This script runs the jinga2 templating engine on an input template-file
# using the specified locale for gettext translations, and outputs
# the resulting (HTML) ouptut-file.
#
# Note that the gettext files need to be prepared first. This script
# is thus to be invoked via the Makefile.
import os
import sys
import re
import gettext
import jinja2
if len(sys.argv) < 3:
sys.exit("Usage: " + __file__ + " <template-file> <locale> <output-file>")
in_file = sys.argv[1]
locale = sys.argv[2]
name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups()
tr = gettext.translation("messages",
localedir="locale",
languages=[locale])
env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=["jinja2.ext.i18n"],
autoescape=False)
env.install_gettext_translations(tr, newstyle=True)
tmpl = env.get_template(in_file)
def self_localized(other_locale):
"""
Return URL for the current page in another locale.
"""
return "../" + other_locale + "/" + in_file.rstrip(".j2")
def url_localized(filename):
return "../" + locale + "/" + filename
def url(x):
# TODO: look at the app root environment variable
# TODO: check if file exists
return "../" + x
import codecs
f = codecs.open("./" + locale + "/" + in_file.rstrip(".j2"), "w", "utf-8")
f.write(tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized))
f.close()
|