@freelog/tools-lib 0.1.165 → 0.1.166

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,67 +1,68 @@
1
- {
2
- "name": "@freelog/tools-lib",
3
- "version": "0.1.165",
4
- "license": "MIT",
5
- "main": "dist/index.js",
6
- "typings": "dist/index.d.ts",
7
- "files": [
8
- "dist",
9
- "src"
10
- ],
11
- "engines": {
12
- "node": ">=10"
13
- },
14
- "scripts": {
15
- "start": "tsdx watch",
16
- "build": "tsdx build",
17
- "test": "tsdx test",
18
- "lint": "tsdx lint",
19
- "prepare": "tsdx build",
20
- "size": "size-limit",
21
- "analyze": "size-limit --why",
22
- "publish": "npm publish --access=public"
23
- },
24
- "husky": {
25
- "hooks": {}
26
- },
27
- "prettier": {
28
- "printWidth": 80,
29
- "semi": true,
30
- "singleQuote": true,
31
- "trailingComma": "es5"
32
- },
33
- "author": "liu-kai-github",
34
- "module": "dist/tools-lib.esm.js",
35
- "size-limit": [
36
- {
37
- "path": "dist/tools-lib.cjs.production.min.js",
38
- "limit": "10 KB"
39
- },
40
- {
41
- "path": "dist/tools-lib.esm.js",
42
- "limit": "10 KB"
43
- }
44
- ],
45
- "devDependencies": {
46
- "@size-limit/preset-small-lib": "^4.11.0",
47
- "@types/js-cookie": "^3.0.2",
48
- "@types/node": "^18.7.16",
49
- "@types/nprogress": "^0.2.0",
50
- "@types/react": "^18.2.17",
51
- "husky": "^6.0.0",
52
- "size-limit": "^4.11.0",
53
- "tsdx": "^0.14.1",
54
- "tslib": "^2.2.0",
55
- "typescript": "^4.7.4"
56
- },
57
- "dependencies": {
58
- "@freelog/resource-policy-lang": "1.1.26",
59
- "axios": "^0.21.1",
60
- "html-react-parser": "^4.2.0",
61
- "i18next": "^21.8.10",
62
- "js-cookie": "^3.0.1",
63
- "moment": "^2.29.1",
64
- "nprogress": "^0.2.0",
65
- "react": "^18.2.0"
66
- }
67
- }
1
+ {
2
+ "name": "@freelog/tools-lib",
3
+ "version": "0.1.166",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "typings": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "src"
10
+ ],
11
+ "engines": {
12
+ "node": ">=10"
13
+ },
14
+ "scripts": {
15
+ "start": "tsdx watch",
16
+ "build": "tsdx build",
17
+ "test": "tsdx test",
18
+ "lint": "tsdx lint",
19
+ "prepare": "tsdx build",
20
+ "size": "size-limit",
21
+ "analyze": "size-limit --why",
22
+ "publish": "npm publish --access=public",
23
+ "resetRegistry": "npm config set registry https://registry.npmjs.org/"
24
+ },
25
+ "husky": {
26
+ "hooks": {}
27
+ },
28
+ "prettier": {
29
+ "printWidth": 80,
30
+ "semi": true,
31
+ "singleQuote": true,
32
+ "trailingComma": "es5"
33
+ },
34
+ "author": "liu-kai-github",
35
+ "module": "dist/tools-lib.esm.js",
36
+ "size-limit": [
37
+ {
38
+ "path": "dist/tools-lib.cjs.production.min.js",
39
+ "limit": "10 KB"
40
+ },
41
+ {
42
+ "path": "dist/tools-lib.esm.js",
43
+ "limit": "10 KB"
44
+ }
45
+ ],
46
+ "devDependencies": {
47
+ "@size-limit/preset-small-lib": "^4.11.0",
48
+ "@types/js-cookie": "^3.0.2",
49
+ "@types/node": "^18.7.16",
50
+ "@types/nprogress": "^0.2.0",
51
+ "@types/react": "^18.2.17",
52
+ "husky": "^6.0.0",
53
+ "size-limit": "^4.11.0",
54
+ "tsdx": "^0.14.1",
55
+ "tslib": "^2.2.0",
56
+ "typescript": "^4.7.4"
57
+ },
58
+ "dependencies": {
59
+ "@freelog/resource-policy-lang": "1.1.26",
60
+ "axios": "^0.21.1",
61
+ "html-react-parser": "^4.2.0",
62
+ "i18next": "^21.8.10",
63
+ "js-cookie": "^3.0.1",
64
+ "moment": "^2.29.1",
65
+ "nprogress": "^0.2.0",
66
+ "react": "^18.2.0"
67
+ }
68
+ }
@@ -1,162 +1,156 @@
1
- import i18next, {Resource} from 'i18next';
2
- import axios from "axios";
3
- import Cookies from 'js-cookie';
4
- import FUtil from '../utils';
5
- // import * as React from 'react';
6
- import htmlReactParser from 'html-react-parser';
7
-
8
- type LanguageKeyType = 'zh_CN' | 'en_US';
9
-
10
- const ossJsonUrl: string = 'https://freelog-i18n.oss-cn-shenzhen.aliyuncs.com/configs/i18n.json';
11
- const ossJsonUrl_Test: string = 'https://freelog-i18n.oss-cn-shenzhen.aliyuncs.com/configs-test/i18n.json';
12
- const localStorage_i18nextLng_key: string = 'locale';
13
- const localStorage_i18nextResources_key: string = 'i18nextResources';
14
-
15
- const allLanguage = [
16
- {value: 'en_US', label: 'English'},
17
- {value: 'zh_CN', label: '简体中文'},
18
- ];
19
-
20
- class I18nNext {
21
-
22
- private _loadingData: 'NotStart' | 'Start' | 'End' = 'NotStart';
23
- private _taskQueue: Function[] = [];
24
- // private _currentLanguage: LanguageKeyType = window.localStorage.getItem(localStorage_i18nextLng_key) as null || 'zh_CN';
25
- private _currentLanguage: LanguageKeyType = Cookies.get(localStorage_i18nextLng_key) as undefined || 'zh_CN';
26
-
27
- constructor() {
28
- this.ready();
29
-
30
- this.ready = this.ready.bind(this);
31
- this.t = this.t.bind(this);
32
- this.changeLanguage = this.changeLanguage.bind(this);
33
- this.getAllLanguage = this.getAllLanguage.bind(this);
34
- this.getCurrentLanguage = this.getCurrentLanguage.bind(this);
35
- }
36
-
37
- async ready(this: I18nNext): Promise<void> {
38
- const exc = () => {
39
- while (this._taskQueue.length > 0) {
40
- const task = this._taskQueue.shift();
41
- task && task();
42
- }
43
- };
44
- const handleTasks = async () => {
45
- // console.log(this._loadingData, 'this._loadingData90iowejflksdfjlsdk');
46
- if (this._loadingData === 'End') {
47
- exc();
48
- return;
49
- }
50
- if (this._loadingData === 'Start') {
51
- return;
52
- }
53
-
54
- // NO_START
55
- this._loadingData = 'Start';
56
-
57
- await this._handleData();
58
- // console.log('######');
59
- exc();
60
- };
61
- const promise = new Promise<void>((resolve) => {
62
- this._taskQueue.push(resolve);
63
- });
64
- handleTasks();
65
- return promise;
66
- }
67
-
68
- t(this: I18nNext, key: string, options?: { [key: string]: any }): string {
69
- return i18next.t(key.trim(), options);
70
- }
71
-
72
- tJSXElement(this: I18nNext, key: string, options?: { [key: string]: any }): string | JSX.Element | JSX.Element[] {
73
- return htmlReactParser(i18next.t(key.trim(), options));
74
- }
75
-
76
-
77
- changeLanguage(this: I18nNext, lng: LanguageKeyType): void {
78
- // return i18next.changeLanguage(lng);
79
- // window.localStorage.setItem(localStorage_i18nextLng_key, lng)
80
- Cookies.set(localStorage_i18nextLng_key, lng, {
81
- expires: 36525,
82
- domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
83
- });
84
- }
85
-
86
- getAllLanguage(this: I18nNext): typeof allLanguage {
87
- return allLanguage;
88
- }
89
-
90
- getCurrentLanguage(this: I18nNext): LanguageKeyType {
91
- return this._currentLanguage;
92
- }
93
-
94
- private async _handleData(this: I18nNext): Promise<void> {
95
-
96
- const lng: string = this._currentLanguage;
97
- const resource: string | null = window.localStorage.getItem(localStorage_i18nextResources_key);
98
- // const resource: string | undefined = Cookies.get(decodeURIComponent(localStorage_i18nextResources_key));
99
- let i18nextResources: Resource | null = resource ? JSON.parse(resource) : null;
100
-
101
- if (!i18nextResources) {
102
- // console.log('######892io3jlkl')
103
- i18nextResources = await this._fetchData();
104
- } else {
105
- this._fetchData();
106
- }
107
-
108
- await i18next
109
- .init({
110
- // the translations
111
- // (tip move them in a JSON file and import them,
112
- // or even better, manage them via a UI: https://react.i18next.com/guides/multiple-translation-files#manage-your-translations-with-a-management-gui)
113
- resources: i18nextResources,
114
- lng: lng, // if you're using a language detector, do not define the lng option
115
- fallbackLng: 'zh_CN',
116
-
117
- interpolation: {
118
- escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
119
- prefix: '{',
120
- suffix: '}',
121
- },
122
- });
123
- this._loadingData = 'End';
124
- }
125
-
126
- private async _fetchData(this: I18nNext): Promise<Resource> {
127
- const url: string = window.location.origin.includes('.freelog.com') ? ossJsonUrl : ossJsonUrl_Test;
128
- const res: any = await axios.get(url + '?timestamp=' + Date.now(), {
129
- withCredentials: false,
130
- });
131
- // console.log(res, 'data09oiw3qjelsfkdfjlsdkfjl');
132
-
133
- const en_US: { [key: string]: string } = {};
134
- const zh_CN: { [key: string]: string } = {};
135
-
136
- for (const [key, value] of Object.entries(res)) {
137
- // console.log(key, value, 'key, value90iowsldfjlsdkj');
138
- en_US[key] = (value as any)['en_US'];
139
- zh_CN[key] = (value as any)['zh_CN'];
140
- }
141
-
142
- const result: Resource = {
143
- en_US: {
144
- translation: en_US,
145
- },
146
- zh_CN: {
147
- translation: zh_CN,
148
- },
149
- };
150
-
151
- // console.log(result, 'result093sdolkfjlsdkjl');
152
- window.localStorage.setItem(localStorage_i18nextResources_key, JSON.stringify(result));
153
- // Cookies.set(localStorage_i18nextResources_key, encodeURIComponent(JSON.stringify(result)), {
154
- // expires: 36525,
155
- // domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
156
- // });
157
-
158
- return result;
159
- }
160
- }
161
-
162
- 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
+ // import * as React from 'react';
6
+ import htmlReactParser from 'html-react-parser';
7
+
8
+ type LanguageKeyType = 'zh_CN' | 'en_US';
9
+
10
+ const ossJsonUrl: string = 'https://freelog-i18n.oss-cn-shenzhen.aliyuncs.com/configs/i18n.json';
11
+ const ossJsonUrl_Test: string = 'https://freelog-i18n.oss-cn-shenzhen.aliyuncs.com/configs-test/i18n.json';
12
+ const localStorage_i18nextLng_key: string = 'locale';
13
+ const localStorage_i18nextResources_key: string = 'i18nextResources';
14
+
15
+ const allLanguage = [
16
+ {value: 'en_US', label: 'English'},
17
+ {value: 'zh_CN', label: '简体中文'},
18
+ ];
19
+
20
+ class I18nNext {
21
+
22
+ private _loadingData: 'NotStart' | 'Start' | 'End' = 'NotStart';
23
+ private _taskQueue: Function[] = [];
24
+ // private _currentLanguage: LanguageKeyType = window.localStorage.getItem(localStorage_i18nextLng_key) as null || 'zh_CN';
25
+ private _currentLanguage: LanguageKeyType = Cookies.get(localStorage_i18nextLng_key) as undefined || 'zh_CN';
26
+
27
+ constructor() {
28
+ this.ready();
29
+
30
+ this.ready = this.ready.bind(this);
31
+ this.t = this.t.bind(this);
32
+ this.changeLanguage = this.changeLanguage.bind(this);
33
+ this.getAllLanguage = this.getAllLanguage.bind(this);
34
+ this.getCurrentLanguage = this.getCurrentLanguage.bind(this);
35
+ }
36
+
37
+ async ready(this: I18nNext): Promise<void> {
38
+ const exc = () => {
39
+ while (this._taskQueue.length > 0) {
40
+ const task = this._taskQueue.shift();
41
+ task && task();
42
+ }
43
+ };
44
+ const handleTasks = async () => {
45
+ // console.log(this._loadingData, 'this._loadingData90iowejflksdfjlsdk');
46
+ if (this._loadingData === 'End') {
47
+ exc();
48
+ return;
49
+ }
50
+ if (this._loadingData === 'Start') {
51
+ return;
52
+ }
53
+
54
+ // NO_START
55
+ this._loadingData = 'Start';
56
+
57
+ await this._handleData();
58
+ // console.log('######');
59
+ exc();
60
+ };
61
+ const promise = new Promise<void>((resolve) => {
62
+ this._taskQueue.push(resolve);
63
+ });
64
+ handleTasks();
65
+ return promise;
66
+ }
67
+
68
+ t(this: I18nNext, key: string, options?: { [key: string]: any }): string {
69
+ return i18next.t(key.trim(), options);
70
+ }
71
+
72
+ tJSXElement(this: I18nNext, key: string, options?: { [key: string]: any }): string | JSX.Element | JSX.Element[] {
73
+ return htmlReactParser(i18next.t(key.trim(), options));
74
+ }
75
+
76
+
77
+ changeLanguage(this: I18nNext, lng: LanguageKeyType): void {
78
+ // return i18next.changeLanguage(lng);
79
+ // window.localStorage.setItem(localStorage_i18nextLng_key, lng)
80
+ Cookies.set(localStorage_i18nextLng_key, lng, {
81
+ expires: 36525,
82
+ domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
83
+ });
84
+ }
85
+
86
+ getAllLanguage(this: I18nNext): typeof allLanguage {
87
+ return allLanguage;
88
+ }
89
+
90
+ getCurrentLanguage(this: I18nNext): LanguageKeyType {
91
+ return this._currentLanguage;
92
+ }
93
+
94
+ private async _handleData(this: I18nNext): Promise<void> {
95
+
96
+ const lng: string = this._currentLanguage;
97
+ const resource: string | null = window.localStorage.getItem(localStorage_i18nextResources_key);
98
+ // const resource: string | undefined = Cookies.get(decodeURIComponent(localStorage_i18nextResources_key));
99
+ let i18nextResources: Resource | null = resource ? JSON.parse(resource) : null;
100
+
101
+ if (!i18nextResources) {
102
+ // console.log('######892io3jlkl')
103
+ i18nextResources = await this._fetchData();
104
+ } else {
105
+ this._fetchData();
106
+ }
107
+
108
+ await i18next
109
+ .init({
110
+ // the translations
111
+ // (tip move them in a JSON file and import them,
112
+ // or even better, manage them via a UI: https://react.i18next.com/guides/multiple-translation-files#manage-your-translations-with-a-management-gui)
113
+ resources: i18nextResources,
114
+ lng: lng, // if you're using a language detector, do not define the lng option
115
+ fallbackLng: 'zh_CN',
116
+
117
+ interpolation: {
118
+ escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
119
+ prefix: '{',
120
+ suffix: '}',
121
+ },
122
+ });
123
+ this._loadingData = 'End';
124
+ }
125
+
126
+ private async _fetchData(this: I18nNext): Promise<Resource> {
127
+ const url: string = window.location.origin.includes('.freelog.com') ? ossJsonUrl : ossJsonUrl_Test;
128
+ const res: any = await axios.get(url + '?timestamp=' + Date.now(), {
129
+ withCredentials: false,
130
+ });
131
+ // console.log(res, 'data09oiw3qjelsfkdfjlsdkfjl');
132
+
133
+ const en_US: { [key: string]: string } = {};
134
+ const zh_CN: { [key: string]: string } = {};
135
+
136
+ for (const [key, value] of Object.entries(res)) {
137
+ // console.log(key, value, 'key, value90iowsldfjlsdkj');
138
+ en_US[key] = (value as any)['en_US'];
139
+ zh_CN[key] = (value as any)['zh_CN'];
140
+ }
141
+
142
+ const result: Resource = {
143
+ en_US: {
144
+ translation: en_US,
145
+ },
146
+ zh_CN: {
147
+ translation: zh_CN,
148
+ },
149
+ };
150
+
151
+ window.localStorage.setItem(localStorage_i18nextResources_key, JSON.stringify(result));
152
+ return result;
153
+ }
154
+ }
155
+
156
+ export default I18nNext;