ztechno_core 0.0.9 → 0.0.10
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/translate_service.d.ts +16 -4
- package/lib/translate_service.js +11 -10
- package/package.json +1 -1
|
@@ -12,14 +12,20 @@ type dbTranslationRow = {
|
|
|
12
12
|
value: string;
|
|
13
13
|
};
|
|
14
14
|
export declare class ZTranslateService {
|
|
15
|
-
private
|
|
15
|
+
private opt;
|
|
16
16
|
private localCache;
|
|
17
|
+
private get sql();
|
|
17
18
|
surpressErrors: boolean;
|
|
18
19
|
getLanguages(): string[];
|
|
19
20
|
getSourceLang(): string;
|
|
20
21
|
getDefaultLang(): string;
|
|
21
|
-
constructor(
|
|
22
|
-
|
|
22
|
+
constructor(opt: {
|
|
23
|
+
sqlService: ZSqlService;
|
|
24
|
+
googleApiKey: string;
|
|
25
|
+
languages?: string[];
|
|
26
|
+
defaultLang?: string;
|
|
27
|
+
sourceLang?: string;
|
|
28
|
+
});
|
|
23
29
|
private codes;
|
|
24
30
|
getLang(cookies: { [key: string]: string }): string;
|
|
25
31
|
translateText(langOrReq: string | any, text: string): Promise<string>;
|
|
@@ -42,6 +48,12 @@ export declare class ZTranslateService {
|
|
|
42
48
|
[key: string]: dbTranslationRow[];
|
|
43
49
|
}>;
|
|
44
50
|
private fetchAll;
|
|
45
|
-
static get(
|
|
51
|
+
static get(opt: {
|
|
52
|
+
sqlService: ZSqlService;
|
|
53
|
+
googleApiKey: string;
|
|
54
|
+
languages?: string[];
|
|
55
|
+
defaultLang?: string;
|
|
56
|
+
sourceLang?: string;
|
|
57
|
+
}): ZTranslateService;
|
|
46
58
|
}
|
|
47
59
|
export {};
|
package/lib/translate_service.js
CHANGED
|
@@ -6,17 +6,20 @@ const translate = require('translate');
|
|
|
6
6
|
const htmlParser = new DomParser();
|
|
7
7
|
let instance = null;
|
|
8
8
|
class ZTranslateService {
|
|
9
|
+
get sql() {
|
|
10
|
+
return this.opt.sqlService;
|
|
11
|
+
}
|
|
9
12
|
getLanguages() {
|
|
10
|
-
return ['en', 'nl'];
|
|
13
|
+
return this.opt.languages || ['en', 'nl'];
|
|
11
14
|
}
|
|
12
15
|
getSourceLang() {
|
|
13
|
-
return '
|
|
16
|
+
return this.opt.defaultLang || 'en';
|
|
14
17
|
}
|
|
15
18
|
getDefaultLang() {
|
|
16
|
-
return '
|
|
19
|
+
return this.opt.defaultLang || 'en';
|
|
17
20
|
}
|
|
18
|
-
constructor(
|
|
19
|
-
this.
|
|
21
|
+
constructor(opt) {
|
|
22
|
+
this.opt = opt;
|
|
20
23
|
this.localCache = {};
|
|
21
24
|
this.surpressErrors = true;
|
|
22
25
|
this.codes = {
|
|
@@ -30,12 +33,10 @@ class ZTranslateService {
|
|
|
30
33
|
[`£`]: `£`,
|
|
31
34
|
[`™`]: `™`,
|
|
32
35
|
};
|
|
36
|
+
translate.key = opt.googleApiKey;
|
|
33
37
|
this.getLanguages().map((lang) => (this.localCache[lang] = {}));
|
|
34
38
|
setInterval(() => this.clearLocalCache(), 1000 * 60 * 60); // Every Hour
|
|
35
39
|
}
|
|
36
|
-
static init(opt) {
|
|
37
|
-
translate.key = opt.key;
|
|
38
|
-
}
|
|
39
40
|
getLang(cookies) {
|
|
40
41
|
const defaultLang = this.getDefaultLang();
|
|
41
42
|
const langKey = (cookies.lang || defaultLang).toLowerCase();
|
|
@@ -210,9 +211,9 @@ class ZTranslateService {
|
|
|
210
211
|
fetchAll() {
|
|
211
212
|
return this.sql.query(`SELECT \`key\`, \`lang\`, \`value\`, \`verified\`, \`created_at\` FROM translations`);
|
|
212
213
|
}
|
|
213
|
-
static get(
|
|
214
|
+
static get(opt) {
|
|
214
215
|
if (instance == null) {
|
|
215
|
-
instance = new ZTranslateService(
|
|
216
|
+
instance = new ZTranslateService(opt);
|
|
216
217
|
}
|
|
217
218
|
return instance;
|
|
218
219
|
}
|