yh-i18n 2.2.11 → 2.2.13

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.
Files changed (4) hide show
  1. package/excelTool.ts +123 -120
  2. package/index.js +17 -10
  3. package/list.vue +21 -6
  4. package/package.json +19 -19
package/excelTool.ts CHANGED
@@ -5,58 +5,58 @@ import http from "@/libs/api.request";
5
5
  import { useI18nStore } from "./index";
6
6
 
7
7
  const verificationCode = "lkyhtranslateexcel";
8
- export async function exportExcel(total) {
9
- const loading = ElLoading.service({
8
+ export async function exportExcel ( total ) {
9
+ const loading = ElLoading.service( {
10
10
  lock: true,
11
11
  text: "正在获取所有翻译字段……",
12
12
  background: "rgba(255, 255, 255, 0.2)",
13
- });
13
+ } );
14
14
  const i18nStore = useI18nStore();
15
15
  let records = await http
16
- .request({
16
+ .request( {
17
17
  url: "/translate/select",
18
18
  method: "post",
19
19
  data: {
20
20
  pageNum: 1,
21
21
  pageSize: total,
22
22
  },
23
- })
24
- .then((res) => {
25
- let localKeys = i18nStore.localList.map((item) => item.value);
23
+ } )
24
+ .then( ( res ) => {
25
+ let localKeys = i18nStore.localList.map( ( item ) => item.value );
26
26
  let { records } = res.data.data;
27
- records = records.map((item) => {
27
+ records = records.map( ( item ) => {
28
28
  try {
29
- let content = JSON.parse(item.content);
30
- let keys = Object.keys(content);
29
+ let content = JSON.parse( item.content );
30
+ let keys = Object.keys( content );
31
31
 
32
- keys.forEach((key) => {
33
- let val = content[key];
34
- item[key] = val;
35
- });
36
- localKeys.forEach((k) => {
37
- if (!keys.includes(k)) {
38
- item[k] = "";
32
+ keys.forEach( ( key ) => {
33
+ let val = content[ key ];
34
+ item[ key ] = val;
35
+ } );
36
+ localKeys.forEach( ( k ) => {
37
+ if ( !keys.includes( k ) ) {
38
+ item[ k ] = "";
39
39
  }
40
- });
41
- } catch (error) {}
40
+ } );
41
+ } catch ( error ) { }
42
42
  return item;
43
- });
43
+ } );
44
44
  return records;
45
- });
46
- loading.setText("正在序列化表格数据……");
45
+ } );
46
+ loading.setText( "正在序列化表格数据……" );
47
47
  const workbook = new ExcelJS.Workbook();
48
48
  workbook.creator = "力控远海";
49
49
  workbook.lastModifiedBy = "力控远海技术中心前端";
50
50
  workbook.keywords = verificationCode;
51
51
  workbook.created = new Date();
52
52
  workbook.modified = new Date();
53
- const sheet = workbook.addWorksheet(`${Config.title}-系统待翻译文字`);
53
+ const sheet = workbook.addWorksheet( `${ Config.title }-系统待翻译文字` );
54
54
 
55
55
  let columns: any[] = [
56
56
  { width: 0, hidden: true },
57
57
  { width: 0, hidden: true },
58
58
  ];
59
- let title: string[] = ["", ""];
59
+ let title: string[] = [ "", "" ];
60
60
  let titleObj = {
61
61
  zh_CN: "中文",
62
62
  en_US: "英语",
@@ -64,30 +64,30 @@ export async function exportExcel(total) {
64
64
  vi: "越南语",
65
65
  tr: "土耳其语",
66
66
  };
67
- Config.i18nList.forEach((item) => {
68
- columns.push({
67
+ Config.i18nList.forEach( ( item ) => {
68
+ columns.push( {
69
69
  width: 30,
70
70
  alignment: { vertical: "middle", horizontal: "center", wrapText: true },
71
71
  value: item,
72
- });
73
- title.push(titleObj[item]);
74
- });
72
+ } );
73
+ title.push( titleObj[ item ] );
74
+ } );
75
75
 
76
76
  sheet.columns = columns;
77
- sheet.addRow(title);
77
+ sheet.addRow( title );
78
78
  sheet.addRows(
79
- records.map((item) => {
79
+ records.map( ( item ) => {
80
80
  let { adTranslateId, key } = item;
81
- let row = [adTranslateId, key];
82
- Config.i18nList.forEach((valKey) => {
83
- row.push(item[valKey]);
84
- });
81
+ let row = [ adTranslateId, key ];
82
+ Config.i18nList.forEach( ( valKey ) => {
83
+ row.push( item[ valKey ] );
84
+ } );
85
85
  return row;
86
- })
86
+ } )
87
87
  );
88
- sheet.getRows(1, 1)?.forEach((row, index) => {
88
+ sheet.getRows( 1, 1 )?.forEach( ( row, index ) => {
89
89
  row.height = 25;
90
- row.eachCell({ includeEmpty: true }, (cell, cellIndex) => {
90
+ row.eachCell( { includeEmpty: true }, ( cell, cellIndex ) => {
91
91
  cell.font = { bold: true };
92
92
  cell.alignment = {
93
93
  vertical: "middle",
@@ -100,11 +100,11 @@ export async function exportExcel(total) {
100
100
  bottom: { style: "thin" },
101
101
  right: { style: "thin" },
102
102
  };
103
- });
104
- });
105
- sheet.getRows(2, records.length)?.forEach((row, index) => {
103
+ } );
104
+ } );
105
+ sheet.getRows( 2, records.length )?.forEach( ( row, index ) => {
106
106
  row.height = 25;
107
- row.eachCell({ includeEmpty: true }, (cell, cellIndex) => {
107
+ row.eachCell( { includeEmpty: true }, ( cell, cellIndex ) => {
108
108
  cell.alignment = {
109
109
  vertical: "middle",
110
110
  horizontal: "left",
@@ -116,138 +116,141 @@ export async function exportExcel(total) {
116
116
  bottom: { style: "thin" },
117
117
  right: { style: "thin" },
118
118
  };
119
- });
120
- });
121
- sheet.views = [{ state: "frozen", xSplit: 0, ySplit: 1, activeCell: "C2" }];
122
- loading.setText("正在生成表格文件……");
123
- workbook.xlsx.writeBuffer().then((data) => {
124
- const blob = new Blob([data], {
119
+ } );
120
+ } );
121
+ sheet.views = [ { state: "frozen", xSplit: 0, ySplit: 1, activeCell: "C2" } ];
122
+ loading.setText( "正在生成表格文件……" );
123
+ workbook.xlsx.writeBuffer().then( ( data ) => {
124
+ const blob = new Blob( [ data ], {
125
125
  type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8",
126
- });
127
- let downloadA = document.createElement("a");
128
- downloadA.download = `${Config.title}-系统待翻译文字.xlsx`;
129
- downloadA.href = window.URL.createObjectURL(blob);
126
+ } );
127
+ let downloadA = document.createElement( "a" );
128
+ downloadA.download = `${ Config.title }-系统待翻译文字.xlsx`;
129
+ downloadA.href = window.URL.createObjectURL( blob );
130
130
  downloadA.click();
131
131
  loading.close();
132
- ElMessage.success("导出成功,请到下载目录查看");
133
- });
132
+ ElMessage.success( "导出成功,请到下载目录查看" );
133
+ } );
134
134
  }
135
135
 
136
- async function getFile() {
136
+ async function getFile () {
137
137
  // @ts-ignore
138
- let fileHandle = await window.showOpenFilePicker({
138
+ let fileHandle = await window.showOpenFilePicker( {
139
139
  id: "importTranslateExcel",
140
140
  types: [
141
141
  {
142
142
  description: "请选择之前导出的文件",
143
143
  accept: {
144
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": [".xlsx", ".xls"],
144
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": [ ".xlsx", ".xls" ],
145
145
  },
146
146
  },
147
147
  ],
148
- });
149
- if (Array.isArray(fileHandle)) {
150
- fileHandle = fileHandle[0];
148
+ } );
149
+ if ( Array.isArray( fileHandle ) ) {
150
+ fileHandle = fileHandle[ 0 ];
151
151
  }
152
- let state = await fileHandle.queryPermission({ mode: "read" });
153
- if (state === "denied") {
152
+ let state = await fileHandle.queryPermission( { mode: "read" } );
153
+ if ( state === "denied" ) {
154
154
  return false;
155
155
  } else {
156
- if (state === "prompt") {
157
- fileHandle.requestPermission({
156
+ if ( state === "prompt" ) {
157
+ fileHandle.requestPermission( {
158
158
  mode: "read",
159
- });
159
+ } );
160
160
  }
161
161
  return await fileHandle.getFile();
162
162
  }
163
163
  }
164
164
 
165
165
 
166
- async function saveData(dataList){
166
+ async function saveData ( dataList ) {
167
167
  return await http
168
- .request({
169
- url: "/translate/save",
170
- type: "json",
171
- method: "POST",
172
- data: dataList,
173
- })
174
- .then((res) => {
175
- return res?.data?.status === 200
176
- })
168
+ .request( {
169
+ url: "/translate/save",
170
+ type: "json",
171
+ method: "POST",
172
+ data: dataList,
173
+ } )
174
+ .then( ( res ) => {
175
+ return res?.data?.status === 200;
176
+ } );
177
177
  }
178
178
 
179
- export async function importExcel(getDataList) {
180
- if (!window["showOpenFilePicker"]) {
181
- ElMessage({
182
- message: "请使用Chrome(Edge) 102 及以上版本的浏览器",
179
+ export async function importExcel ( e, getDataList ) {
180
+ // if (!window["showOpenFilePicker"]) {
181
+ // ElMessage({
182
+ // message: "请使用Chrome(Edge) 102 及以上版本的浏览器",
183
+ // type: "warning",
184
+ // });
185
+ // return false;
186
+ // }
187
+ // let file = await getFile();
188
+ const file = e.target.files[ 0 ];
189
+ if ( !( file instanceof File ) ) {
190
+ ElMessage( {
191
+ message: "请选择文件",
183
192
  type: "warning",
184
- });
185
- return false;
186
- }
187
- let file = await getFile();
188
- if (file === false) {
189
- ElMessage({
190
- message: "请授权浏览器读取文件",
191
- type: "warning",
192
- });
193
+ } );
193
194
  return false;
194
195
  } else {
195
196
  try {
196
197
  const workbook = new ExcelJS.Workbook();
197
- await workbook.xlsx.load(file);
198
- if (workbook.keywords !== verificationCode) {
199
- ElMessage({
198
+ // @ts-ignore
199
+ await workbook.xlsx.load( file );
200
+ if ( workbook.keywords !== verificationCode ) {
201
+ ElMessage( {
200
202
  message: "您选择的文件不是系统导出的文件",
201
203
  type: "warning",
202
- });
204
+ } );
203
205
  return false;
204
206
  }
205
- const loading = ElLoading.service({
207
+ const loading = ElLoading.service( {
206
208
  lock: true,
207
209
  text: "开始读取已翻译的字段",
208
210
  background: "rgba(255, 255, 255, 0.5)",
209
211
  customClass: "yh-i18n-loading"
210
- });
211
- let sheet = workbook.worksheets[0];
212
+ } );
213
+ let sheet = workbook.worksheets[ 0 ];
214
+ // @ts-ignore
212
215
  let len = sheet._rows.length - 1;
213
216
  let dataList: any[] = [];
214
- sheet.getRows(2, len)?.forEach((row) => {
215
- let id = row.getCell(1).value;
216
- let key = row.getCell(2).value;
217
+ sheet.getRows( 2, len )?.forEach( ( row ) => {
218
+ let id = row.getCell( 1 ).value;
219
+ let key = row.getCell( 2 ).value;
217
220
  let content: any = {};
218
- Config.i18nList.forEach((item, index) => {
219
- content[item] = row.getCell(index + 3).value;
220
- });
221
- dataList.push({
221
+ Config.i18nList.forEach( ( item, index ) => {
222
+ content[ item ] = row.getCell( index + 3 ).value;
223
+ } );
224
+ dataList.push( {
222
225
  id,
223
226
  key,
224
- content: JSON.stringify(content),
225
- });
226
- });
227
- loading.setText("读取完成,正在更新");
228
-
227
+ content: JSON.stringify( content ),
228
+ } );
229
+ } );
230
+ loading.setText( "读取完成,正在更新" );
231
+
229
232
  let dataLen = dataList.length;
230
233
  let flag = true;
231
234
  let saveLength = 200;
232
- while (flag) {
233
- if(dataList.length > saveLength) {
234
- let perCent = (dataLen - dataList.length)/dataLen
235
- let fillIndex = parseInt((perCent / 0.5 * 10).toFixed(0));
236
- let loadingText = new Array(20);
237
- loadingText.fill('◇',0,20-fillIndex)
238
- loadingText.fill('◆',20-fillIndex)
239
- loading.setText(`${(perCent*100).toFixed(2)}% ${loadingText.join('')} 正在更新…………`)
240
- await saveData(dataList.splice(0,saveLength))
235
+ while ( flag ) {
236
+ if ( dataList.length > saveLength ) {
237
+ let perCent = ( dataLen - dataList.length ) / dataLen;
238
+ let fillIndex = parseInt( ( perCent / 0.5 * 10 ).toFixed( 0 ) );
239
+ let loadingText = new Array( 20 );
240
+ loadingText.fill( '◇', 0, 20 - fillIndex );
241
+ loadingText.fill( '◆', 20 - fillIndex );
242
+ loading.setText( `${ ( perCent * 100 ).toFixed( 2 ) }% ${ loadingText.join( '' ) } 正在更新…………` );
243
+ await saveData( dataList.splice( 0, saveLength ) );
241
244
  } else {
242
- await saveData(dataList)
245
+ await saveData( dataList );
243
246
  flag = false;
244
247
  }
245
248
  }
246
249
  loading.close();
247
- ElMessage.success("导入数据保存成功")
250
+ ElMessage.success( "导入数据保存成功" );
248
251
  getDataList();
249
- } catch (error) {
250
- ElMessage.error("导入保存翻译数据遇到错误:",error.message)
252
+ } catch ( error ) {
253
+ ElMessage.error( "导入保存翻译数据遇到错误:", error.message );
251
254
  }
252
255
  }
253
256
  }
package/index.js CHANGED
@@ -25,7 +25,14 @@ import el_vi from "element-plus/dist/locale/vi.mjs";
25
25
  import el_tr from "element-plus/dist/locale/tr.mjs";
26
26
 
27
27
  // 自动根据浏览器系统语言设置语言
28
- const navLang = navigator.language.replace("-", "_");
28
+ let navLang = navigator.language.replace("-", "_");
29
+ if (navLang.indexOf("en") > -1 && navLang !== "en_US") {
30
+ navLang = "en_US";
31
+ }
32
+
33
+ if (navLang.indexOf("zh") > -1 && navLang !== "zh_CN") {
34
+ navLang = "zh_CN";
35
+ }
29
36
  const initlang = localStorage.translateLanguage || navLang || "zh_CN";
30
37
 
31
38
  export let i18n = null;
@@ -40,7 +47,7 @@ Object.keys(zhCNBase).forEach((key) => {
40
47
 
41
48
  const unHandle = [];
42
49
 
43
- function addTranslate(key) {
50
+ function addTranslate (key) {
44
51
  if (key.indexOf("vxe") === -1) {
45
52
  try {
46
53
  axios.request({
@@ -58,7 +65,7 @@ function addTranslate(key) {
58
65
  }
59
66
 
60
67
  let recursionAddTimer = null;
61
- function recursionAddTranslate() {
68
+ function recursionAddTranslate () {
62
69
  if (recursionAddTimer !== null) {
63
70
  return false;
64
71
  }
@@ -99,7 +106,7 @@ export const useI18nStore = defineStore("i18nStore", () => {
99
106
  /**
100
107
  * @param {I18nList} l 要设置的新语言
101
108
  */
102
- function setLang(l) {
109
+ function setLang (l) {
103
110
  lang.value = l;
104
111
  localStorage.translateLanguage = l;
105
112
  if (i18n) {
@@ -116,7 +123,7 @@ export const useI18nStore = defineStore("i18nStore", () => {
116
123
  });
117
124
 
118
125
  const localList = reactive([]);
119
- function setLocalList(list) {
126
+ function setLocalList (list) {
120
127
  list.forEach((item) => {
121
128
  switch (item) {
122
129
  case "zh_CN":
@@ -184,7 +191,7 @@ export const useI18nStore = defineStore("i18nStore", () => {
184
191
  };
185
192
  });
186
193
 
187
- async function getRemoteMessage(list) {
194
+ async function getRemoteMessage (list) {
188
195
  try {
189
196
  let messages = {
190
197
  zh_CN: {},
@@ -231,7 +238,7 @@ async function getRemoteMessage(list) {
231
238
  } catch (error) {}
232
239
  }
233
240
 
234
- function createLocalMessage(list) {
241
+ function createLocalMessage (list) {
235
242
  let messages = {};
236
243
  list.forEach((item) => {
237
244
  switch (item) {
@@ -260,7 +267,7 @@ function createLocalMessage(list) {
260
267
  return messages;
261
268
  }
262
269
 
263
- export function addI18nPage(router) {
270
+ export function addI18nPage (router) {
264
271
  if (isDev) {
265
272
  router.addRoute("Index", {
266
273
  path: "translate",
@@ -274,7 +281,7 @@ export function addI18nPage(router) {
274
281
  }
275
282
  }
276
283
 
277
- export function cLog(string, isError = false) {
284
+ export function cLog (string, isError = false) {
278
285
  if (isError) {
279
286
  console.error("%cyhI18n:%c", "font-size: 16px;font-weight: bold;color: #00ffff", "font-size: 16px;font-weight: bold;color: #ccccc", string);
280
287
  } else {
@@ -283,7 +290,7 @@ export function cLog(string, isError = false) {
283
290
  }
284
291
 
285
292
  export const yhI18n = {
286
- install(app, config) {
293
+ install (app, config) {
287
294
  let { list, router, pinia, VXETable } = config;
288
295
  if (!list) {
289
296
  cLog("请设置 Config.i18nList ,并将其设置到 yhI18n 的 congfig.list 上,否则 国际化插件将失效");
package/list.vue CHANGED
@@ -36,11 +36,13 @@
36
36
  @click="exportExcel(listForm.total)">
37
37
  {{ ct("导出") }}
38
38
  </el-button>
39
- <el-button
40
- text
41
- @click="importExcel(getDataList)">
39
+ <div class="el-button is-text import-excel-button">
40
+ <input
41
+ class="import-excel-input"
42
+ type="file"
43
+ @change="importExcel($event, getDataList)" />
42
44
  {{ ct("导入") }}
43
- </el-button>
45
+ </div>
44
46
  </div>
45
47
  <el-table
46
48
  ref="i18nList"
@@ -475,8 +477,21 @@ onMounted(() => {
475
477
  padding: 0 8px;
476
478
  flex: 1;
477
479
  .el-form--inline .el-form-item {
478
- margin-bottom: 0;
479
- margin-right: 18px;
480
+ margin-bottom: 0;
481
+ margin-right: 18px;
482
+ }
483
+ }
484
+
485
+ .import-excel-button {
486
+ position: relative;
487
+ .import-excel-input {
488
+ position: absolute;
489
+ left: 0;
490
+ top: 0;
491
+ opacity: 0;
492
+ width: 100%;
493
+ height: 100%;
494
+ z-index: 10;
480
495
  }
481
496
  }
482
497
  }
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
- {
2
- "name": "yh-i18n",
3
- "version": "2.2.11",
4
- "description": "对于国际化的封装",
5
- "main": "index.js",
6
- "scripts": {
7
- "pub:npm": "npm publish --registry https://registry.npmjs.org/ --no-git-checks",
8
- "pub:aliyun": "npm publish --registry https://packages.aliyun.com/60765e0161a945067837bb5f/npm/npm-registry/ --no-git-checks"
9
- },
10
- "dependencies": {
11
- "exceljs": "4.4.0"
12
- },
13
- "peerDependencies": {
14
- "vue-i18n": "9.8.0",
15
- "vue": "3.2.47",
16
- "vxe-table": "4.3.10",
17
- "element-plus": "2.3.8"
18
- },
19
- "author": "Liubin"
1
+ {
2
+ "name": "yh-i18n",
3
+ "version": "2.2.13",
4
+ "description": "对于国际化的封装",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "pub:npm": "npm publish --registry https://registry.npmjs.org/ --no-git-checks",
8
+ "pub:aliyun": "npm publish --registry https://packages.aliyun.com/60765e0161a945067837bb5f/npm/npm-registry/ --no-git-checks"
9
+ },
10
+ "dependencies": {
11
+ "exceljs": "4.4.0"
12
+ },
13
+ "peerDependencies": {
14
+ "vue-i18n": "9.8.0",
15
+ "vue": "3.2.47",
16
+ "vxe-table": "4.3.10",
17
+ "element-plus": "2.3.8"
18
+ },
19
+ "author": "Liubin"
20
20
  }