td-octopus 0.1.2 → 0.1.4

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.
@@ -0,0 +1,49 @@
1
+ const fs = require('fs');
2
+ const axios = require('axios');
3
+ const XLSX = require('xlsx');
4
+ const path = require('path');
5
+
6
+ const { getProjectConfig } = require('../src/utils');
7
+ const { convertJsonToXlsx } = require('../src/download');
8
+ const { OCTOPUS_CONFIG_FILE } = require('../src/utils/const')
9
+
10
+ exports.command = 'download';
11
+
12
+ exports.describe = 'download 正确配置downloadUrl里的code与projectId后执行,可以从司南下载翻译文档xls到本地目录';
13
+
14
+ exports.handler = async (argv) => {
15
+ const config = getProjectConfig();
16
+ const otpPath = path.resolve(process.cwd(), config.otpDir);
17
+ const url = config.downloadUrl
18
+ let lang = "en-US"
19
+ if (!url) {
20
+ console.log(`请配置${OCTOPUS_CONFIG_FILE}里面的downloadUrl`)
21
+ return;
22
+ }
23
+ axios.get(url)
24
+ .then((response) => {
25
+ // 给目录和翻译文件item赋值
26
+ const jsonData = response?.data?.data?.jsonData?.children || [];
27
+ // 将嵌套的 JSON 数据转换为 node-xlsx 格式
28
+ const xlsxData = convertJsonToXlsx(jsonData, module);
29
+
30
+ const options = {
31
+ '!cols': [{ wpx: 100 }, { wpx: 100 }, { wpx: 100 }, { wpx: 100 }]
32
+ };
33
+
34
+ const worksheet = XLSX.utils.aoa_to_sheet(xlsxData[0]?.data);
35
+ worksheet['!cols'] = options['!cols'];
36
+
37
+ const workbook = XLSX.utils.book_new();
38
+ XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
39
+ XLSX.writeFile(workbook, `${otpPath}/${lang}/translate_${lang}.xls`);
40
+ console.log(`下载成功`)
41
+
42
+ })
43
+ .catch((error) => {
44
+ console.error(`Error: ${error.message}`);
45
+ console.error(`请正确配置downloadUrl里的code与projectId`);
46
+
47
+ });
48
+ }
49
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "td-octopus",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "I18N tool",
5
5
  "author": "Anthony Li",
