-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
89 lines (70 loc) · 2.46 KB
/
main.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from os import curdir
from jinja2 import Environment, PackageLoader, select_autoescape
import json
import pkgutil
import click
import gettext
env = Environment(
loader=PackageLoader("resume"),
autoescape=select_autoescape(),
extensions=["jinja2.ext.i18n"],
)
def main(lang:str):
template = env.get_template("index.html.jinja")
localedir = curdir + '/lang'
env.install_gettext_translations(translations=gettext.translation(
domain='resume', localedir=localedir, languages=[lang]))
content = pkgutil.get_data("resume", "resume.json")
if content is None:
print("can not find resume.json in app dir")
exit(-1)
resume = json.loads(content.decode("utf-8"))
# print(resume['basics'])
with open("index.html", "w+") as f:
f.write(template.render(resume=resume, basics=resume["basics"]))
@click.group()
def cli():
"""A simple command line tool."""
pass
@cli.command()
def testtrans():
"""test translate text"""
localedir = curdir + '/lang'
gettext.bindtextdomain('resume', localedir)
gettext.textdomain('resume')
print(gettext.gettext("Links"))
@cli.command()
@click.option('-l', '--lang', help="language, en_US(defualt) or zh_CN", type=str, default='en_US')
def collectmsg(lang:str):
"""generate po file"""
template = env.get_template("index.html.jinja")
po_file = curdir + f'/lang/{lang}/LC_MESSAGES/resume.po'
with open(po_file, 'w+') as po:
po.write(
'msgid ""\n'
'msgstr ""\n'
'"Content-Type: text/plain; charset=UTF-8\\n"\n'
'"Project-Id-Version: MetaSearch 0.1-dev\\n"\n'
'"MIME-Version: 1.0\\n"\n'
)
msg_strings: list[str] = []
if template.filename is None:
print("template file is not find")
return
with open(template.filename) as f:
for (lineno, function, message) in env.extract_translations(f.read()):
if message not in msg_strings:
msg_strings.append(message)
po.write(f"#{function}:{lineno}\n")
po.write(f"msgid \"{message}\"\n")
po.write('msgstr ""\n\n')
@cli.command()
@click.option('-l', '--lang', help="language, en_US(defualt) or zh_CN", type=str, default='en_US')
def gen(lang:str):
"""generate html"""
main(lang)
cli.add_command(testtrans)
cli.add_command(collectmsg)
cli.add_command(gen)
if __name__ == "__main__":
cli()