ztechno_core 0.0.9 → 0.0.11

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.
@@ -12,14 +12,20 @@ type dbTranslationRow = {
12
12
  value: string;
13
13
  };
14
14
  export declare class ZTranslateService {
15
- private sql;
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(sql: ZSqlService);
22
- static init(opt: { key: string }): void;
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(sqlService: ZSqlService): ZTranslateService;
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 {};
@@ -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 'nl';
16
+ return this.opt.defaultLang || 'en';
14
17
  }
15
18
  getDefaultLang() {
16
- return 'nl';
19
+ return this.opt.defaultLang || 'en';
17
20
  }
18
- constructor(sql) {
19
- this.sql = sql;
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
  [`&#163`]: `£`,
31
34
  [`&#8482`]: `™`,
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();
@@ -90,13 +91,13 @@ class ZTranslateService {
90
91
  }
91
92
  async translateHtml(html, cookies) {
92
93
  const lang = this.getLang(cookies);
93
- const defaultLang = this.getDefaultLang();
94
+ const srcLang = this.getSourceLang();
94
95
  const dom = htmlParser.parseFromString(html);
95
96
  const htmlNodes = dom.getElementsByTagName('html');
96
97
  const mainNodes = dom.getElementsByTagName('main');
97
98
  const isView = htmlNodes.length === 0;
98
99
  const domNode = isView ? mainNodes[0] : htmlNodes[0];
99
- if (lang !== defaultLang) {
100
+ if (lang !== srcLang) {
100
101
  const node = isView ? domNode : domNode.getElementsByTagName('body')[0];
101
102
  const promises = [];
102
103
  this.translateHtmlRec(lang, node, promises);
@@ -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(sqlService) {
214
+ static get(opt) {
214
215
  if (instance == null) {
215
- instance = new ZTranslateService(sqlService);
216
+ instance = new ZTranslateService(opt);
216
217
  }
217
218
  return instance;
218
219
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztechno_core",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "description": "Core files for ztechno framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",