@chaoswise/intl 2.0.0 → 2.1.1

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,6 +2,7 @@
2
2
  const runCollect = require('./scripts/collect');
3
3
  const runUpdate = require('./scripts/update');
4
4
  const runInitConfig = require('./scripts/initConfig');
5
+ const runAddLocale = require('./scripts/addLocale');
5
6
 
6
7
  // 可执行的指令
7
8
  const SCRIPTS = ['intl', 'collect', 'update'];
@@ -24,3 +25,7 @@ if(script === 'collect') {
24
25
  if(script === 'update') {
25
26
  runUpdate();
26
27
  }
28
+ // 收集全部语种
29
+ if(script === 'addLocale') {
30
+ runAddLocale();
31
+ }
@@ -0,0 +1,33 @@
1
+ const getConf = require('./conf');
2
+ const { targetOutputFiles } = require('./util/getTargetFiles');
3
+ const transformAst = require('./util/transformAst');
4
+ const getWordsByIds = require('./util/getWordsByIds');
5
+ const log = require('./util/log');
6
+ const genXlsx = require('./util/genXlsx');
7
+
8
+ async function update() {
9
+ log.info('收集全部词条...');
10
+ const conf = getConf();
11
+ const files = targetOutputFiles(conf);
12
+
13
+ const info = transformAst('update', files, conf, {});
14
+ const _downloadIds = [...new Set(info.downloadIds.filter(Boolean))];
15
+ // 获取全部词条
16
+ const allWords = await getWordsByIds(_downloadIds, conf);
17
+
18
+ const localeKeys = Object.keys(allWords);
19
+ const xlsxData = _downloadIds.map(id => {
20
+ const localeObj = {};
21
+ localeKeys.forEach(key => {
22
+ localeObj[key] = allWords[key][id] || '待翻译';
23
+ });
24
+ return {
25
+ id,
26
+ ...localeObj
27
+ }
28
+ });
29
+
30
+ await genXlsx(xlsxData)
31
+ }
32
+
33
+ module.exports = update;
@@ -1,34 +1,34 @@
1
- const { v4: uuidv4 } = require("uuid");
1
+ const { v4: uuidv4 } = require('uuid');
2
2
 
3
- const getConf = require("./conf");
4
- const { targetEntryFiles } = require("./util/getTargetFiles");
5
- const transformAst = require("./util/transformAst");
6
- const log = require("./util/log");
7
- const file = require("./util/file");
8
- const writeNewWordsFile = require("./util/writeNewWordsFile");
9
- const { readWordJson } = require("./util/getWord");
10
- const getGroupName = require("./util/getGroupName");
11
- const getPkgJson = require("./util/getPkgJson");
3
+ const getConf = require('./conf');
4
+ const { targetEntryFiles } = require('./util/getTargetFiles');
5
+ const transformAst = require('./util/transformAst');
6
+ const log = require('./util/log');
7
+ const file = require('./util/file');
8
+ const writeNewWordsFile = require('./util/writeNewWordsFile');
9
+ const { readWordJson } = require('./util/getWord');
10
+ const getGroupName = require('./util/getGroupName');
11
+ const getPkgJson = require('./util/getPkgJson');
12
12
 
13
- const service = require("./service");
13
+ const service = require('./service');
14
14
 
15
15
  async function collect() {
16
- log.info("工程扫描中...");
16
+ log.info('工程扫描中...');
17
17
 
18
18
  const package = getPkgJson();
19
19
  const conf = getConf();
20
20
  const groupName = getGroupName();
21
- const newWordsFileType = conf.newWordsFileType || "excel";
21
+ const newWordsFileType = conf.newWordsFileType || 'excel';
22
22
 
23
23
  // 检查newWordsFileType
24
- if (!["json", "excel"].includes(newWordsFileType)) {
24
+ if (!['json', 'excel'].includes(newWordsFileType)) {
25
25
  log.error(`配置:newWordsFileType 只能为json或者excel`);
26
26
  process.exit(1);
27
27
  }
28
28
 
29
29
  // 读取wordJson
30
30
  if (conf.localWordPath && !readWordJson(conf.localWordPath)) {
31
- log.error("localWordPath配置有误");
31
+ log.error('localWordPath配置有误');
32
32
  process.exit(1);
33
33
  }
34
34
 
@@ -37,19 +37,19 @@ async function collect() {
37
37
 
38
38
  // 遍历文件,并对代码进行ast处理的方法:transformAst(type, files, conf, replaceWords)
39
39
  // 第一次遍历,只收集中文,不传入replaceWords
40
- const info = transformAst("Collect", files, conf);
40
+ const info = transformAst('Collect', files, conf);
41
41
 
42
42
  // 在国际化平台中获取所有已存在中文信息
43
- const res = await service.searchByZh(info.allWords);
43
+ const res = await service.searchByZh(info.allWords, groupName);
44
44
  if (res.code !== 10000) {
45
- log.error("请求数据出错:" + res.msg);
45
+ log.error('请求数据出错:' + res.msg);
46
46
  process.exit(1);
47
47
  }
48
48
  const wordsMap = res.data;
49
49
  // 获取所有语种
50
50
  const languageRes = await service.getLanguage();
51
51
  if (languageRes.code !== 10000) {
52
- log.error("请求数据出错:" + languageRes.msg);
52
+ log.error('请求数据出错:' + languageRes.msg);
53
53
  process.exit(1);
54
54
  }
55
55
  const languages = languageRes.data;
@@ -76,7 +76,7 @@ async function collect() {
76
76
  if (Object.keys(currentCopy).length < languages.length) {
77
77
  newWords[currentDt.key] = {};
78
78
  languages.forEach((dt) => {
79
- newWords[currentDt.key][dt.key] = currentCopy[dt.key] || "待翻译";
79
+ newWords[currentDt.key][dt.key] = currentCopy[dt.key] || '待翻译';
80
80
  });
81
81
  } else {
82
82
  exist[currentDt.key] = currentDt;
@@ -99,7 +99,7 @@ async function collect() {
99
99
  });
100
100
  // 词条不存在
101
101
  if (value.length === 0) {
102
- newWords[key][dt.key] = "待翻译";
102
+ newWords[key][dt.key] = '待翻译';
103
103
  }
104
104
  // {"zh_CN": "你好a","zh_TW": "","en_US": "hello"},{"zh_CN": "你好a","en_US": "hello"}
105
105
  // 处理en_US在这两个数据中翻译相同的情况,比如 {en_US: hello}
@@ -110,11 +110,11 @@ async function collect() {
110
110
  else if (hasValue.length > 1) {
111
111
  newWords[key][
112
112
  dt.key
113
- ] = `该词条在平台中对应多个翻译,请手动确认:${hasValue.join("/")}`;
113
+ ] = `该词条在平台中对应多个翻译,请手动确认:${hasValue.join('/')}`;
114
114
  }
115
115
  // 词条存在但是该语种未翻译,比如zh_TW在上面数据中都未翻译的情况
116
116
  else {
117
- newWords[key][dt.key] = "待翻译";
117
+ newWords[key][dt.key] = '待翻译';
118
118
  }
119
119
  }
120
120
  });
@@ -125,10 +125,10 @@ async function collect() {
125
125
  await service.saveExist({ version: package.version, groupName, exist });
126
126
 
127
127
  // 第二次遍历,传入replaceWords,对代码进行国际化通用API转化,把词条替换成数据库的id,或者脚本临时生成的uuid
128
- transformAst("collect", files, conf, replaceWords);
128
+ transformAst('collect', files, conf, replaceWords);
129
129
 
130
- log.success("★★★ 脚本执行成功 ★★★");
131
- log.info("国际化配置文件: .intlconfig.js,可根据需求自定义修改");
130
+ log.success('★★★ 脚本执行成功 ★★★');
131
+ log.info('国际化配置文件: .intlconfig.js,可根据需求自定义修改');
132
132
  // 本次扫描出新的待翻译词条,写入到newWords中
133
133
  if (Object.keys(newWords).length) {
134
134
  writeNewWordsFile(newWordsFileType, newWords, package.version);
@@ -138,7 +138,7 @@ async function collect() {
138
138
  if (warnLogs.length) {
139
139
  const fileName = `intl.logs.warn.${new Date()
140
140
  .toLocaleString()
141
- .replace(/[\/ ]/g, "_")}.txt`;
141
+ .replace(/[\/ ]/g, '_')}.txt`;
142
142
  file.write(fileName, JSON.stringify(warnLogs, null, 2));
143
143
  log.warn(`存在脚本无法处理的情况,具体查看 ${fileName} 手动处理`);
144
144
  }
@@ -7,15 +7,15 @@ module.exports = function (excludes = []) {
7
7
 
8
8
  // exclude pattern, <string array>
9
9
  // e.g. ['**/dist/**', '**/*.config.js', '**/*.data.js']
10
- exclude: [
11
- '**/src/public/**',
12
- '**/_MOCK_/**'
13
- ],
10
+ exclude: ['**/src/public/**', '**/_MOCK_/**'],
14
11
 
15
12
  // output path, <string array>
16
13
  // e.g. ['dist']
17
14
  output: ['src'], // default i18n in place
18
15
 
16
+ // 国际化词条导出目录(locales/*.json)
17
+ localeOutput: 'src/public',
18
+
19
19
  // ignored components, <string array>
20
20
  // e.g. ['EventTracker']
21
21
  ignoreComponents: ['style', 'svg'],
@@ -1,9 +1,9 @@
1
- const axios = require("axios");
2
- const getConf = require("../conf");
1
+ const axios = require('axios');
2
+ const getConf = require('../conf');
3
3
 
4
4
  const conf = getConf();
5
5
 
6
- axios.defaults.baseURL = (conf.baseURL || "") + "/api/i18n";
6
+ axios.defaults.baseURL = (conf.baseURL || '') + '/api/i18n';
7
7
 
8
8
  axios.interceptors.response.use(
9
9
  function (response) {
@@ -19,26 +19,26 @@ axios.interceptors.response.use(
19
19
  );
20
20
 
21
21
  // 在国际化平台中获取所有已存在中文信息
22
- exports.searchByZh = (zhs) => {
23
- return axios.post("/content/getContentsByMain", zhs);
22
+ exports.searchByZh = (zhs, groupName) => {
23
+ return axios.post('/content/getContentsByMain?groupName=' + groupName, zhs);
24
24
  };
25
25
 
26
26
  // 查询所有语种
27
27
  exports.getLanguage = () => {
28
- return axios.get("/language/list");
28
+ return axios.get('/language/list');
29
29
  };
30
30
 
31
31
  // 根据词条id获取所有数据库词条信息
32
32
  exports.getJson = (data) => {
33
- return axios.post("/content/downloadContentByKeys", data);
33
+ return axios.post('/content/downloadContentByKeys', data);
34
34
  };
35
35
 
36
36
  // 保存替换的特殊方法
37
37
  exports.saveMethod = (data) => {
38
- return axios.post("/history/method/save", data);
38
+ return axios.post('/history/method/save', data);
39
39
  };
40
40
 
41
41
  // 保存已有词条
42
42
  exports.saveExist = (data) => {
43
- return axios.post("/history/exist/save", data);
43
+ return axios.post('/history/exist/save', data);
44
44
  };
@@ -1,40 +1,40 @@
1
- const getConf = require("./conf");
2
- const { targetOutputFiles } = require("./util/getTargetFiles");
3
- const transformAst = require("./util/transformAst");
4
- const downloadJson = require("./util/downloadJson");
5
- const log = require("./util/log");
6
- const file = require("./util/file");
7
- const getPkgJson = require("./util/getPkgJson");
8
- const getGroupName = require("./util/getGroupName");
1
+ const getConf = require('./conf');
2
+ const { targetOutputFiles } = require('./util/getTargetFiles');
3
+ const transformAst = require('./util/transformAst');
4
+ const downloadJson = require('./util/downloadJson');
5
+ const log = require('./util/log');
6
+ const file = require('./util/file');
7
+ const getPkgJson = require('./util/getPkgJson');
8
+ const getGroupName = require('./util/getGroupName');
9
9
 
10
10
  async function update() {
11
- log.info("词条更新中...");
11
+ log.info('词条更新中...');
12
12
  const conf = getConf();
13
13
  const files = targetOutputFiles(conf);
14
14
 
15
15
  // relationKey { key: id } key是脚本生成的临时id,id是数据库对应词条的id
16
- const relationKey = file.readJson("relationKey.json") || {};
16
+ const relationKey = file.readJson('relationKey.json') || {};
17
17
 
18
- const info = transformAst("update", files, conf, relationKey);
18
+ const info = transformAst('update', files, conf, relationKey);
19
19
  const _downloadIds = [...new Set(info.downloadIds.filter(Boolean))];
20
- await downloadJson(_downloadIds);
20
+ await downloadJson(_downloadIds, conf);
21
21
 
22
22
  const needDelete = Boolean(Object.keys(relationKey).length);
23
23
  if (needDelete) {
24
24
  const package = getPkgJson();
25
25
  const groupName = getGroupName();
26
26
 
27
- let type = conf.newWordsFileType || "excel";
28
- type = type === "json" ? "json" : "xlsx";
27
+ let type = conf.newWordsFileType || 'excel';
28
+ type = type === 'json' ? 'json' : 'xlsx';
29
29
 
30
30
  const version = package.version;
31
31
  const fileName = `${groupName}_${version}.${type}`;
32
32
  file.delete(fileName);
33
- file.delete("relationKey.json");
34
- log.success("已删除历史文件:" + fileName + "和relationKey.json");
33
+ file.delete('relationKey.json');
34
+ log.success('已删除历史文件:' + fileName + '和relationKey.json');
35
35
  }
36
36
 
37
- log.success("★★★ 脚本执行成功 ★★★");
37
+ log.success('★★★ 脚本执行成功 ★★★');
38
38
  }
39
39
 
40
40
  // update();
@@ -1,10 +1,11 @@
1
- const service = require("../service");
2
- const file = require("./file");
3
- const log = require("./log");
4
- const getGroupName = require("./getGroupName");
5
- const getPkgJson = require("./getPkgJson");
1
+ const service = require('../service');
2
+ const file = require('./file');
3
+ const log = require('./log');
4
+ const getGroupName = require('./getGroupName');
5
+ const getPkgJson = require('./getPkgJson');
6
+ const path = require('path');
6
7
 
7
- module.exports = async function downloadJson(downloadIds) {
8
+ module.exports = async function downloadJson(downloadIds, conf) {
8
9
  // 读取groupName
9
10
  const groupName = getGroupName();
10
11
  const package = getPkgJson();
@@ -22,6 +23,8 @@ module.exports = async function downloadJson(downloadIds) {
22
23
  const data = res.data || {};
23
24
  Object.keys(data).forEach((key) => {
24
25
  const item = data[key];
25
- file.write(`locales/${key}.json`, JSON.stringify(item));
26
+
27
+ const localeOutput = path.resolve(conf.localeOutput, `locales/${key}.json`);
28
+ file.write(localeOutput, JSON.stringify(item));
26
29
  });
27
30
  };
@@ -0,0 +1,23 @@
1
+ const file = require("./file");
2
+ const log = require("./log");
3
+ const getGroupName = require("./getGroupName");
4
+ const getPkgJson = require("./getPkgJson");
5
+
6
+ module.exports = async function genXlsx(words) {
7
+ const package = getPkgJson();
8
+ // 读取groupName
9
+ const groupName = getGroupName();
10
+ const fileName = `${groupName}_${package.version}.xlsx`;
11
+ // 写入xlsx
12
+ // 把新增的词条对象,转为数组
13
+ const newWordsArr = Object.entries(words).map(([id, word]) => ({
14
+ id,
15
+ ...word,
16
+ }));
17
+ // 合并数据,并写入文件
18
+ console.log('%c [ newWordsArr ]-14', 'font-size:13px; background:pink; color:#bf2c9f;', newWordsArr)
19
+
20
+ file.writeXlsx(fileName, { Sheet1: newWordsArr });
21
+
22
+ log.info(`全部词条请查看: ${fileName}中`);
23
+ };
@@ -0,0 +1,22 @@
1
+ const service = require('../service');
2
+ const log = require('./log');
3
+ const getGroupName = require('./getGroupName');
4
+ const getPkgJson = require('./getPkgJson');
5
+
6
+ module.exports = async function getWordsByIds(downloadIds) {
7
+ // 读取groupName
8
+ const groupName = getGroupName();
9
+ const package = getPkgJson();
10
+
11
+ const res = await service.getJson({
12
+ version: package.version,
13
+ groupName,
14
+ downloadIds,
15
+ });
16
+ if (res.code !== 10000) {
17
+ log.error(res.msg);
18
+ process.exit(1);
19
+ }
20
+
21
+ return res.data || {};
22
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaoswise/intl",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "author": "cloudwiser",
5
5
  "description": "intl",
6
6
  "main": "lib/index.js",
@@ -84,5 +84,5 @@
84
84
  "react-dom": "^16.13.1"
85
85
  },
86
86
  "license": "MIT",
87
- "gitHead": "6bea40aa64b55508f7efbeed5a97719232bd0ff0"
87
+ "gitHead": "39a7b110a359f24658c1b44af5d8867207a76ce4"
88
88
  }