-
Notifications
You must be signed in to change notification settings - Fork 9
/
i18n_api.py
176 lines (139 loc) · 4.49 KB
/
i18n_api.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
from api import *
from commons import *
from app import app
aqlc.create_collection('translations')
@register('set_locale')
def _():
j = g.j
locale = es('locale')
if locale not in dict_of_languages or locale=='expl':
raise Exception('this locale is not yet supported here.')
return {'error':False,'set_locale':locale}
@register('get_allowed_languages')
def _():
return {'allowed_languages':dict_of_languages}
@register('update_translation')
def _():
j = g.j
must_be_logged_in()
banned_check()
if current_user_doesnt_have_enough_likes():
raise Exception('you don\'t have enough likes to submit translation')
original = es('original')
o2=j['original']
lang = es('lang')
string = es('string')
uid = g.current_user['uid']
if lang not in trans.allowed_languages:
raise Exception('this language is not supported yet.')
# if original not in trans.d:
# print(original)
# print(o2)
# raise Exception('original text not in trans.d')
t_c = time_iso_now()
new_trans = dict(
original=original,
lang=lang,
string=string,
uid=uid,
t_c=t_c,
t_u=t_c,
approved=False,
)
aql('''
let d = @nt
upsert {uid:d.uid, lang:d.lang, original:d.original}
insert d update {string:d.string, t_u:d.t_c} into translations
''', nt=new_trans)
return {}
@register('approve_translation')
def _():
must_be_logged_in()
must_be_admin()
trans_id = es('id')
delete = eb('delete')
aql('''
for t in translations
filter t._key==@key
update t with {approved:@approved} in translations
''', key=trans_id, approved=not delete)
return {}
def textualize(s):
resp = make_response(s, 200)
resp.headers['Content-type'] = 'text/plain; charset=utf-8'
return resp
def get_all_translations():
all_translations = aql('''
for t in translations
let user = (for i in users filter i.uid==t.uid return i)[0]
collect original = t.original into groups = merge(t, {user})
return {original, groups}
''', silent=True)
return all_translations
# for d in all_translations:
# original = d['original']
@stale_cache(ttr=10, ttl=1200) # database to code form
def at2d2():
at = get_all_translations()
d2 = {}
for d in at:
orig = d['original']
lots = d['groups']
d2[orig] = d2o = {}
for t in lots: # t -> translation object {original, string, lang}
t_lang = t['lang']
if t['approved']:
if t_lang not in d2o:
d2o[t_lang] = t
else:
if d2o[t_lang]['user']['pagerank'] < t['user']['pagerank']:
d2o[t_lang] = t
for t_lang in d2o:
d2o[t_lang] = d2o[t_lang]['string']
return d2
@stale_cache(ttr=3, ttl=120) # code to database form
def d2at():
if not trans.scanned:
trans.scan_dir_and_extract()
d = trans.d
l = []
for original,langs in d.items():
od = dict(original=original, groups=[])
groups = od['groups']
l.append(od)
# commented out because jinja2 is inspect-unfriendly
# ts2o = trans.s2[original]
# fn,ln = ts2o['filename'], ts2o['lineno']
for lang, string in langs.items():
nd = dict(lang=lang, string=string)
if original in trans.s2:
nd['filename'] = trans.s2[original]['filename']
nd['lineno'] = trans.s2[original]['lineno']
nd['original'] = original
# dict(lang=lang, string=string, filename=fn, lineno=ln)
groups.append(nd)
return l
trans.get_d2 = at2d2
@app.route('/translations')
def translations_page():
at = get_all_translations() # from database
at2 = d2at() # from code
ld = {}
for d in at2+at:
original = d['original']
if original not in ld:
ld[original] = []
ld[original]+=d['groups']
l = [(k,v) for k,v in ld.items()]
l.sort(key=lambda t:t[0])
return render_template_g('trans.html.jinja', translations=l, page_title='翻译')
@app.route('/translations/list_allowed_languages')
def list_allowed_languages():
tal = trans.allowed_languages
s = '\n'.join((str(k) +' '+ repr(v) for k,v in tal.items()))
return textualize(s)
@app.route('/translations/list_translations')
def list_translations():
d = at2d2()
s = '\n'.join((str(k) +' '+ repr(v) for k,v in d.items()))
return textualize(s)