-
Notifications
You must be signed in to change notification settings - Fork 12
/
words.js
199 lines (186 loc) · 5.78 KB
/
words.js
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
const fs = require('fs');
const pkg = require('./package.json');
const iopackage = require('./io-package.json');
const fileName = 'words.js';
const EMPTY = '';
const languages = {
en: {},
de: {},
ru: {},
pt: {},
nl: {},
fr: {},
it: {},
es: {},
pl: {},
uk: {},
'zh-cn': {}
};
function lang2data(lang) {
let str ='{\n';
let count = 0;
for (const w in lang) {
if (lang.hasOwnProperty(w)) {
count++;
const key = ' "' + w.replace(/"/g, '\\"') + '": ';
str += key + '"' + lang[w].replace(/"/g, '\\"') + '",\n';
}
}
if (!count) {
return '{\n}';
} else {
return str.substring(0, str.length - 2) + '\n}';
}
}
function readWordJs(src) {
try {
let words;
if (fs.existsSync(src + 'js/' + fileName)) {
words = fs.readFileSync(src + 'js/' + fileName).toString();
} else {
words = fs.readFileSync(src + fileName).toString();
}
words = words.substring(words.indexOf('{'), words.length);
words = words.substring(0, words.lastIndexOf(';'));
const resultFunc = new Function('return ' + words + ';');
return resultFunc();
} catch (e) {
return null;
}
}
function padRight(text, totalLength) {
return text + (text.length < totalLength ? new Array(totalLength - text.length).join(' ') : '');
}
function writeWordJs(data, src) {
let text = '';
text += '/*global systemDictionary:true */\n';
text += "'use strict';\n\n";
text += 'systemDictionary = {\n';
for (const word in data) {
if (data.hasOwnProperty(word)) {
text += ' ' + padRight('"' + word.replace(/"/g, '\\"') + '": {', 50);
let line = '';
for (const lang in data[word]) {
if (data[word].hasOwnProperty(lang)) {
line += '"' + lang + '": "' + padRight(data[word][lang].replace(/"/g, '\\"') + '",', 50) + ' ';
}
}
if (line) {
line = line.trim();
line = line.substring(0, line.length - 1);
}
text += line + '},\n';
}
}
text += '};';
if (fs.existsSync(src + 'js/' + fileName)) {
fs.writeFileSync(src + 'js/' + fileName, text);
} else {
fs.writeFileSync(src + '' + fileName, text);
}
}
function words2languages(src) {
const langs = Object.assign({}, languages);
const data = readWordJs(src);
if (data) {
for (const word in data) {
if (data.hasOwnProperty(word)) {
for (const lang in data[word]) {
if (data[word].hasOwnProperty(lang)) {
langs[lang][word] = data[word][lang];
// pre-fill all other languages
for (const j in langs) {
if (langs.hasOwnProperty(j)) {
langs[j][word] = langs[j][word] || EMPTY;
}
}
}
}
}
}
if (!fs.existsSync(src + 'i18n/')) {
fs.mkdirSync(src + 'i18n/');
}
for (const l in langs) {
if (!langs.hasOwnProperty(l))
continue;
const keys = Object.keys(langs[l]);
keys.sort();
const obj = {};
for (let k = 0; k < keys.length; k++) {
obj[keys[k]] = langs[l][keys[k]];
}
if (!fs.existsSync(src + 'i18n/' + l)) {
fs.mkdirSync(src + 'i18n/' + l);
}
fs.writeFileSync(src + 'i18n/' + l + '/translations.json', lang2data(obj));
}
} else {
console.error('Cannot read or parse ' + fileName);
}
}
function languages2words(src) {
const dirs = fs.readdirSync(src + 'i18n/');
const langs = {};
const bigOne = {};
const order = Object.keys(languages);
dirs.sort(function (a, b) {
const posA = order.indexOf(a);
const posB = order.indexOf(b);
if (posA === -1 && posB === -1) {
if (a > b)
return 1;
if (a < b)
return -1;
return 0;
} else if (posA === -1) {
return -1;
} else if (posB === -1) {
return 1;
} else {
if (posA > posB)
return 1;
if (posA < posB)
return -1;
return 0;
}
});
for (const lang of dirs) {
if (lang === 'flat.txt')
continue;
langs[lang] = fs.readFileSync(`${src}i18n/${lang}/translations.json`).toString();
langs[lang] = JSON.parse(langs[lang]);
const words = langs[lang];
for (const word in words) {
if (words.hasOwnProperty(word)) {
bigOne[word] = bigOne[word] || {};
if (words[word] !== EMPTY) {
bigOne[word][lang] = words[word];
}
}
}
}
// read actual words.js
const aWords = readWordJs();
const temporaryIgnore = ['flat.txt'];
if (aWords) {
// Merge words together
for (const w in aWords) {
if (aWords.hasOwnProperty(w)) {
if (!bigOne[w]) {
console.warn(`Take from actual words.js: ${w}`);
bigOne[w] = aWords[w];
}
dirs.forEach(function (lang) {
if (temporaryIgnore.indexOf(lang) !== -1)
return;
if (!bigOne[w][lang]) {
console.warn(`Missing "${lang}": ${w}`);
}
});
}
}
}
writeWordJs(bigOne, src);
}
languages2words(`${__dirname}/admin/`);