diff options
Diffstat (limited to 'template.py')
-rwxr-xr-x | template.py | 59 |
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. |
10 | import os | 10 | import os |
11 | import os.path | ||
11 | import sys | 12 | import sys |
12 | import re | 13 | import re |
13 | import gettext | 14 | import gettext |
14 | import jinja2 | 15 | import jinja2 |
16 | import glob | ||
17 | import codecs | ||
15 | 18 | ||
16 | if len(sys.argv) < 3: | 19 | env = 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 | ||
19 | in_file = sys.argv[1] | ||
20 | locale = sys.argv[2] | ||
21 | 23 | ||
22 | name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() | 24 | for 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 | ||
24 | tr = 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 | ||
28 | env = 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) | ||
31 | env.install_gettext_translations(tr, newstyle=True) | ||
32 | 36 | ||
33 | tmpl = 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 | ||
35 | def 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 | ||
41 | def url_localized(filename): | 45 | tr = gettext.translation("messages", |
42 | return "../" + locale + "/" + filename | 46 | localedir="locale", |
47 | languages=[locale]) | ||
43 | 48 | ||
44 | def 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 | ||
49 | import codecs | 51 | |
50 | f = 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) |
51 | f.write(tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized)) | 53 | out_name = "./" + locale + "/" + in_file.rstrip(".j2") |
52 | f.close() | 54 | with codecs.open(out_name, "w", "utf-8") as f: |
55 | f.write(content) | ||