6
6
  "bin": {
@@ -9,6 +9,7 @@
9
9
  "license": "MIT",
10
10
  "dependencies": {
11
11
  "@babel/core": "^7.18.2",
12
+ "axios": "^1.6.5",
12
13
  "baidu-translate": "^1.3.0",
13
14
  "colors": "^1.4.0",
14
15
  "glob": "^8.0.3",
@@ -25,7 +26,7 @@
25
26
  "typescript": "^3.2.2",
26
27
  "update-notifier": "^4.1.0",
27
28
  "vue-template-compiler": "^2.6.14",
28
- "xlsx": "0.12.2",
29
+ "xlsx": "0.16.9",
29
30
  "yargs": "^15.3.1"
30
31
  },
31
32
  "keywords": [
@@ -0,0 +1,25 @@
1
+ const convertJsonToXlsx = (data, n) => {
2
+
3
+ let module = ["中文", "英文"]
4
+ const result = [{ name: 'Sheet1', data: [['需要翻译的字段'].concat(["中文", "人工翻译"])] }];
5
+
6
+ const traverse = (node) => {
7
+ if (node.children && node.children.length > 0) {
8
+ node.children.forEach(child => traverse(child));
9
+ } else {
10
+ let arr = [node.path]
11
+ module?.map(item => {
12
+ arr.push(node[item])
13
+ })
14
+ result[0].data.push(arr);
15
+ }
16
+ };
17
+ data?.map(item => {
18
+ return traverse(item);
19
+ })
20
+ return result;
21
+ };
22
+
23
+ module.exports = {
24
+ convertJsonToXlsx
25
+ }
@@ -30,8 +30,9 @@ function importExcel() {
30
30
  }
31
31
 
32
32
  spinner.start('正在从excel开始同步')
33
- distLang.forEach((lang) => {
34
- parseExcel(otpPath + `/${lang}/translate_${lang}.xls`, function (translateMap) {
33
+ distLang.concat("zh-CN").forEach((lang) => {
34
+ let isZhCN = lang === "zh-CN"
35
+ parseExcel(otpPath + `/${isZhCN ? "en-US" : lang}/translate_${isZhCN ? "en-US" : lang}.xls`, function (translateMap) {
35
36
  const currentLangMap = syncLang(lang);
36
37
  const langFlat = flatObject(currentLangMap);
37
38
 
@@ -43,7 +44,7 @@ function importExcel() {
43
44
 
44
45
  // 重新生成翻译文件
45
46
  rewriteFiles(fileKeyValueList, lang);
46
- });
47
+ }, isZhCN);
47
48
  })
48
49
  spinner.succeed('从excel同步成功')
49
50
  })()
@@ -22,13 +22,14 @@ const PROJECT_CONFIG = {
22
22
  fileSuffix: ['.ts', '.js', '.vue', '.jsx', '.tsx'], //默认只提取当前后缀名
23
23
  defaultTranslateKeyApi: 'Pinyin', // 批量提取文案时生成key值时的默认翻译源
24
24
  importI18N: `import I18N from 'src/utils/I18N';`,
25
- include:['./src'],
26
- exclude:[],
25
+ include: ['./src'],
26
+ exclude: [],
27
27
  regPath: 'locale', // 正则匹配路径地址使用
28
28
  resourcePath: [
29
29
  "./src/constants/locale"
30
30
  ],
31
- reservedKey: ["template", "case"]
31
+ reservedKey: ["template", "case"],
32
+ downloadUrl: "https://sinan.tongdun.me/api/i18n/queryConfig?code=&projectId="
32
33
  },
33
34
  langMap: {
34
35
  ['en-US']: 'en',
@@ -154,8 +154,8 @@ async function getAdjustLangObjAndAddList(lang, langObj = {}, zhCNObj = {}, baid
154
154
 
155
155
  adjustLangObj[key] = langObj[key] || zhCNObj[key];
156
156
  }
157
- if(baiduApiKey) {
158
- spinner.text = `当前配置了百度翻译,预计翻译时间需要${(needAddList.length / 60)?.toFixed(2)}分钟,如果等不及,请先去掉百度翻译配置`
157
+ if (baiduApiKey) {
158
+ spinner.text = `当前配置了百度翻译,预计翻译时间需要${(needAddList.length / 60)?.toFixed(2)}分钟,如果等不及,请先去掉百度翻译配置`
159
159
  }
160
160
  const addList = await combinText(needAddList, lang, spinner);
161
161
 
@@ -315,7 +315,7 @@ function generateExcel(addList, path, lang) {
315
315
  * @param {*} path excel的路径地址
316
316
  * @returns {"a.b.c": "test"}
317
317
  */
318
- function parseExcel(path, callback) {
318
+ function parseExcel(path, callback, isZhCN = false) {
319
319
  if (!shell.test('-e', path)) {
320
320
  console.log('当前目录下没有找到翻译.xls文件');
321
321
  return;
@@ -331,8 +331,12 @@ function parseExcel(path, callback) {
331
331
 
332
332
  const translateMap = list.reduce((total, item) => {
333
333
  const key = item['需要翻译的字段'];
334
- const value = item['人工翻译'];
335
-
334
+ let value
335
+ if (isZhCN) {
336
+ value = item['中文'];
337
+ } else {
338
+ value = item['人工翻译'];
339
+ }
336
340
  total[key] = value;
337
341
  return total;
338
342
  }, {});