-
Notifications
You must be signed in to change notification settings - Fork 3
/
i18n.py
executable file
·208 lines (169 loc) · 5.36 KB
/
i18n.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
'''i18n file generator
Usage:
i18n ls
i18n bootstrap [-w]
i18n gen [--lang LANG] [-w]
Commands:
ls List i18n items
bootstrap Initialiaz .toml file from a elm file containing traductions
gen generate an elm file in the given lang.
Options:
-w, --write save/replace in file.
-l, --lang lang to use (DEFAULT: en).
Examples:
i18n.py ls
i18n.py gen -w -l fr
'''
import os
import re
from string import Template
from docopt import docopt
elm_header = """\
module Text exposing (..)
{-
Auto-generated i18n.
DO NOT EDIT
-}
"""
elm_entry_template = """\
{k} : String
{k} =
{v}
"""
class I18N(object):
src_path = "src/"
i18n_input = "i18n/i18n.toml"
i18n_output = "src/Text.elm"
default_lang = "en"
def __init__(self, conf):
self.conf = conf
def run(self):
q = self.conf
if q["ls"]:
self.list_items()
elif q["bootstrap"]:
data = self.bootstrap()
if q["--write"]:
with open(self.i18n_input, "w") as _f:
for ll in data:
k = ll[0]
v = ll[1]
_f.write("[%s]\n" % k)
_f.write(" en=%s\n" % v)
_f.write("\n")
else:
print(data)
elif q["gen"]:
lang = q.get("LANG") or self.default_lang
data = self.gen(lang)
if q["--write"]:
with open(self.i18n_output, "w") as _f:
_f.write(elm_header)
for ll in data:
k = ll[1]
v = ll[2]
_f.write(elm_entry_template.format(k=k, v=v))
_f.write("\n\n")
print("%s written" % self.i18n_output)
else:
print(data)
def gen(self, lang):
with open(self.i18n_input) as _f:
lines = _f.readlines()
multiline = False
multiline_content = ""
in_entry = False # not used
entry = ""
l = "" # current lang
# data is a tuple of (#keyword identifier, #text content, #list of named arguments)
data = []
for line in lines:
line = line.strip()
if line.startswith("[") and line.endswith("]"):
# Got an entry
entry = line[1:-1]
continue
elif line.startswith("#"):
continue
if multiline:
# Multiline entry
multiline_content += line
if line.endswith('"""') or line.endswith("'''"):
multiline = False
self._append_or_replace_entry(l, entry, multiline_content, data)
else:
multiline_content += "\n"
else:
# Single line entry
ll = line.split("=")
if len(ll) < 2: continue
l = ll[0].strip()
v = "=".join(ll[1:]).strip()
if l not in [lang, self.default_lang]:
continue
if v.startswith('"""') or v.startswith("'''"):
multiline = True
multiline_content = v + "\n"
continue
self._append_or_replace_entry(l, entry, v, data)
return data
def _append_or_replace_entry(self, l, entry, v, data):
if len(data) == 0:
# Empty data
data.append([l, entry, v])
return
last_lang = data[-1][0]
last_entry = data[-1][1]
if entry != last_entry:
# Normal append
data.append([l, entry, v])
return
if l == self.default_lang:
# Do not replace default lang entry if entry already exists
return
# replace defualt lang by current
data[-1] = [l, entry, v]
def bootstrap(self):
with open("src/temp.elm") as _f:
lines = _f.readlines()
multiline = False
last_k = ""
content = ""
data = []
for l in lines:
l = l.rstrip()
if multiline:
content += l
if l.endswith('"""') or l.endswith("'''"):
multiline = False
data.append([last_k, content])
else:
content += "\n"
else:
ll = l.split("=")
if len(ll) < 2: continue
k = ll[0].strip()
v = "=".join(ll[1:]).strip()
if v.startswith('"""') or v.startswith("'''"):
multiline = True
last_k = k
content = v + "\n"
continue
data.append([k, v])
return data
def list_items(self):
with open(self.i18n_input) as _f:
lines = _f.readlines()
n_entries = 0
for l in lines:
l = l.strip()
if l.startswith("[") and l.endswith("]"):
n_entries +=1
print("Number of entries: %d" % n_entries)
# Number of entrie per lang
#grep "en=" i18n/i18n.toml | wc -l
if __name__ == "__main__":
args = docopt(__doc__, version="i18n v0")
p = I18N(args)
p.run()