@freelog/tools-lib 0.1.104 → 0.1.105

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,64 +1,65 @@
1
- {
2
- "name": "@freelog/tools-lib",
3
- "version": "0.1.104",
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
- "peerDependencies": {},
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/crypto-js": "^4.0.1",
49
- "@types/nprogress": "^0.2.0",
50
- "husky": "^6.0.0",
51
- "size-limit": "^4.11.0",
52
- "tsdx": "^0.14.1",
53
- "tslib": "^2.2.0",
54
- "typescript": "^4.3.2"
55
- },
56
- "dependencies": {
57
- "@freelog/resource-policy-lang": "1.1.26",
58
- "@freelog/tools-lib": "^0.1.43",
59
- "axios": "^0.21.1",
60
- "crypto-js": "^4.0.0",
61
- "moment": "^2.29.1",
62
- "nprogress": "^0.2.0"
63
- }
64
- }
1
+ {
2
+ "name": "@freelog/tools-lib",
3
+ "version": "0.1.105",
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
+ "peerDependencies": {},
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/crypto-js": "^4.0.1",
49
+ "@types/nprogress": "^0.2.0",
50
+ "husky": "^6.0.0",
51
+ "size-limit": "^4.11.0",
52
+ "tsdx": "^0.14.1",
53
+ "tslib": "^2.2.0",
54
+ "typescript": "^4.3.2"
55
+ },
56
+ "dependencies": {
57
+ "@freelog/resource-policy-lang": "1.1.26",
58
+ "@freelog/tools-lib": "^0.1.43",
59
+ "axios": "^0.21.1",
60
+ "crypto-js": "^4.0.0",
61
+ "i18next": "^21.8.10",
62
+ "moment": "^2.29.1",
63
+ "nprogress": "^0.2.0"
64
+ }
65
+ }
@@ -0,0 +1,134 @@
1
+ import i18next, {Resource} from 'i18next';
2
+ import axios from "axios";
3
+
4
+ type LanguageKeyType = 'zh_CN' | 'en_US';
5
+
6
+ const ossJsonUrl: string = 'https://freelog-i18n.oss-cn-shenzhen.aliyuncs.com/configs/i18n.json';
7
+ const localStorage_i18nextLng_key: string = 'i18nextLng';
8
+ const localStorage_i18nextResources_key: string = 'i18nextResources';
9
+
10
+ const allLanguage = [
11
+ {value: 'en_US', label: 'English'},
12
+ {value: 'zh_CN', label: '简体中文'},
13
+ ];
14
+
15
+ let self: I18nNext;
16
+
17
+ class I18nNext {
18
+
19
+ private _loadingData: 'NotStart' | 'Start' | 'End' = 'NotStart';
20
+ private _taskQueue: Function[] = [];
21
+ private _currentLanguage: LanguageKeyType = window.localStorage.getItem(localStorage_i18nextLng_key) as null || 'zh_CN';
22
+
23
+ constructor() {
24
+ self = this;
25
+ self.ready();
26
+ }
27
+
28
+ async ready() {
29
+ const exc = () => {
30
+ while (self._taskQueue.length > 0) {
31
+ const task = self._taskQueue.shift();
32
+ task && task();
33
+ }
34
+ };
35
+ const handleTasks = async () => {
36
+ if (self._loadingData === 'End') {
37
+ exc();
38
+ return;
39
+ }
40
+ if (self._loadingData === 'Start') {
41
+ return;
42
+ }
43
+
44
+ // NO_START
45
+ self._loadingData = 'Start';
46
+
47
+ await self._handleData();
48
+ // console.log('######');
49
+ exc();
50
+ };
51
+ const promise = new Promise((resolve) => {
52
+ self._taskQueue.push(resolve);
53
+ });
54
+ handleTasks();
55
+ return promise;
56
+ }
57
+
58
+ t(key: string, options?: { [key: string]: any }) {
59
+ return i18next.t(key, options);
60
+ }
61
+
62
+ changeLanguage(lng: LanguageKeyType) {
63
+ // return i18next.changeLanguage(lng);
64
+ window.localStorage.setItem(localStorage_i18nextLng_key, lng)
65
+ }
66
+
67
+ getAllLanguage(): typeof allLanguage {
68
+ return allLanguage;
69
+ }
70
+
71
+ getCurrentLanguage(): LanguageKeyType {
72
+ return self._currentLanguage;
73
+ }
74
+
75
+ private async _handleData() {
76
+
77
+ const lng: string = self._currentLanguage;
78
+ const resource: string | null = window.localStorage.getItem(localStorage_i18nextResources_key);
79
+ let i18nextResources: Resource | null = resource ? JSON.parse(resource) : null;
80
+
81
+ if (!i18nextResources) {
82
+ // console.log('######892io3jlkl')
83
+ i18nextResources = await self._fetchData();
84
+ } else {
85
+ self._fetchData();
86
+ }
87
+
88
+ await i18next
89
+ .init({
90
+ // the translations
91
+ // (tip move them in a JSON file and import them,
92
+ // or even better, manage them via a UI: https://react.i18next.com/guides/multiple-translation-files#manage-your-translations-with-a-management-gui)
93
+ resources: i18nextResources,
94
+ lng: lng, // if you're using a language detector, do not define the lng option
95
+ fallbackLng: 'zh_CN',
96
+
97
+ interpolation: {
98
+ escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
99
+ },
100
+ });
101
+ }
102
+
103
+ private async _fetchData(): Promise<Resource> {
104
+ const res: any = await axios.get(ossJsonUrl, {
105
+ withCredentials: false,
106
+ });
107
+ // console.log(res, 'data09oiw3qjelsfkdfjlsdkfjl');
108
+
109
+ const en_US: { [key: string]: string } = {};
110
+ const zh_CN: { [key: string]: string } = {};
111
+
112
+ for (const [key, value] of Object.entries(res)) {
113
+ // console.log(key, value, 'key, value90iowsldfjlsdkj');
114
+ en_US[key] = (value as any)['en_US'];
115
+ zh_CN[key] = (value as any)['zh_CN'];
116
+ }
117
+
118
+ const result: Resource = {
119
+ en_US: {
120
+ translation: en_US,
121
+ },
122
+ zh_CN: {
123
+ translation: zh_CN,
124
+ },
125
+ };
126
+
127
+ // console.log(result, 'result093sdolkfjlsdkjl');
128
+ window.localStorage.setItem(localStorage_i18nextResources_key, JSON.stringify(result));
129
+
130
+ return result;
131
+ }
132
+ }
133
+
134
+ export default I18nNext;
@@ -0,0 +1,7 @@
1
+ import I18nNext from './I18nNext';
2
+
3
+ const FI18n = {
4
+ i18nNext: new I18nNext(),
5
+ };
6
+
7
+ export default FI18n;
package/src/index.ts CHANGED
@@ -1,7 +1,9 @@
1
- import FUtil from './utils';
2
- import FServiceAPI from './service-API';
3
-
4
- export {
5
- FUtil,
6
- FServiceAPI,
7
- };
1
+ import FUtil from './utils';
2
+ import FServiceAPI from './service-API';
3
+ import FI18n from './i18n';
4
+
5
+ export {
6
+ FUtil,
7
+ FServiceAPI,
8
+ FI18n,
9
+ };
@@ -0,0 +1,35 @@
1
+ import FUtil from '../utils';
2
+
3
+ // 列出翻译
4
+ interface ConfigsListParamsType {
5
+ key: string;
6
+ content: string;
7
+ tagIds: string[];
8
+ status: 0 | 1 | 2 | 3; // 0:全部 1:待翻译 2:待发布 3:已发布
9
+ }
10
+
11
+ export function configsList(params: ConfigsListParamsType) {
12
+ // console.log('####@30984i2o3jdsjflfkjsdl')
13
+ return FUtil.Request({
14
+ method: 'POST',
15
+ url: `/v2/i18n/configs/list`,
16
+ data: params,
17
+ });
18
+ }
19
+
20
+ // oss保存的翻译
21
+ // interface ConfigsListParamsType {
22
+ // key: string;
23
+ // content: string;
24
+ // tagIds: string[];
25
+ // status: 0 | 1 | 2 | 3; // 0:全部 1:待翻译 2:待发布 3:已发布
26
+ // }
27
+ //
28
+ // export function configsList(params: ConfigsListParamsType) {
29
+ // // console.log('####@30984i2o3jdsjflfkjsdl')
30
+ // return FUtil.Request({
31
+ // method: 'POST',
32
+ // url: `/v2/i18n/configs/list`,
33
+ // data: params,
34
+ // });
35
+ // }
@@ -1,33 +1,35 @@
1
- import * as Node from './nodes';
2
- import * as Exhibit from './presentables';
3
- import * as Storage from './storages';
4
- import * as Collection from './collections';
5
- import * as Resource from './resources';
6
- import * as User from './user';
7
- import * as InformalNode from './informalNodes';
8
- import * as Contract from './contracts';
9
- import * as Transaction from './transactions';
10
- import * as Captcha from './captcha';
11
- import * as Event from './events';
12
- import * as Activity from './activities';
13
- import * as TestQualification from './testQualifications';
14
- import * as Statistic from './statistics';
15
-
16
- const FServiceAPI = {
17
- Node,
18
- Exhibit,
19
- Storage,
20
- Collection,
21
- Resource,
22
- User,
23
- InformalNode,
24
- Contract,
25
- Transaction,
26
- Captcha,
27
- Event,
28
- Activity,
29
- TestQualification,
30
- Statistic,
31
- };
32
-
33
- export default FServiceAPI;
1
+ import * as Node from './nodes';
2
+ import * as Exhibit from './presentables';
3
+ import * as Storage from './storages';
4
+ import * as Collection from './collections';
5
+ import * as Resource from './resources';
6
+ import * as User from './user';
7
+ import * as InformalNode from './informalNodes';
8
+ import * as Contract from './contracts';
9
+ import * as Transaction from './transactions';
10
+ import * as Captcha from './captcha';
11
+ import * as Event from './events';
12
+ import * as Activity from './activities';
13
+ import * as TestQualification from './testQualifications';
14
+ import * as Statistic from './statistics';
15
+ import * as I18n from './i18n';
16
+
17
+ const FServiceAPI = {
18
+ Node,
19
+ Exhibit,
20
+ Storage,
21
+ Collection,
22
+ Resource,
23
+ User,
24
+ InformalNode,
25
+ Contract,
26
+ Transaction,
27
+ Captcha,
28
+ Event,
29
+ Activity,
30
+ TestQualification,
31
+ Statistic,
32
+ I18n,
33
+ };
34
+
35
+ export default FServiceAPI;
@@ -1,18 +1,20 @@
1
- import * as Format from './format';
2
- import * as Regexp from './regexp';
3
- import * as LinkTo from './linkTo';
4
- import * as Predefined from './predefined';
5
- import Axios, {request} from './axios';
6
- import * as Tool from './tools';
7
-
8
- const FUtil = {
9
- Format,
10
- Regexp,
11
- LinkTo,
12
- Predefined,
13
- Axios,
14
- Request: request,
15
- Tool,
16
- };
17
-
18
- export default FUtil;
1
+ import * as Format from './format';
2
+ import * as Regexp from './regexp';
3
+ import * as LinkTo from './linkTo';
4
+ import * as Predefined from './predefined';
5
+ import Axios, {request} from './axios';
6
+ import * as Tool from './tools';
7
+ // import I18n from '../i18n';
8
+
9
+ const FUtil = {
10
+ Format,
11
+ Regexp,
12
+ LinkTo,
13
+ Predefined,
14
+ Axios,
15
+ Request: request,
16
+ Tool,
17
+ // i18n: new I18n(),
18
+ };
19
+
20
+ export default FUtil;