@freelog/tools-lib 0.1.124 → 0.1.127

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freelog/tools-lib",
3
- "version": "0.1.124",
3
+ "version": "0.1.127",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -52,7 +52,7 @@
52
52
  "size-limit": "^4.11.0",
53
53
  "tsdx": "^0.14.1",
54
54
  "tslib": "^2.2.0",
55
- "typescript": "^4.3.2"
55
+ "typescript": "^4.7.4"
56
56
  },
57
57
  "dependencies": {
58
58
  "@freelog/resource-policy-lang": "1.1.26",
@@ -6,6 +6,7 @@ import FUtil from '../utils';
6
6
  type LanguageKeyType = 'zh_CN' | 'en_US';
7
7
 
8
8
  const ossJsonUrl: string = 'https://freelog-i18n.oss-cn-shenzhen.aliyuncs.com/configs/i18n.json';
9
+ const ossJsonUrl_Test: string = 'https://freelog-i18n.oss-cn-shenzhen.aliyuncs.com/configs-test/i18n.json';
9
10
  const localStorage_i18nextLng_key: string = 'locale';
10
11
  const localStorage_i18nextResources_key: string = 'i18nextResources';
11
12
 
@@ -14,8 +15,6 @@ const allLanguage = [
14
15
  {value: 'zh_CN', label: '简体中文'},
15
16
  ];
16
17
 
17
- let self: I18nNext;
18
-
19
18
  class I18nNext {
20
19
 
21
20
  private _loadingData: 'NotStart' | 'Start' | 'End' = 'NotStart';
@@ -24,45 +23,50 @@ class I18nNext {
24
23
  private _currentLanguage: LanguageKeyType = Cookies.get(localStorage_i18nextLng_key) as undefined || 'zh_CN';
25
24
 
26
25
  constructor() {
27
- self = this;
28
- self.ready();
26
+ this.ready();
27
+
28
+ this.ready = this.ready.bind(this);
29
+ this.t = this.t.bind(this);
30
+ this.changeLanguage = this.changeLanguage.bind(this);
31
+ this.getAllLanguage = this.getAllLanguage.bind(this);
32
+ this.getCurrentLanguage = this.getCurrentLanguage.bind(this);
29
33
  }
30
34
 
31
- async ready() {
35
+ async ready(this: I18nNext) {
32
36
  const exc = () => {
33
- while (self._taskQueue.length > 0) {
34
- const task = self._taskQueue.shift();
37
+ while (this._taskQueue.length > 0) {
38
+ const task = this._taskQueue.shift();
35
39
  task && task();
36
40
  }
37
41
  };
38
42
  const handleTasks = async () => {
39
- if (self._loadingData === 'End') {
43
+ if (this._loadingData === 'End') {
40
44
  exc();
41
45
  return;
42
46
  }
43
- if (self._loadingData === 'Start') {
47
+ if (this._loadingData === 'Start') {
44
48
  return;
45
49
  }
46
50
 
47
51
  // NO_START
48
- self._loadingData = 'Start';
52
+ this._loadingData = 'Start';
49
53
 
50
- await self._handleData();
54
+ await this._handleData();
51
55
  // console.log('######');
52
56
  exc();
53
57
  };
54
58
  const promise = new Promise((resolve) => {
55
- self._taskQueue.push(resolve);
59
+ this._taskQueue.push(resolve);
56
60
  });
57
61
  handleTasks();
58
62
  return promise;
59
63
  }
60
64
 
61
- t(key: string, options?: { [key: string]: any }) {
65
+ t(this: I18nNext, key: string, options?: { [key: string]: any }) {
62
66
  return i18next.t(key, options);
63
67
  }
64
68
 
65
- changeLanguage(lng: LanguageKeyType) {
69
+ changeLanguage(this: I18nNext, lng: LanguageKeyType) {
66
70
  // return i18next.changeLanguage(lng);
67
71
  // window.localStorage.setItem(localStorage_i18nextLng_key, lng)
68
72
  Cookies.set(localStorage_i18nextLng_key, lng, {
@@ -71,26 +75,26 @@ class I18nNext {
71
75
  });
72
76
  }
73
77
 
74
- getAllLanguage(): typeof allLanguage {
78
+ getAllLanguage(this: I18nNext): typeof allLanguage {
75
79
  return allLanguage;
76
80
  }
77
81
 
78
- getCurrentLanguage(): LanguageKeyType {
79
- return self._currentLanguage;
82
+ getCurrentLanguage(this: I18nNext): LanguageKeyType {
83
+ return this._currentLanguage;
80
84
  }
81
85
 
82
- private async _handleData() {
86
+ private async _handleData(this: I18nNext) {
83
87
 
84
- const lng: string = self._currentLanguage;
88
+ const lng: string = this._currentLanguage;
85
89
  const resource: string | null = window.localStorage.getItem(localStorage_i18nextResources_key);
86
90
  // const resource: string | undefined = Cookies.get(decodeURIComponent(localStorage_i18nextResources_key));
87
91
  let i18nextResources: Resource | null = resource ? JSON.parse(resource) : null;
88
92
 
89
93
  if (!i18nextResources) {
90
94
  // console.log('######892io3jlkl')
91
- i18nextResources = await self._fetchData();
95
+ i18nextResources = await this._fetchData();
92
96
  } else {
93
- self._fetchData();
97
+ this._fetchData();
94
98
  }
95
99
 
96
100
  await i18next
@@ -110,8 +114,8 @@ class I18nNext {
110
114
  });
111
115
  }
112
116
 
113
- private async _fetchData(): Promise<Resource> {
114
- const res: any = await axios.get(ossJsonUrl, {
117
+ private async _fetchData(this: I18nNext): Promise<Resource> {
118
+ const res: any = await axios.get(window.location.origin.includes('.freelog.com') ? ossJsonUrl : ossJsonUrl_Test, {
115
119
  withCredentials: false,
116
120
  });
117
121
  // console.log(res, 'data09oiw3qjelsfkdfjlsdkfjl');