aboutsummaryrefslogtreecommitdiff
path: root/template.py
blob: e86574dba47db655e5a3c9d2df65bf4998c6d149 (plain) (blame)
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
#!/usr/bin/env python

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()