aboutsummaryrefslogtreecommitdiff
path: root/template.py
diff options
context:
space:
mode:
Diffstat (limited to 'template.py')
-rwxr-xr-xtemplate.py59
1 files changed, 31 insertions, 28 deletions
diff --git a/template.py b/template.py
index f872ad3e..179e32a7 100755
--- a/template.py
+++ b/template.py
@@ -8,45 +8,48 @@
8# Note that the gettext files need to be prepared first. This script 8# Note that the gettext files need to be prepared first. This script
9# is thus to be invoked via the Makefile. 9# is thus to be invoked via the Makefile.
10import os 10import os
11import os.path
11import sys 12import sys
12import re 13import re
13import gettext 14import gettext
14import jinja2 15import jinja2
16import glob
17import codecs
15 18
16if len(sys.argv) < 3: 19env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
17 sys.exit("Usage: " + __file__ + " <template-file> <locale> <output-file>") 20 extensions=["jinja2.ext.i18n"],
21 autoescape=False)
18 22
19in_file = sys.argv[1]
20locale = sys.argv[2]
21 23
22name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() 24for in_file in glob.glob("*.j2"):
25 name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups()
26 tmpl = env.get_template(in_file)
23 27
24tr = gettext.translation("messages", 28 def self_localized(other_locale):
25 localedir="locale", 29 """
26 languages=[locale]) 30 Return URL for the current page in another locale.
31 """
32 return "../" + other_locale + "/" + in_file.rstrip(".j2")
27 33
28env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), 34 def url_localized(filename):
29 extensions=["jinja2.ext.i18n"], 35 return "../" + locale + "/" + filename
30 autoescape=False)
31env.install_gettext_translations(tr, newstyle=True)
32 36
33tmpl = env.get_template(in_file) 37 def url(x):
38 # TODO: look at the app root environment variable
39 # TODO: check if file exists
40 return "../" + x
34 41
35def self_localized(other_locale): 42 for l in glob.glob("locale/*/"):
36 """ 43 locale = os.path.basename(l[:-1])
37 Return URL for the current page in another locale.
38 """
39 return "../" + other_locale + "/" + in_file.rstrip(".j2")
40 44
41def url_localized(filename): 45 tr = gettext.translation("messages",
42 return "../" + locale + "/" + filename 46 localedir="locale",
47 languages=[locale])
43 48
44def url(x): 49 env.install_gettext_translations(tr, newstyle=True)
45 # TODO: look at the app root environment variable
46 # TODO: check if file exists
47 return "../" + x
48 50
49import codecs 51
50f = codecs.open("./" + locale + "/" + in_file.rstrip(".j2"), "w", "utf-8") 52 content = tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized)
51f.write(tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized)) 53 out_name = "./" + locale + "/" + in_file.rstrip(".j2")
52f.close() 54 with codecs.open(out_name, "w", "utf-8") as f:
55 f.write(content)