ztechno_core 0.0.1 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/crypto_service.d.ts +10 -10
- package/lib/crypto_service.js +77 -59
- package/lib/index.d.ts +5 -0
- package/lib/index.js +33 -0
- package/lib/mail_service.d.ts +19 -19
- package/lib/mail_service.js +35 -34
- package/lib/sql_service.d.ts +8 -7
- package/lib/sql_service.js +223 -78
- package/lib/translate_service.d.ts +45 -46
- package/lib/translate_service.js +436 -243
- package/package.json +5 -5
package/lib/translate_service.js
CHANGED
|
@@ -1,243 +1,436 @@
|
|
|
1
|
-
|
|
2
|
-
var __awaiter =
|
|
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
|
-
return
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value));
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
var __generator =
|
|
34
|
+
(this && this.__generator) ||
|
|
35
|
+
function (thisArg, body) {
|
|
36
|
+
var _ = {
|
|
37
|
+
label: 0,
|
|
38
|
+
sent: function () {
|
|
39
|
+
if (t[0] & 1) throw t[1];
|
|
40
|
+
return t[1];
|
|
41
|
+
},
|
|
42
|
+
trys: [],
|
|
43
|
+
ops: [],
|
|
44
|
+
},
|
|
45
|
+
f,
|
|
46
|
+
y,
|
|
47
|
+
t,
|
|
48
|
+
g;
|
|
49
|
+
return (
|
|
50
|
+
(g = { next: verb(0), throw: verb(1), return: verb(2) }),
|
|
51
|
+
typeof Symbol === 'function' &&
|
|
52
|
+
(g[Symbol.iterator] = function () {
|
|
53
|
+
return this;
|
|
54
|
+
}),
|
|
55
|
+
g
|
|
56
|
+
);
|
|
57
|
+
function verb(n) {
|
|
58
|
+
return function (v) {
|
|
59
|
+
return step([n, v]);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function step(op) {
|
|
63
|
+
if (f) throw new TypeError('Generator is already executing.');
|
|
64
|
+
while ((g && ((g = 0), op[0] && (_ = 0)), _))
|
|
65
|
+
try {
|
|
66
|
+
if (
|
|
67
|
+
((f = 1),
|
|
68
|
+
y &&
|
|
69
|
+
(t = op[0] & 2 ? y['return'] : op[0] ? y['throw'] || ((t = y['return']) && t.call(y), 0) : y.next) &&
|
|
70
|
+
!(t = t.call(y, op[1])).done)
|
|
71
|
+
)
|
|
72
|
+
return t;
|
|
73
|
+
if (((y = 0), t)) op = [op[0] & 2, t.value];
|
|
74
|
+
switch (op[0]) {
|
|
75
|
+
case 0:
|
|
76
|
+
case 1:
|
|
77
|
+
t = op;
|
|
78
|
+
break;
|
|
79
|
+
case 4:
|
|
80
|
+
_.label++;
|
|
81
|
+
return { value: op[1], done: false };
|
|
82
|
+
case 5:
|
|
83
|
+
_.label++;
|
|
84
|
+
y = op[1];
|
|
85
|
+
op = [0];
|
|
86
|
+
continue;
|
|
87
|
+
case 7:
|
|
88
|
+
op = _.ops.pop();
|
|
89
|
+
_.trys.pop();
|
|
90
|
+
continue;
|
|
91
|
+
default:
|
|
92
|
+
if (!((t = _.trys), (t = t.length > 0 && t[t.length - 1])) && (op[0] === 6 || op[0] === 2)) {
|
|
93
|
+
_ = 0;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
|
|
97
|
+
_.label = op[1];
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
101
|
+
_.label = t[1];
|
|
102
|
+
t = op;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
if (t && _.label < t[2]) {
|
|
106
|
+
_.label = t[2];
|
|
107
|
+
_.ops.push(op);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
if (t[2]) _.ops.pop();
|
|
111
|
+
_.trys.pop();
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
op = body.call(thisArg, _);
|
|
115
|
+
} catch (e) {
|
|
116
|
+
op = [6, e];
|
|
117
|
+
y = 0;
|
|
118
|
+
} finally {
|
|
119
|
+
f = t = 0;
|
|
120
|
+
}
|
|
121
|
+
if (op[0] & 5) throw op[1];
|
|
122
|
+
return { value: op[0] ? op[1] : void 0, done: true };
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
126
|
+
exports.ZTranslateService = void 0;
|
|
127
|
+
var sql_service_1 = require('./sql_service');
|
|
128
|
+
var DomParser = require('dom-parser');
|
|
129
|
+
var translate = require('translate');
|
|
130
|
+
var htmlParser = new DomParser();
|
|
131
|
+
var sql = sql_service_1.ZSqlService.get();
|
|
132
|
+
var instance = null;
|
|
133
|
+
var ZTranslateService = /** @class */ (function () {
|
|
134
|
+
function ZTranslateService() {
|
|
135
|
+
var _a;
|
|
136
|
+
var _this = this;
|
|
137
|
+
this.localCache = {};
|
|
138
|
+
this.surpressErrors = true;
|
|
139
|
+
this.codes =
|
|
140
|
+
((_a = {}),
|
|
141
|
+
(_a['''] = "'"),
|
|
142
|
+
(_a['"'] = '"'),
|
|
143
|
+
(_a['“'] = '\u201C'),
|
|
144
|
+
(_a['”'] = '\u201D'),
|
|
145
|
+
(_a['©'] = '\u00A9'),
|
|
146
|
+
(_a['®'] = '\u00AE'),
|
|
147
|
+
(_a['€'] = '\u20AC'),
|
|
148
|
+
(_a['£'] = '\u00A3'),
|
|
149
|
+
(_a['™'] = '\u2122'),
|
|
150
|
+
_a);
|
|
151
|
+
this.getLanguages().map(function (lang) {
|
|
152
|
+
return (_this.localCache[lang] = {});
|
|
153
|
+
});
|
|
154
|
+
setInterval(function () {
|
|
155
|
+
return _this.clearLocalCache();
|
|
156
|
+
}, 1000 * 60 * 60); // Every Hour
|
|
157
|
+
}
|
|
158
|
+
ZTranslateService.prototype.getLanguages = function () {
|
|
159
|
+
return ['en', 'nl'];
|
|
160
|
+
};
|
|
161
|
+
ZTranslateService.prototype.getSourceLang = function () {
|
|
162
|
+
return 'nl';
|
|
163
|
+
};
|
|
164
|
+
ZTranslateService.prototype.getDefaultLang = function () {
|
|
165
|
+
return 'nl';
|
|
166
|
+
};
|
|
167
|
+
ZTranslateService.init = function (opt) {
|
|
168
|
+
translate.key = opt.key;
|
|
169
|
+
};
|
|
170
|
+
ZTranslateService.prototype.getLang = function (cookies) {
|
|
171
|
+
var defaultLang = this.getDefaultLang();
|
|
172
|
+
var langKey = (cookies.lang || defaultLang).toLowerCase();
|
|
173
|
+
var langs = this.getLanguages();
|
|
174
|
+
if (!langs.includes(langKey)) {
|
|
175
|
+
return defaultLang;
|
|
176
|
+
}
|
|
177
|
+
return langKey;
|
|
178
|
+
};
|
|
179
|
+
ZTranslateService.prototype.translateText = function (langOrReq, text) {
|
|
180
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
181
|
+
var lang, replaceCount, codeIndexStart, first, codeLength, code, localCached, remoteCached, result, err_1;
|
|
182
|
+
return __generator(this, function (_a) {
|
|
183
|
+
switch (_a.label) {
|
|
184
|
+
case 0:
|
|
185
|
+
lang = typeof langOrReq === 'string' ? langOrReq : this.getLang(langOrReq.cookies);
|
|
186
|
+
text = text.trim();
|
|
187
|
+
if (text.length === 1) {
|
|
188
|
+
return [2 /*return*/, text];
|
|
189
|
+
}
|
|
190
|
+
replaceCount = 0;
|
|
191
|
+
while (text.includes('&#')) {
|
|
192
|
+
codeIndexStart = text.indexOf('&#');
|
|
193
|
+
first = text.substring(codeIndexStart);
|
|
194
|
+
codeLength = first.indexOf('') + 1;
|
|
195
|
+
code = first.substring(0, codeLength);
|
|
196
|
+
if (this.codes[code] === undefined) {
|
|
197
|
+
throw new Error('Cant recognize character code="'.concat(code, '"\n for text=').concat(text, '\n\n'));
|
|
198
|
+
// return text
|
|
199
|
+
}
|
|
200
|
+
text = text.substring(0, codeIndexStart) + this.codes[text] + text.substring(codeIndexStart + codeLength);
|
|
201
|
+
// text = text.replace(code, codes[text])
|
|
202
|
+
if (replaceCount++ > 1000) {
|
|
203
|
+
throw new Error(
|
|
204
|
+
'Replace Count > 1000!!! character code="'.concat(code, '"\n for text=').concat(text, '\n\n'),
|
|
205
|
+
);
|
|
206
|
+
// return text
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
localCached = this.checkLocalCache(text, lang);
|
|
210
|
+
if (localCached !== false) {
|
|
211
|
+
return [2 /*return*/, localCached.value];
|
|
212
|
+
}
|
|
213
|
+
return [4 /*yield*/, this.fetch(text, lang)];
|
|
214
|
+
case 1:
|
|
215
|
+
remoteCached = _a.sent();
|
|
216
|
+
if (remoteCached !== false) {
|
|
217
|
+
return [2 /*return*/, remoteCached.value];
|
|
218
|
+
}
|
|
219
|
+
_a.label = 2;
|
|
220
|
+
case 2:
|
|
221
|
+
_a.trys.push([2, 4, , 5]);
|
|
222
|
+
return [
|
|
223
|
+
4 /*yield*/,
|
|
224
|
+
translate(text, {
|
|
225
|
+
from: this.getSourceLang(),
|
|
226
|
+
to: lang,
|
|
227
|
+
}),
|
|
228
|
+
];
|
|
229
|
+
case 3:
|
|
230
|
+
result = _a.sent();
|
|
231
|
+
return [3 /*break*/, 5];
|
|
232
|
+
case 4:
|
|
233
|
+
err_1 = _a.sent();
|
|
234
|
+
result = '?';
|
|
235
|
+
return [3 /*break*/, 5];
|
|
236
|
+
case 5:
|
|
237
|
+
return [4 /*yield*/, this.insert(text, lang, { value: result })];
|
|
238
|
+
case 6:
|
|
239
|
+
_a.sent();
|
|
240
|
+
return [2 /*return*/, result];
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
ZTranslateService.prototype.translateHtml = function (html, cookies) {
|
|
246
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
247
|
+
var lang, defaultLang, dom, htmlNodes, mainNodes, isView, domNode, node, promises, output;
|
|
248
|
+
return __generator(this, function (_a) {
|
|
249
|
+
switch (_a.label) {
|
|
250
|
+
case 0:
|
|
251
|
+
lang = this.getLang(cookies);
|
|
252
|
+
defaultLang = this.getDefaultLang();
|
|
253
|
+
dom = htmlParser.parseFromString(html);
|
|
254
|
+
htmlNodes = dom.getElementsByTagName('html');
|
|
255
|
+
mainNodes = dom.getElementsByTagName('main');
|
|
256
|
+
isView = htmlNodes.length === 0;
|
|
257
|
+
domNode = isView ? mainNodes[0] : htmlNodes[0];
|
|
258
|
+
if (!(lang !== defaultLang)) return [3 /*break*/, 2];
|
|
259
|
+
node = isView ? domNode : domNode.getElementsByTagName('body')[0];
|
|
260
|
+
promises = [];
|
|
261
|
+
this.translateHtmlRec(lang, node, promises);
|
|
262
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
263
|
+
case 1:
|
|
264
|
+
_a.sent();
|
|
265
|
+
_a.label = 2;
|
|
266
|
+
case 2:
|
|
267
|
+
output = domNode ? domNode.outerHTML : html;
|
|
268
|
+
return [2 /*return*/, output.startsWith('<!DOCTYPE html>') ? output : '<!DOCTYPE html>\r\n'.concat(output)];
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
ZTranslateService.prototype.translateHtmlRec = function (lang, node, promises, skipTranslate) {
|
|
274
|
+
var _this = this;
|
|
275
|
+
if (skipTranslate === void 0) {
|
|
276
|
+
skipTranslate = false;
|
|
277
|
+
}
|
|
278
|
+
if (node.getAttribute('notranslate') != null) {
|
|
279
|
+
skipTranslate = true;
|
|
280
|
+
}
|
|
281
|
+
if (node.nodeName === '#text') {
|
|
282
|
+
var nodeText = node;
|
|
283
|
+
var text_1 = nodeText.text.replace(/[\r|\n|\r\n]+/g, ' ').replace(/\s\s+/g, ' ');
|
|
284
|
+
var value = text_1.trim();
|
|
285
|
+
var meta_1 = {
|
|
286
|
+
prefix: genSpaces(text_1.length - text_1.trimStart().length),
|
|
287
|
+
suffix: genSpaces(text_1.length - text_1.trimEnd().length),
|
|
288
|
+
};
|
|
289
|
+
if (skipTranslate === true || text_1.length === 0 || !strContainsLetters(text_1)) {
|
|
290
|
+
node.text = meta_1.prefix + text_1 + meta_1.suffix;
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
promises.push(
|
|
294
|
+
this.translateText(lang, value)
|
|
295
|
+
.then(function (translatedText) {
|
|
296
|
+
node.text = meta_1.prefix + translatedText + meta_1.suffix;
|
|
297
|
+
})
|
|
298
|
+
.catch(function (err) {
|
|
299
|
+
node.text = text_1;
|
|
300
|
+
if (!_this.surpressErrors) {
|
|
301
|
+
throw err; // TODO: Find out if surpressing is better
|
|
302
|
+
}
|
|
303
|
+
}),
|
|
304
|
+
);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
// const hasChildren = node.childNodes !== undefined
|
|
308
|
+
for (var _i = 0, _a = node.childNodes || []; _i < _a.length; _i++) {
|
|
309
|
+
var child = _a[_i];
|
|
310
|
+
this.translateHtmlRec(lang, child, promises, skipTranslate);
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
ZTranslateService.prototype.update = function (key, lang, data) {
|
|
314
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
315
|
+
return __generator(this, function (_a) {
|
|
316
|
+
switch (_a.label) {
|
|
317
|
+
case 0:
|
|
318
|
+
return [
|
|
319
|
+
4 /*yield*/,
|
|
320
|
+
sql.query(
|
|
321
|
+
'\n INSERT INTO translations\n (`key`, `lang`, `value`)\n VALUES\n (?, ?, ?)\n ON DUPLICATE KEY UPDATE value=?\n ',
|
|
322
|
+
[key, lang, data.value, data.value],
|
|
323
|
+
),
|
|
324
|
+
];
|
|
325
|
+
case 1:
|
|
326
|
+
return [2 /*return*/, _a.sent()];
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
};
|
|
331
|
+
ZTranslateService.prototype.checkLocalCache = function (key, lang) {
|
|
332
|
+
var hasLocal = !this.localCache[lang].hasOwnProperty(key);
|
|
333
|
+
return hasLocal ? false : this.localCache[lang][key];
|
|
334
|
+
};
|
|
335
|
+
ZTranslateService.prototype.insertLocalCache = function (key, lang, data) {
|
|
336
|
+
if (this.localCache[lang].hasOwnProperty(key)) {
|
|
337
|
+
// console.warn(`Translations already cached!`, { key, lang, data})
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
this.localCache[lang][key] = data;
|
|
341
|
+
};
|
|
342
|
+
ZTranslateService.prototype.clearLocalCache = function () {
|
|
343
|
+
var _this = this;
|
|
344
|
+
Object.keys(this.localCache).map(function (k) {
|
|
345
|
+
_this.localCache[k] = {};
|
|
346
|
+
});
|
|
347
|
+
};
|
|
348
|
+
ZTranslateService.prototype.fetch = function (key, lang) {
|
|
349
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
350
|
+
var results, value;
|
|
351
|
+
return __generator(this, function (_a) {
|
|
352
|
+
switch (_a.label) {
|
|
353
|
+
case 0:
|
|
354
|
+
return [4 /*yield*/, sql.query('SELECT `value` FROM translations WHERE `lang`=? AND `key`=?', [lang, key])];
|
|
355
|
+
case 1:
|
|
356
|
+
results = _a.sent();
|
|
357
|
+
if (results.length > 0) {
|
|
358
|
+
value = results[0].value;
|
|
359
|
+
this.insertLocalCache(key, lang, { value: value });
|
|
360
|
+
return [2 /*return*/, { value: value }];
|
|
361
|
+
}
|
|
362
|
+
return [2 /*return*/, false];
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
ZTranslateService.prototype.insert = function (key, lang, data) {
|
|
368
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
369
|
+
return __generator(this, function (_a) {
|
|
370
|
+
switch (_a.label) {
|
|
371
|
+
case 0:
|
|
372
|
+
return [
|
|
373
|
+
4 /*yield*/,
|
|
374
|
+
sql.query('INSERT IGNORE INTO translations (`key`, `lang`, `value`) VALUES (?, ?, ?)', [
|
|
375
|
+
key,
|
|
376
|
+
lang,
|
|
377
|
+
data.value,
|
|
378
|
+
]),
|
|
379
|
+
];
|
|
380
|
+
case 1:
|
|
381
|
+
_a.sent();
|
|
382
|
+
return [2 /*return*/];
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
};
|
|
387
|
+
ZTranslateService.prototype.fetchLang = function (lang) {
|
|
388
|
+
return sql.query('SELECT `key`, `lang`, `value`, `verified`, `created_at` FROM translations WHERE `lang`=?', [
|
|
389
|
+
lang,
|
|
390
|
+
]);
|
|
391
|
+
};
|
|
392
|
+
ZTranslateService.prototype.fetchAllGrouped = function () {
|
|
393
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
394
|
+
var output, allTranslations;
|
|
395
|
+
return __generator(this, function (_a) {
|
|
396
|
+
switch (_a.label) {
|
|
397
|
+
case 0:
|
|
398
|
+
output = {};
|
|
399
|
+
return [4 /*yield*/, this.fetchAll()];
|
|
400
|
+
case 1:
|
|
401
|
+
allTranslations = _a.sent();
|
|
402
|
+
allTranslations.map(function (translation) {
|
|
403
|
+
var key = translation.key;
|
|
404
|
+
if (!output.hasOwnProperty(key)) {
|
|
405
|
+
output[key] = [];
|
|
406
|
+
}
|
|
407
|
+
output[key].push(translation);
|
|
408
|
+
});
|
|
409
|
+
return [2 /*return*/, output];
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
ZTranslateService.prototype.fetchAll = function () {
|
|
415
|
+
return sql.query('SELECT `key`, `lang`, `value`, `verified`, `created_at` FROM translations');
|
|
416
|
+
};
|
|
417
|
+
ZTranslateService.get = function () {
|
|
418
|
+
if (instance == null) {
|
|
419
|
+
instance = new ZTranslateService();
|
|
420
|
+
}
|
|
421
|
+
return instance;
|
|
422
|
+
};
|
|
423
|
+
return ZTranslateService;
|
|
424
|
+
})();
|
|
425
|
+
exports.ZTranslateService = ZTranslateService;
|
|
426
|
+
function strContainsLetters(text) {
|
|
427
|
+
var regExp = /[a-zA-Z]/g;
|
|
428
|
+
return regExp.test(text);
|
|
429
|
+
}
|
|
430
|
+
function genSpaces(length) {
|
|
431
|
+
var output = '';
|
|
432
|
+
for (var i = 0; i < length; i++) {
|
|
433
|
+
output += ' ';
|
|
434
|
+
}
|
|
435
|
+
return output;
|
|
436
|
+
}
|