@freelog/tools-lib 0.1.140 → 0.1.141

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.140",
3
+ "version": "0.1.141",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -1,154 +1,155 @@
1
- import i18next, {Resource} from 'i18next';
2
- import axios from "axios";
3
- import Cookies from 'js-cookie';
4
- import FUtil from '../utils';
5
-
6
- type LanguageKeyType = 'zh_CN' | 'en_US';
7
-
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';
10
- const localStorage_i18nextLng_key: string = 'locale';
11
- const localStorage_i18nextResources_key: string = 'i18nextResources';
12
-
13
- const allLanguage = [
14
- {value: 'en_US', label: 'English'},
15
- {value: 'zh_CN', label: '简体中文'},
16
- ];
17
-
18
- class I18nNext {
19
-
20
- private _loadingData: 'NotStart' | 'Start' | 'End' = 'NotStart';
21
- private _taskQueue: Function[] = [];
22
- // private _currentLanguage: LanguageKeyType = window.localStorage.getItem(localStorage_i18nextLng_key) as null || 'zh_CN';
23
- private _currentLanguage: LanguageKeyType = Cookies.get(localStorage_i18nextLng_key) as undefined || 'zh_CN';
24
-
25
- constructor() {
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);
33
- }
34
-
35
- async ready(this: I18nNext) {
36
- const exc = () => {
37
- while (this._taskQueue.length > 0) {
38
- const task = this._taskQueue.shift();
39
- task && task();
40
- }
41
- };
42
- const handleTasks = async () => {
43
- // console.log(this._loadingData, 'this._loadingData90iowejflksdfjlsdk');
44
- if (this._loadingData === 'End') {
45
- exc();
46
- return;
47
- }
48
- if (this._loadingData === 'Start') {
49
- return;
50
- }
51
-
52
- // NO_START
53
- this._loadingData = 'Start';
54
-
55
- await this._handleData();
56
- // console.log('######');
57
- exc();
58
- };
59
- const promise = new Promise((resolve) => {
60
- this._taskQueue.push(resolve);
61
- });
62
- handleTasks();
63
- return promise;
64
- }
65
-
66
- t(this: I18nNext, key: string, options?: { [key: string]: any }) {
67
- return i18next.t(key, options);
68
- }
69
-
70
- changeLanguage(this: I18nNext, lng: LanguageKeyType) {
71
- // return i18next.changeLanguage(lng);
72
- // window.localStorage.setItem(localStorage_i18nextLng_key, lng)
73
- Cookies.set(localStorage_i18nextLng_key, lng, {
74
- expires: 36525,
75
- domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
76
- });
77
- }
78
-
79
- getAllLanguage(this: I18nNext): typeof allLanguage {
80
- return allLanguage;
81
- }
82
-
83
- getCurrentLanguage(this: I18nNext): LanguageKeyType {
84
- return this._currentLanguage;
85
- }
86
-
87
- private async _handleData(this: I18nNext) {
88
-
89
- const lng: string = this._currentLanguage;
90
- const resource: string | null = window.localStorage.getItem(localStorage_i18nextResources_key);
91
- // const resource: string | undefined = Cookies.get(decodeURIComponent(localStorage_i18nextResources_key));
92
- let i18nextResources: Resource | null = resource ? JSON.parse(resource) : null;
93
-
94
- if (!i18nextResources) {
95
- // console.log('######892io3jlkl')
96
- i18nextResources = await this._fetchData();
97
- } else {
98
- this._fetchData();
99
- }
100
-
101
- await i18next
102
- .init({
103
- // the translations
104
- // (tip move them in a JSON file and import them,
105
- // or even better, manage them via a UI: https://react.i18next.com/guides/multiple-translation-files#manage-your-translations-with-a-management-gui)
106
- resources: i18nextResources,
107
- lng: lng, // if you're using a language detector, do not define the lng option
108
- fallbackLng: 'zh_CN',
109
-
110
- interpolation: {
111
- escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
112
- prefix: '{',
113
- suffix: '}',
114
- },
115
- });
116
- this._loadingData = 'End';
117
- }
118
-
119
- private async _fetchData(this: I18nNext): Promise<Resource> {
120
- const res: any = await axios.get(window.location.origin.includes('.freelog.com') ? ossJsonUrl : ossJsonUrl_Test, {
121
- withCredentials: false,
122
- });
123
- // console.log(res, 'data09oiw3qjelsfkdfjlsdkfjl');
124
-
125
- const en_US: { [key: string]: string } = {};
126
- const zh_CN: { [key: string]: string } = {};
127
-
128
- for (const [key, value] of Object.entries(res)) {
129
- // console.log(key, value, 'key, value90iowsldfjlsdkj');
130
- en_US[key] = (value as any)['en_US'];
131
- zh_CN[key] = (value as any)['zh_CN'];
132
- }
133
-
134
- const result: Resource = {
135
- en_US: {
136
- translation: en_US,
137
- },
138
- zh_CN: {
139
- translation: zh_CN,
140
- },
141
- };
142
-
143
- // console.log(result, 'result093sdolkfjlsdkjl');
144
- window.localStorage.setItem(localStorage_i18nextResources_key, JSON.stringify(result));
145
- // Cookies.set(localStorage_i18nextResources_key, encodeURIComponent(JSON.stringify(result)), {
146
- // expires: 36525,
147
- // domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
148
- // });
149
-
150
- return result;
151
- }
152
- }
153
-
154
- export default I18nNext;
1
+ import i18next, {Resource} from 'i18next';
2
+ import axios from "axios";
3
+ import Cookies from 'js-cookie';
4
+ import FUtil from '../utils';
5
+
6
+ type LanguageKeyType = 'zh_CN' | 'en_US';
7
+
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';
10
+ const localStorage_i18nextLng_key: string = 'locale';
11
+ const localStorage_i18nextResources_key: string = 'i18nextResources';
12
+
13
+ const allLanguage = [
14
+ {value: 'en_US', label: 'English'},
15
+ {value: 'zh_CN', label: '简体中文'},
16
+ ];
17
+
18
+ class I18nNext {
19
+
20
+ private _loadingData: 'NotStart' | 'Start' | 'End' = 'NotStart';
21
+ private _taskQueue: Function[] = [];
22
+ // private _currentLanguage: LanguageKeyType = window.localStorage.getItem(localStorage_i18nextLng_key) as null || 'zh_CN';
23
+ private _currentLanguage: LanguageKeyType = Cookies.get(localStorage_i18nextLng_key) as undefined || 'zh_CN';
24
+
25
+ constructor() {
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);
33
+ }
34
+
35
+ async ready(this: I18nNext) {
36
+ const exc = () => {
37
+ while (this._taskQueue.length > 0) {
38
+ const task = this._taskQueue.shift();
39
+ task && task();
40
+ }
41
+ };
42
+ const handleTasks = async () => {
43
+ // console.log(this._loadingData, 'this._loadingData90iowejflksdfjlsdk');
44
+ if (this._loadingData === 'End') {
45
+ exc();
46
+ return;
47
+ }
48
+ if (this._loadingData === 'Start') {
49
+ return;
50
+ }
51
+
52
+ // NO_START
53
+ this._loadingData = 'Start';
54
+
55
+ await this._handleData();
56
+ // console.log('######');
57
+ exc();
58
+ };
59
+ const promise = new Promise((resolve) => {
60
+ this._taskQueue.push(resolve);
61
+ });
62
+ handleTasks();
63
+ return promise;
64
+ }
65
+
66
+ t(this: I18nNext, key: string, options?: { [key: string]: any }) {
67
+ return i18next.t(key, options);
68
+ }
69
+
70
+ changeLanguage(this: I18nNext, lng: LanguageKeyType) {
71
+ // return i18next.changeLanguage(lng);
72
+ // window.localStorage.setItem(localStorage_i18nextLng_key, lng)
73
+ Cookies.set(localStorage_i18nextLng_key, lng, {
74
+ expires: 36525,
75
+ domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
76
+ });
77
+ }
78
+
79
+ getAllLanguage(this: I18nNext): typeof allLanguage {
80
+ return allLanguage;
81
+ }
82
+
83
+ getCurrentLanguage(this: I18nNext): LanguageKeyType {
84
+ return this._currentLanguage;
85
+ }
86
+
87
+ private async _handleData(this: I18nNext) {
88
+
89
+ const lng: string = this._currentLanguage;
90
+ const resource: string | null = window.localStorage.getItem(localStorage_i18nextResources_key);
91
+ // const resource: string | undefined = Cookies.get(decodeURIComponent(localStorage_i18nextResources_key));
92
+ let i18nextResources: Resource | null = resource ? JSON.parse(resource) : null;
93
+
94
+ if (!i18nextResources) {
95
+ // console.log('######892io3jlkl')
96
+ i18nextResources = await this._fetchData();
97
+ } else {
98
+ this._fetchData();
99
+ }
100
+
101
+ await i18next
102
+ .init({
103
+ // the translations
104
+ // (tip move them in a JSON file and import them,
105
+ // or even better, manage them via a UI: https://react.i18next.com/guides/multiple-translation-files#manage-your-translations-with-a-management-gui)
106
+ resources: i18nextResources,
107
+ lng: lng, // if you're using a language detector, do not define the lng option
108
+ fallbackLng: 'zh_CN',
109
+
110
+ interpolation: {
111
+ escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
112
+ prefix: '{',
113
+ suffix: '}',
114
+ },
115
+ });
116
+ this._loadingData = 'End';
117
+ }
118
+
119
+ private async _fetchData(this: I18nNext): Promise<Resource> {
120
+ const url: string = window.location.origin.includes('.freelog.com') ? ossJsonUrl : ossJsonUrl_Test;
121
+ const res: any = await axios.get(url + '?timestamp=' + Date.now(), {
122
+ withCredentials: false,
123
+ });
124
+ // console.log(res, 'data09oiw3qjelsfkdfjlsdkfjl');
125
+
126
+ const en_US: { [key: string]: string } = {};
127
+ const zh_CN: { [key: string]: string } = {};
128
+
129
+ for (const [key, value] of Object.entries(res)) {
130
+ // console.log(key, value, 'key, value90iowsldfjlsdkj');
131
+ en_US[key] = (value as any)['en_US'];
132
+ zh_CN[key] = (value as any)['zh_CN'];
133
+ }
134
+
135
+ const result: Resource = {
136
+ en_US: {
137
+ translation: en_US,
138
+ },
139
+ zh_CN: {
140
+ translation: zh_CN,
141
+ },
142
+ };
143
+
144
+ // console.log(result, 'result093sdolkfjlsdkjl');
145
+ window.localStorage.setItem(localStorage_i18nextResources_key, JSON.stringify(result));
146
+ // Cookies.set(localStorage_i18nextResources_key, encodeURIComponent(JSON.stringify(result)), {
147
+ // expires: 36525,
148
+ // domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
149
+ // });
150
+
151
+ return result;
152
+ }
153
+ }
154
+
155
+ export default I18nNext;