@freelog/tools-lib 0.1.104 → 0.1.107

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.
@@ -22,3 +22,8 @@ export declare function policyCodeTranslationToText(code: string, targetType: st
22
22
  error: string[] | null;
23
23
  text?: string;
24
24
  }>;
25
+ /**
26
+ * 将资源类型关键字数组,转换成标准展示文字
27
+ * @param arr 关键字数组
28
+ */
29
+ export declare function resourceTypeKeyArrToResourceType(arr: string[]): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freelog/tools-lib",
3
- "version": "0.1.104",
3
+ "version": "0.1.107",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -46,6 +46,7 @@
46
46
  "devDependencies": {
47
47
  "@size-limit/preset-small-lib": "^4.11.0",
48
48
  "@types/crypto-js": "^4.0.1",
49
+ "@types/js-cookie": "^3.0.2",
49
50
  "@types/nprogress": "^0.2.0",
50
51
  "husky": "^6.0.0",
51
52
  "size-limit": "^4.11.0",
@@ -58,6 +59,8 @@
58
59
  "@freelog/tools-lib": "^0.1.43",
59
60
  "axios": "^0.21.1",
60
61
  "crypto-js": "^4.0.0",
62
+ "i18next": "^21.8.10",
63
+ "js-cookie": "^3.0.1",
61
64
  "moment": "^2.29.1",
62
65
  "nprogress": "^0.2.0"
63
66
  }
@@ -0,0 +1,146 @@
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 localStorage_i18nextLng_key: string = 'locale';
10
+ const localStorage_i18nextResources_key: string = 'i18nextResources';
11
+
12
+ const allLanguage = [
13
+ {value: 'en_US', label: 'English'},
14
+ {value: 'zh_CN', label: '简体中文'},
15
+ ];
16
+
17
+ let self: I18nNext;
18
+
19
+ class I18nNext {
20
+
21
+ private _loadingData: 'NotStart' | 'Start' | 'End' = 'NotStart';
22
+ private _taskQueue: Function[] = [];
23
+ // private _currentLanguage: LanguageKeyType = window.localStorage.getItem(localStorage_i18nextLng_key) as null || 'zh_CN';
24
+ private _currentLanguage: LanguageKeyType = Cookies.get(localStorage_i18nextLng_key) as undefined || 'zh_CN';
25
+
26
+ constructor() {
27
+ self = this;
28
+ self.ready();
29
+ }
30
+
31
+ async ready() {
32
+ const exc = () => {
33
+ while (self._taskQueue.length > 0) {
34
+ const task = self._taskQueue.shift();
35
+ task && task();
36
+ }
37
+ };
38
+ const handleTasks = async () => {
39
+ if (self._loadingData === 'End') {
40
+ exc();
41
+ return;
42
+ }
43
+ if (self._loadingData === 'Start') {
44
+ return;
45
+ }
46
+
47
+ // NO_START
48
+ self._loadingData = 'Start';
49
+
50
+ await self._handleData();
51
+ // console.log('######');
52
+ exc();
53
+ };
54
+ const promise = new Promise((resolve) => {
55
+ self._taskQueue.push(resolve);
56
+ });
57
+ handleTasks();
58
+ return promise;
59
+ }
60
+
61
+ t(key: string, options?: { [key: string]: any }) {
62
+ return i18next.t(key, options);
63
+ }
64
+
65
+ changeLanguage(lng: LanguageKeyType) {
66
+ // return i18next.changeLanguage(lng);
67
+ // window.localStorage.setItem(localStorage_i18nextLng_key, lng)
68
+ Cookies.set(localStorage_i18nextLng_key, lng, {
69
+ expires: 36525,
70
+ domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
71
+ });
72
+ }
73
+
74
+ getAllLanguage(): typeof allLanguage {
75
+ return allLanguage;
76
+ }
77
+
78
+ getCurrentLanguage(): LanguageKeyType {
79
+ return self._currentLanguage;
80
+ }
81
+
82
+ private async _handleData() {
83
+
84
+ const lng: string = self._currentLanguage;
85
+ const resource: string | null = window.localStorage.getItem(localStorage_i18nextResources_key);
86
+ // const resource: string | undefined = Cookies.get(decodeURIComponent(localStorage_i18nextResources_key));
87
+ let i18nextResources: Resource | null = resource ? JSON.parse(resource) : null;
88
+
89
+ if (!i18nextResources) {
90
+ // console.log('######892io3jlkl')
91
+ i18nextResources = await self._fetchData();
92
+ } else {
93
+ self._fetchData();
94
+ }
95
+
96
+ await i18next
97
+ .init({
98
+ // the translations
99
+ // (tip move them in a JSON file and import them,
100
+ // or even better, manage them via a UI: https://react.i18next.com/guides/multiple-translation-files#manage-your-translations-with-a-management-gui)
101
+ resources: i18nextResources,
102
+ lng: lng, // if you're using a language detector, do not define the lng option
103
+ fallbackLng: 'zh_CN',
104
+
105
+ interpolation: {
106
+ escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
107
+ },
108
+ });
109
+ }
110
+
111
+ private async _fetchData(): Promise<Resource> {
112
+ const res: any = await axios.get(ossJsonUrl, {
113
+ withCredentials: false,
114
+ });
115
+ // console.log(res, 'data09oiw3qjelsfkdfjlsdkfjl');
116
+
117
+ const en_US: { [key: string]: string } = {};
118
+ const zh_CN: { [key: string]: string } = {};
119
+
120
+ for (const [key, value] of Object.entries(res)) {
121
+ // console.log(key, value, 'key, value90iowsldfjlsdkj');
122
+ en_US[key] = (value as any)['en_US'];
123
+ zh_CN[key] = (value as any)['zh_CN'];
124
+ }
125
+
126
+ const result: Resource = {
127
+ en_US: {
128
+ translation: en_US,
129
+ },
130
+ zh_CN: {
131
+ translation: zh_CN,
132
+ },
133
+ };
134
+
135
+ // console.log(result, 'result093sdolkfjlsdkjl');
136
+ window.localStorage.setItem(localStorage_i18nextResources_key, JSON.stringify(result));
137
+ // Cookies.set(localStorage_i18nextResources_key, encodeURIComponent(JSON.stringify(result)), {
138
+ // expires: 36525,
139
+ // domain: FUtil.Format.completeUrlByDomain('').replace(/http(s)?:\/\//, ''),
140
+ // });
141
+
142
+ return result;
143
+ }
144
+ }
145
+
146
+ 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
1
  import FUtil from './utils';
