-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
generate
executable file
·75 lines (61 loc) · 2.05 KB
/
generate
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /usr/bin/env python
import os
import babel
import babel.dates
import hashlib
import datetime
from gettext import GNUTranslations
from jinja2 import Environment, FileSystemLoader
def md5_filter(value):
return hashlib.md5(value).hexdigest()
def format_datetime(year, month, day):
return babel.dates.format_date(datetime.date(year, month, day),
locale=env.globals["locale"])
env = Environment(loader=FileSystemLoader('templates'),
extensions=['jinja2.ext.i18n'])
env.filters["md5"] = md5_filter
env.globals["datetime"] = format_datetime
templates = [
"index.html",
"features.html",
"download.html",
"docs.html",
"support.html",
"development.html",
"roadmap.html",
"donate.html",
"shell.html",
]
BASE_URL_PREFIX = os.path.join('/', os.environ.get('BASE_URL_PREFIX', ''))
STATIC_PATH = os.path.join(BASE_URL_PREFIX, "static")
# Sorted alphabetically:
languages = ["cs", "de", "en", "fr", "nl", "pt", "ru", "zh", "es"]
print(f"STATIC_PATH: {STATIC_PATH}")
for language in languages:
print("Generating '%s' pages" % language)
env.globals["locale"] = language
if language == "en":
# The templates are written in English, so they don't need to be
# translated
env.install_null_translations()
else:
translations = GNUTranslations(open("i18n/%s.mo" % language, 'rb'))
env.install_gettext_translations(translations)
for template in templates:
t = env.get_template(template)
print("Processing '%s'" % template)
name = os.path.splitext(template)[0]
s = t.render(
{
name + "_active": "active",
"static_path": STATIC_PATH,
"copyright_year": datetime.datetime.utcnow().year
}
)
os.makedirs(os.path.join('build', language), exist_ok=True)
f = open(os.path.join('build', language, template), "wb")
try:
f.write(s.encode("utf-8"))
f.write(b"\n")
finally:
f.close()