-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
100 lines (100 loc) · 2.43 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>i18n Database Translations</title>
<style>
html {
font-family: sans-serif;
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
table {
border-left: 1px solid #CCC;
border-right: 1px solid #CCC;
border-bottom: 1px solid #CCC;
}
table, textarea {
width: 100%;
}
td {
padding: 8px;
}
tr.native {
background: #F5F5F5;
}
tr.native > td {
border-top: 1px solid #CCC;
border-bottom: 1px solid #DDD;
}
tr.native > td:first-child {
font-weight: bold;
}
button {
margin: 8px auto;
width: 100%;
min-height: 40px;
font-weight: bold;
}
</style>
<script>
const i18n=/*${db}*/;
function updateDB() {
[].forEach.call(document.querySelectorAll('tr.native'), updateDBEntry);
fetch('/update', {
method: 'post',
body: JSON.stringify(i18n.db)
})
.then(response => response.text())
.then(text => {
if (confirm('Database updated.\nWould you like to exit?')) {
fetch('/exit').then(response => response.text()).then(() => close());
}
})
.catch(e => alert(e))
;
}
function updateDBEntry(row) {
var
key = row.getAttribute('data-key'),
lang = row.querySelector('td').textContent,
updates = [row]
;
while (
row &&
(row = row.nextElementSibling) &&
(row.className !== 'native')
) {
updates.push(row);
}
row = updates.shift();
updates.forEach(parseDBEntry, {
key,
values: i18n.db[key][lang].v
});
}
function parseDBEntry(row) {
const lang = row.querySelector('td').textContent;
const text = row.querySelector('textarea').value;
const grab = (t, ...v) => {
i18n.db[this.key][lang] = {
t: t.slice(),
v: v.map(value => {
const i = this.values.indexOf(value);
if (i < 0) throw new Error(`Unable to define ${lang} sentence: ${text}`);
return i;
})
};
};
eval('grab`' + text + '`');
}
</script>
</head>
<body>
<!--${table}-->
<button onclick="updateDB()">update translations</button>
</body>
</html>