2
2
  import FServiceAPI from './service-API';
3
+ import FI18n from './i18n';
3
4
 
4
5
  export {
5
6
  FUtil,
6
7
  FServiceAPI,
8
+ FI18n,
7
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
+ // }
@@ -12,6 +12,7 @@ import * as Event from './events';
12
12
  import * as Activity from './activities';
13
13
  import * as TestQualification from './testQualifications';
14
14
  import * as Statistic from './statistics';
15
+ import * as I18n from './i18n';
15
16
 
16
17
  const FServiceAPI = {
17
18
  Node,
@@ -28,6 +29,7 @@ const FServiceAPI = {
28
29
  Activity,
29
30
  TestQualification,
30
31
  Statistic,
32
+ I18n,
31
33
  };
32
34
 
33
35
  export default FServiceAPI;
@@ -33,7 +33,7 @@ interface IResourceInfo {
33
33
  // 创建资源
34
34
  export interface CreateParamsType {
35
35
  name: string;
36
- resourceType: string;
36
+ resourceType: string[]
37
37
  policies?: any[];
38
38
  coverImages?: string[];
39
39
  intro?: string;
@@ -272,13 +272,13 @@ interface UpdateObjectParamsType {
272
272
  type: string;
273
273
  versionRange?: string;
274
274
  }[];
275
- resourceType?: string;
275
+ resourceType?: string[];
276
276
  }
277
277
 
278
278
  export function updateObject({objectIdOrName, ...params}: UpdateObjectParamsType) {
279
279
  return FUtil.Request({
280
280
  method: 'PUT',
281
- url: `/v1/storages/objects/${objectIdOrName}`,
281
+ url: `/v2/storages/objects/${objectIdOrName}`,
282
282
  data: params,
283
283
  });
284
284
  }
@@ -301,7 +301,7 @@ export function batchObjectList(params: BatchObjectListParamsType) {
301
301
  // 根据sha1和类型获取文件属性
302
302
  interface FilePropertyParamsType {
303
303
  sha1: string;
304
- resourceType: string;
304
+ // resourceType: string;
305
305
  }
306
306
 
307
307
  export function fileProperty({sha1, ...params}: FilePropertyParamsType) {
@@ -87,3 +87,12 @@ export async function policyCodeTranslationToText(code: string, targetType: stri
87
87
  };
88
88
  }
89
89
  }
90
+
91
+
92
+ /**
93
+ * 将资源类型关键字数组,转换成标准展示文字
94
+ * @param arr 关键字数组
95
+ */
96
+ export function resourceTypeKeyArrToResourceType(arr: string[]): string {
97
+ return arr.join(' / ');
98
+ }
@@ -4,6 +4,7 @@ import * as LinkTo from './linkTo';
4
4
  import * as Predefined from './predefined';
5
5
  import Axios, {request} from './axios';
6
6
  import * as Tool from './tools';
7
+ // import I18n from '../i18n';
7
8
 
8
9
  const FUtil = {
9
10
  Format,
@@ -13,6 +14,7 @@ const FUtil = {
13
14
  Axios,
14
15
  Request: request,
15
16
  Tool,
17
+ // i18n: new I18n(),
16
18
  };
17
19
 
18
20
  export default FUtil;