gc_i18n 1.1.0 → 1.1.2

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.
@@ -2,11 +2,13 @@
2
2
  import _ from "lodash-es";
3
3
  import { getAllTranslate, getLanguages, saveTranslate } from "../libs/service";
4
4
  import { Message, Modal } from "view-ui-plus";
5
+ import { convertArrayToObject } from "../libs/utils";
5
6
 
6
7
  export default {
7
8
  data() {
8
9
  return {
9
10
  isModalVisible: false,
11
+ loading: false,
10
12
  searchText: "",
11
13
  name: "",
12
14
  type: "pageData",
@@ -52,7 +54,9 @@ export default {
52
54
  </Tooltip>
53
55
  </span>
54
56
  ) : (
55
- <span style={{ color: "green" }}>正常</span>
57
+ <span style={{ color: "green" }}>
58
+ {row.common ? "全局" : "当前页"}
59
+ </span>
56
60
  );
57
61
  }
58
62
  }
@@ -86,49 +90,18 @@ export default {
86
90
  modelValue={data}
87
91
  onOnChange={(e) => {
88
92
  const value = e.target.value;
89
- if (this.type === "pageData") {
90
- this.pageData[index][key] = value;
91
- } else {
92
- this.data[index] = {
93
- ...this.data[index],
94
- [key]: value
95
- };
96
- }
97
- const { dictKey, page } = row;
98
- const findIndex = _.findIndex(this.mySaveData, {
99
- key: dictKey,
100
- lang: lang.code
101
- });
102
- console.log("page", page);
103
- if (dictKey && findIndex === -1) {
104
- this.mySaveData.push({
105
- page: page || this.name,
106
- key: dictKey,
107
- lang: lang.code,
108
- value
109
- });
110
- } else {
111
- this.mySaveData[findIndex]["value"] = value;
112
- }
93
+ const { dictKey } = row;
94
+ const findIndex = _.findIndex(this.mySaveData, { dictKey });
113
95
 
114
- const findZhIndex = _.findIndex(this.mySaveData, {
115
- key: dictKey,
116
- page: this.name,
117
- lang: "zh-CN"
118
- });
119
- if (findZhIndex == -1 && row["zh-CN"] === dictKey) {
96
+ if (findIndex > -1) {
97
+ this.mySaveData[findIndex][key] = value;
98
+ } else {
120
99
  this.mySaveData.push({
121
- page: this.name,
122
- key: dictKey,
123
- lang: "zh-CN",
124
- value: _.get(
125
- _.find(this.initialData, { dictKey }),
126
- "zh-CN",
127
- dictKey
128
- )
100
+ ...row,
101
+ [key]: value,
102
+ page: this.type === "pageData" ? this.name : "common"
129
103
  });
130
104
  }
131
- console.log(" this.mySaveData", this.mySaveData);
132
105
  }}
133
106
  ></Input>
134
107
  );
@@ -147,6 +120,7 @@ export default {
147
120
  methods: {
148
121
  closeModal() {
149
122
  this.searchText = "";
123
+ this.type = "pageData";
150
124
  this.isModalVisible = false;
151
125
  },
152
126
  openModal({ name }) {
@@ -161,11 +135,31 @@ export default {
161
135
  },
162
136
 
163
137
  async saveData({ msg = "保存成功" }, close = true) {
164
- console.log("this.mySaveData", this.mySaveData);
138
+ // 提取翻译值的逻辑
139
+ const extractedData = this.mySaveData.map((item) => {
140
+ return _.map(this.languages, (lang) => {
141
+ if (!_.isEmpty(item[lang.code])) {
142
+ return {
143
+ key: item.dictKey,
144
+ page: item.page,
145
+ lang: lang.code,
146
+ value: item[lang.code]
147
+ };
148
+ }
149
+ return undefined;
150
+ });
151
+ });
152
+ const data = _.compact(extractedData[0]);
153
+
154
+ // 保存翻译值
155
+ if (_.isEmpty(data)) {
156
+ Message.error({ content: "没有需要保存的数据" });
157
+ return;
158
+ }
165
159
  const res = await saveTranslate(
166
160
  {
167
161
  appCode: this.appCode,
168
- data: this.mySaveData
162
+ data
169
163
  },
170
164
  this.token
171
165
  );
@@ -194,6 +188,7 @@ export default {
194
188
  }
195
189
  },
196
190
  async getPageData(firstResult = 1) {
191
+ this.loading = true;
197
192
  const res = await getAllTranslate(
198
193
  {
199
194
  appCode: this.appCode,
@@ -220,7 +215,7 @@ export default {
220
215
  page: "common"
221
216
  });
222
217
  if (commonData) {
223
- arr.push(commonData);
218
+ arr.push({ ...commonData, page: this.name, common: true });
224
219
  } else {
225
220
  arr.push({
226
221
  dictKey: key,
@@ -232,10 +227,12 @@ export default {
232
227
  }
233
228
  });
234
229
  this.pageData = _.cloneDeep(arr);
230
+ this.initialData = _.cloneDeep(this.pageData);
235
231
  this.pageDataShow = arr.slice(
236
232
  (firstResult - 1) * this.page.pageSize,
237
233
  firstResult * this.page.pageSize
238
234
  );
235
+ this.loading = false;
239
236
  // this.page = {
240
237
  // firstResult: res.data.retVal.firstResult,
241
238
  // pageSize: res.data.retVal.pageSize,
@@ -244,6 +241,7 @@ export default {
244
241
  }
245
242
  },
246
243
  async search(data, type) {
244
+ this.loading = true;
247
245
  const res = await getAllTranslate(
248
246
  {
249
247
  appCode: this.appCode,
@@ -264,6 +262,7 @@ export default {
264
262
  totalRows: res.data.retVal.totalRows
265
263
  };
266
264
  }
265
+ this.loading = false;
267
266
  },
268
267
  changeTab(name) {
269
268
  this.type = name;
@@ -293,96 +292,120 @@ export default {
293
292
  :width="70"
294
293
  title="多语言管理中心"
295
294
  footer-hide
295
+ @on-cancel="closeModal"
296
296
  >
297
- <Tabs
298
- v-model="type"
299
- type="card"
300
- class="gc_i18n_tabs"
301
- @on-click="changeTab"
302
- >
303
- <TabPane
304
- name="pageData"
305
- label="当前页面"
306
- >
307
- <Table
308
- border
309
- :height="380"
310
- :columns="columns"
311
- :data="pageDataShow"
312
- ></Table
313
- ></TabPane>
314
- <TabPane
315
- name="data"
316
- label="当前应用"
317
- >
318
- <Table
319
- border
320
- :height="380"
321
- :columns="columns"
322
- :data="data"
323
- ></Table
324
- ></TabPane>
325
- <template #extra>
326
- <div
327
- class="extra"
328
- v-if="type === 'data'"
297
+ <template v-if="isModalVisible">
298
+ <div>
299
+ <Tabs
300
+ v-model="type"
301
+ type="card"
302
+ class="gc_i18n_tabs"
303
+ @on-click="changeTab"
329
304
  >
330
- <Input
331
- prefix="ios-search"
332
- placeholder="请输入搜索内容"
333
- v-model="searchText"
334
- clearable
335
- @on-enter="search"
336
- @on-clear="search"
337
- />
338
- </div>
339
- </template>
340
- </Tabs>
305
+ <TabPane
306
+ name="pageData"
307
+ label="当前页面"
308
+ >
309
+ <Spin
310
+ class="gc_i18n_spin"
311
+ v-if="loading"
312
+ ></Spin>
313
+ <Table
314
+ v-else
315
+ border
316
+ :height="380"
317
+ :columns="columns"
318
+ :data="pageDataShow"
319
+ ></Table
320
+ ></TabPane>
321
+ <TabPane
322
+ name="data"
323
+ label="当前应用"
324
+ >
325
+ <Spin
326
+ class="gc_i18n_spin"
327
+ v-if="loading"
328
+ ></Spin>
329
+ <Table
330
+ v-else
331
+ border
332
+ :height="380"
333
+ :columns="columns"
334
+ :data="data"
335
+ ></Table
336
+ ></TabPane>
337
+ <template #extra>
338
+ <div
339
+ class="extra"
340
+ v-if="type === 'data'"
341
+ >
342
+ <Input
343
+ prefix="ios-search"
344
+ placeholder="请输入搜索内容"
345
+ v-model="searchText"
346
+ clearable
347
+ @on-enter="search"
348
+ @on-clear="search"
349
+ />
350
+ </div>
351
+ </template>
352
+ </Tabs>
341
353
 
342
- <div class="gc_i18n_page">
343
- <div style="display: flex">
344
- <Page
345
- v-if="type === 'pageData'"
346
- v-model="firstResult"
347
- :total="translationKeys.length"
348
- :page-size="10"
349
- simple
350
- show-total
351
- @on-change="changePage"
352
- />
353
- <Page
354
- v-else
355
- v-model="firstResult"
356
- :total="page.totalRows"
357
- :page-size="10"
358
- simple
359
- show-total
360
- @on-change="changePage"
361
- />
362
- <div>
363
-
364
- {{ type === "pageData" ? translationKeys.length : page.totalRows }}
365
- 项数据
354
+ <div class="gc_i18n_page">
355
+ <div style="display: flex">
356
+ <Page
357
+ v-if="type === 'pageData'"
358
+ v-model="firstResult"
359
+ :total="translationKeys.length"
360
+ :page-size="10"
361
+ simple
362
+ show-total
363
+ @on-change="changePage"
364
+ />
365
+ <Page
366
+ v-else
367
+ v-model="firstResult"
368
+ :total="page.totalRows"
369
+ :page-size="10"
370
+ simple
371
+ show-total
372
+ @on-change="changePage"
373
+ />
374
+ <div>
375
+
376
+ {{
377
+ type === "pageData" ? translationKeys.length : page.totalRows
378
+ }}
379
+ 项数据
380
+ </div>
381
+ </div>
382
+ <div>
383
+ <Button
384
+ style="margin-right: 10px"
385
+ @click="closeModal"
386
+ >关闭</Button
387
+ >
388
+ <Button
389
+ type="primary"
390
+ @click="saveData"
391
+ >保存</Button
392
+ >
393
+ </div>
366
394
  </div>
367
395
  </div>
368
- <div>
369
- <Button
370
- style="margin-right: 10px"
371
- @click="closeModal"
372
- >关闭</Button
373
- >
374
- <Button
375
- type="primary"
376
- @click="saveData"
377
- >保存</Button
378
- >
379
- </div>
380
- </div>
396
+ </template>
381
397
  </Modal>
382
398
  </template>
383
- <style lang="less">
399
+ <style lang="less" scoped>
400
+ .gc_i18n_spin {
401
+ min-height: 300px;
402
+ display: flex;
403
+ justify-content: center;
404
+ padding-top: 50px;
405
+ }
384
406
  .gc_i18n_tabs {
385
407
  padding-top: 5px;
408
+
386
409
  .ivu-tabs-bar {
387
410
  margin-bottom: 1px !important;
388
411
  }
package/packages/index.js CHANGED
@@ -11,10 +11,26 @@ import "./libs/http";
11
11
  import _ from "lodash-es";
12
12
  import store2 from "store2";
13
13
 
14
+ import { jws } from "jsrsasign";
15
+
16
+ // 生成 JWT 的函数
17
+ function generateJWT(orgCode) {
18
+ const header = {
19
+ alg: "HS512" // 算法
20
+ };
21
+ const payload = {
22
+ orgCode
23
+ };
24
+ const sHeader = JSON.stringify(header);
25
+ const sPayload = JSON.stringify(payload);
26
+ const secret = "";
27
+ // 同步生成 JWT
28
+ return jws.JWS.sign("HS512", sHeader, sPayload, secret);
29
+ }
14
30
  export default class I18n {
15
31
  constructor(options = {}) {
16
- const { router, appCode, messages, token, locale } = options;
17
- this.token = token || store2.get("token");
32
+ const { router, appCode, messages, token, orgCode } = options;
33
+ this.token = token || generateJWT(orgCode);
18
34
  this.appCode = appCode;
19
35
  this.router = router;
20
36
  this.locale = store2.get("I18N_LANGUAGE") || navigator.language || "zh-CN";
@@ -105,9 +121,6 @@ export default class I18n {
105
121
  });
106
122
 
107
123
  const originalT = this.i18n.global.t;
108
- globalThis.$deepScan = function (val) {
109
- return val;
110
- };
111
124
 
112
125
  globalThis.$t = this.i18n.global.t = (key, args, comment) => {
113
126
  const routeName = _.toUpper(this.router?.currentRoute?.value?.name);
@@ -138,7 +151,11 @@ export default class I18n {
138
151
  return originalTranslation;
139
152
  };
140
153
 
141
- globalThis.$$t = (key, val, nameSpace) => {
154
+ globalThis.$deepScan = function (val) {
155
+ return val;
156
+ };
157
+
158
+ globalThis.$t = (key, val, nameSpace) => {
142
159
  const languageStore = store2.namespace(`I18N_${_.toUpper(this.appCode)}`);
143
160
  const message = _.get(languageStore.get(this.locale), "translatesDTOs");
144
161
  // 获取i18n语言数据
@@ -160,6 +177,22 @@ export default class I18n {
160
177
  getLanguage(language) {
161
178
  return store2.get("I18N_TEST.zh-TW");
162
179
  }
180
+ async getLanguages(isRemote = false) {
181
+ const langs = store2.get("I18N_LANGUAGES");
182
+ if (langs && !isRemote) {
183
+ return langs;
184
+ } else {
185
+ const res = await getLanguages({
186
+ appCode: this.appCode,
187
+ token: this.token
188
+ });
189
+ if (res.data) {
190
+ const languages = _.get(res.data, "retVal");
191
+ store2.set("I18N_LANGUAGES", languages);
192
+ return languages;
193
+ }
194
+ }
195
+ }
163
196
  async setLanguage(language) {
164
197
  return new Promise(async (resolve, reject) => {
165
198
  const res = await getTranslate({
package/src/main.js CHANGED
@@ -7,13 +7,12 @@ import zh from "view-ui-plus/dist/locale/zh-CN";
7
7
  const i18n = new gc_i18n({
8
8
  appCode: "TEST",
9
9
  router,
10
- token:
11
- "eyJhbGciOiJIUzUxMiJ9.eyJkZXZpY2VUeXBlIjoiQ09NUFVURVIiLCJtYWluQXBwQ29kZSI6IlNTTyIsIm9yZ0NvZGUiOiJHUkVFTkNMT1VEIiwidWNTZXJ2ZXJVcmwiOiJodHRwczovL3Rlc3QuaWhvdGVsLmNuL3VjLXdlYi8iLCJhcHBDb2RlIjoiU1NPIiwidXNlclR5cGUiOiJOT1JNQUwiLCJsb2dpbkF0IjoxNzQ0MTA0OTQ5MDAwLCJwcmluY2lwYWxVc2VyQ29kZSI6IkhRTCIsImV4cCI6MzI1MDM2NTEyMDAsInVzZXJDb2RlIjoiSFFMIiwic3NvIjoiU1NPX1NFUlZFUiJ9.ra6-Yz0FEZdLObSuR1UsP8v2f3TXUWw0NDBKYUSrR4gbFtlrtgQcyp78LF3rLJMIaGn7A_YmqrzxP1pW4-ZGyA",
10
+ orgCode: "GREENCLOUD",
12
11
  messages: {
13
12
  "zh-CN": { ...zh, test: "合计{0}条" }
14
13
  }
15
14
  });
16
-
15
+ window.i18n = i18n;
17
16
  const app = createApp(App).use(router).use(i18n);
18
17
 
19
18
  app.mount("#app");
@@ -51,7 +51,7 @@ const change = (lang) => {
51
51
  </div>
52
52
  <div>
53
53
  <div>测试:</div>
54
- <div>{{ $t("4j3dy86", "找不到") }}</div>
54
+ <div>{{ $t("ewzh2", "找不到") }}</div>
55
55
  </div>
56
56
  <div>
57
57
  <div>测试:</div>
package/src/view/Home.vue CHANGED
@@ -2,8 +2,10 @@
2
2
  import { useI18n } from "vue-i18n";
3
3
  const { locale, changeLocal } = useI18n();
4
4
 
5
- const change = (lang) => {
6
- locale.value = lang;
5
+ const change = async (lang) => {
6
+ const res = await i18n.getLanguages();
7
+ console.log("res", res);
8
+ // locale.value = lang;
7
9
  };
8
10
  </script>
9
11
 
package/vite.config.js CHANGED
@@ -19,7 +19,15 @@ export default defineConfig({
19
19
  rollupOptions: {
20
20
  terserOptions: false,
21
21
  // 确保外部化处理那些你不想打包进库的依赖
22
- external: ["vue", "axios", "lodash-es", "view-ui-plus"],
22
+ external: [
23
+ "vue",
24
+ "axios",
25
+ "lodash-es",
26
+ "view-ui-plus",
27
+ "store2",
28
+ "jsrsasign",
29
+ "keyboardjs"
30
+ ],
23
31
  output: {
24
32
  // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
25
33
  globals: {
@@ -40,7 +48,7 @@ export default defineConfig({
40
48
  // outDir:
41
49
  // "/Users/huqiliang/Documents/github/auto-i18n-translation-plugins/example/vue3/node_modules/gc_i18n/lib"
42
50
  // outDir:
43
- // "/Users/huqiliang/Documents/lvyun/travel-ship-standard-front/node_modules/gc_i18n/lib"
51
+ // "/Users/huqiliang/Documents/lvyun/dpms-front/node_modules/gc_i18n/lib"
44
52
  },
45
53
  plugins: [vue(), vueJsx(), libCss()]
46
54
  });