@chaoswise/intl 1.2.1 → 2.0.0
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.
- package/bin/scripts/collect.js +93 -45
- package/bin/scripts/service/index.js +13 -8
- package/bin/scripts/update.js +25 -17
- package/bin/scripts/util/downloadJson.js +17 -8
- package/bin/scripts/util/getGroupName.js +4 -4
- package/bin/scripts/util/getPkgJson.js +6 -0
- package/bin/scripts/util/getTargetFiles.js +33 -15
- package/bin/scripts/util/transformAst.js +50 -44
- package/bin/scripts/util/writeNewWordsFile.js +12 -9
- package/lib/useIntl/context.js +1 -2
- package/lib/useIntl/index.js +1 -2
- package/package.json +3 -2
package/bin/scripts/collect.js
CHANGED
|
@@ -1,96 +1,144 @@
|
|
|
1
|
-
const { v4: uuidv4 } = require(
|
|
1
|
+
const { v4: uuidv4 } = require("uuid");
|
|
2
2
|
|
|
3
|
-
const getConf = require(
|
|
4
|
-
const
|
|
5
|
-
const transformAst = require(
|
|
6
|
-
const log = require(
|
|
7
|
-
const file = require(
|
|
8
|
-
const writeNewWordsFile = require(
|
|
9
|
-
const { readWordJson } = require(
|
|
10
|
-
const getGroupName = require(
|
|
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");
|
|
11
12
|
|
|
12
|
-
const service = require(
|
|
13
|
+
const service = require("./service");
|
|
13
14
|
|
|
14
15
|
async function collect() {
|
|
16
|
+
log.info("工程扫描中...");
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
const package = getPkgJson();
|
|
18
19
|
const conf = getConf();
|
|
19
20
|
const groupName = getGroupName();
|
|
20
|
-
const newWordsFileType = conf.newWordsFileType ||
|
|
21
|
+
const newWordsFileType = conf.newWordsFileType || "excel";
|
|
21
22
|
|
|
22
23
|
// 检查newWordsFileType
|
|
23
|
-
if (![
|
|
24
|
+
if (!["json", "excel"].includes(newWordsFileType)) {
|
|
24
25
|
log.error(`配置:newWordsFileType 只能为json或者excel`);
|
|
25
26
|
process.exit(1);
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
// 读取wordJson
|
|
29
30
|
if (conf.localWordPath && !readWordJson(conf.localWordPath)) {
|
|
30
|
-
log.error(
|
|
31
|
+
log.error("localWordPath配置有误");
|
|
31
32
|
process.exit(1);
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
// 获取需要遍历的文件
|
|
35
|
-
const files =
|
|
36
|
+
const files = targetEntryFiles(conf);
|
|
36
37
|
|
|
37
38
|
// 遍历文件,并对代码进行ast处理的方法:transformAst(type, files, conf, replaceWords)
|
|
38
39
|
// 第一次遍历,只收集中文,不传入replaceWords
|
|
39
|
-
const info = transformAst(
|
|
40
|
+
const info = transformAst("Collect", files, conf);
|
|
40
41
|
|
|
41
42
|
// 在国际化平台中获取所有已存在中文信息
|
|
42
43
|
const res = await service.searchByZh(info.allWords);
|
|
43
|
-
// 在国际化平台记录特殊方法
|
|
44
|
-
await service.saveMethod({ groupName, specialMethod: info.specialMethod });
|
|
45
|
-
|
|
46
44
|
if (res.code !== 10000) {
|
|
47
|
-
log.error(
|
|
45
|
+
log.error("请求数据出错:" + res.msg);
|
|
48
46
|
process.exit(1);
|
|
49
47
|
}
|
|
50
|
-
|
|
51
48
|
const wordsMap = res.data;
|
|
49
|
+
// 获取所有语种
|
|
50
|
+
const languageRes = await service.getLanguage();
|
|
51
|
+
if (languageRes.code !== 10000) {
|
|
52
|
+
log.error("请求数据出错:" + languageRes.msg);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
const languages = languageRes.data;
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
// 在国际化平台记录特殊方法
|
|
58
|
+
await service.saveMethod({
|
|
59
|
+
version: package.version,
|
|
60
|
+
groupName,
|
|
61
|
+
specialMethod: info.specialMethod,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const replaceWords = {}; // { zh: key } 需要在代码中替换的词条id
|
|
65
|
+
const newWords = {}; // { zh: '中文1', en: '请选择:en1/en2' }
|
|
55
66
|
const exist = {}; // 根据中文只查出一条数据的已有词条
|
|
56
67
|
Object.entries(wordsMap).forEach(([word, value]) => {
|
|
57
68
|
// 如果当前中文词条在平台中存在,且对应英文翻译有且只有一条,那么直接替换代码中的中文为当前id,否则生成临时key,让开发者手动确认
|
|
58
69
|
if (value.length === 1) {
|
|
59
|
-
const
|
|
60
|
-
replaceWords[word] = key;
|
|
61
|
-
|
|
70
|
+
const currentDt = value[0];
|
|
71
|
+
replaceWords[word] = currentDt.key;
|
|
72
|
+
|
|
73
|
+
// 判断新增语种的情况,比如语种新增一个zh_TW,那么需要新的翻译
|
|
74
|
+
const currentCopy = { ...currentDt };
|
|
75
|
+
delete currentCopy.key;
|
|
76
|
+
if (Object.keys(currentCopy).length < languages.length) {
|
|
77
|
+
newWords[currentDt.key] = {};
|
|
78
|
+
languages.forEach((dt) => {
|
|
79
|
+
newWords[currentDt.key][dt.key] = currentCopy[dt.key] || "待翻译";
|
|
80
|
+
});
|
|
81
|
+
} else {
|
|
82
|
+
exist[currentDt.key] = currentDt;
|
|
83
|
+
}
|
|
62
84
|
} else {
|
|
63
|
-
const
|
|
64
|
-
replaceWords[word] =
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
85
|
+
const key = uuidv4();
|
|
86
|
+
replaceWords[word] = key;
|
|
87
|
+
// 处理需要翻译的词条
|
|
88
|
+
newWords[key] = {};
|
|
89
|
+
languages.forEach((dt) => {
|
|
90
|
+
// 主语言直接赋值word
|
|
91
|
+
if (dt.isMain) {
|
|
92
|
+
newWords[key][dt.key] = word;
|
|
93
|
+
} else {
|
|
94
|
+
let hasValue = [];
|
|
95
|
+
value.forEach((item) => {
|
|
96
|
+
if (item[dt.key] && !hasValue.includes(item[dt.key])) {
|
|
97
|
+
hasValue.push(item[dt.key]);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
// 词条不存在
|
|
101
|
+
if (value.length === 0) {
|
|
102
|
+
newWords[key][dt.key] = "待翻译";
|
|
103
|
+
}
|
|
104
|
+
// {"zh_CN": "你好a","zh_TW": "","en_US": "hello"},{"zh_CN": "你好a","en_US": "hello"}
|
|
105
|
+
// 处理en_US在这两个数据中翻译相同的情况,比如 {en_US: hello}
|
|
106
|
+
else if (hasValue.length === 1) {
|
|
107
|
+
newWords[key][dt.key] = hasValue[0];
|
|
108
|
+
}
|
|
109
|
+
// 比如:zh_TW: ' ' / undefined
|
|
110
|
+
else if (hasValue.length > 1) {
|
|
111
|
+
newWords[key][
|
|
112
|
+
dt.key
|
|
113
|
+
] = `该词条在平台中对应多个翻译,请手动确认:${hasValue.join("/")}`;
|
|
114
|
+
}
|
|
115
|
+
// 词条存在但是该语种未翻译,比如zh_TW在上面数据中都未翻译的情况
|
|
116
|
+
else {
|
|
117
|
+
newWords[key][dt.key] = "待翻译";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
74
121
|
}
|
|
75
122
|
});
|
|
76
123
|
|
|
77
|
-
|
|
78
124
|
// 在国际化平台中保存所有已存在词条
|
|
79
|
-
await service.saveExist({groupName, exist});
|
|
125
|
+
await service.saveExist({ version: package.version, groupName, exist });
|
|
80
126
|
|
|
81
127
|
// 第二次遍历,传入replaceWords,对代码进行国际化通用API转化,把词条替换成数据库的id,或者脚本临时生成的uuid
|
|
82
|
-
transformAst(
|
|
128
|
+
transformAst("collect", files, conf, replaceWords);
|
|
83
129
|
|
|
84
|
-
log.success(
|
|
85
|
-
log.info(
|
|
130
|
+
log.success("★★★ 脚本执行成功 ★★★");
|
|
131
|
+
log.info("国际化配置文件: .intlconfig.js,可根据需求自定义修改");
|
|
86
132
|
// 本次扫描出新的待翻译词条,写入到newWords中
|
|
87
133
|
if (Object.keys(newWords).length) {
|
|
88
|
-
writeNewWordsFile(newWordsFileType, newWords);
|
|
134
|
+
writeNewWordsFile(newWordsFileType, newWords, package.version);
|
|
89
135
|
}
|
|
90
136
|
|
|
91
137
|
const warnLogs = log.logs.warn;
|
|
92
138
|
if (warnLogs.length) {
|
|
93
|
-
const fileName = `intl.logs.warn.${
|
|
139
|
+
const fileName = `intl.logs.warn.${new Date()
|
|
140
|
+
.toLocaleString()
|
|
141
|
+
.replace(/[\/ ]/g, "_")}.txt`;
|
|
94
142
|
file.write(fileName, JSON.stringify(warnLogs, null, 2));
|
|
95
143
|
log.warn(`存在脚本无法处理的情况,具体查看 ${fileName} 手动处理`);
|
|
96
144
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
const axios = require(
|
|
2
|
-
const getConf = require(
|
|
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 ||
|
|
6
|
+
axios.defaults.baseURL = (conf.baseURL || "") + "/api/i18n";
|
|
7
7
|
|
|
8
8
|
axios.interceptors.response.use(
|
|
9
9
|
function (response) {
|
|
@@ -20,20 +20,25 @@ axios.interceptors.response.use(
|
|
|
20
20
|
|
|
21
21
|
// 在国际化平台中获取所有已存在中文信息
|
|
22
22
|
exports.searchByZh = (zhs) => {
|
|
23
|
-
return axios.post(
|
|
23
|
+
return axios.post("/content/getContentsByMain", zhs);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// 查询所有语种
|
|
27
|
+
exports.getLanguage = () => {
|
|
28
|
+
return axios.get("/language/list");
|
|
24
29
|
};
|
|
25
30
|
|
|
26
31
|
// 根据词条id获取所有数据库词条信息
|
|
27
|
-
exports.getJson = (
|
|
28
|
-
return axios.post(
|
|
32
|
+
exports.getJson = (data) => {
|
|
33
|
+
return axios.post("/content/downloadContentByKeys", data);
|
|
29
34
|
};
|
|
30
35
|
|
|
31
36
|
// 保存替换的特殊方法
|
|
32
37
|
exports.saveMethod = (data) => {
|
|
33
|
-
return axios.post(
|
|
38
|
+
return axios.post("/history/method/save", data);
|
|
34
39
|
};
|
|
35
40
|
|
|
36
41
|
// 保存已有词条
|
|
37
42
|
exports.saveExist = (data) => {
|
|
38
|
-
return axios.post(
|
|
43
|
+
return axios.post("/history/exist/save", data);
|
|
39
44
|
};
|
package/bin/scripts/update.js
CHANGED
|
@@ -1,33 +1,41 @@
|
|
|
1
|
-
const getConf = require(
|
|
2
|
-
const
|
|
3
|
-
const transformAst = require(
|
|
4
|
-
const downloadJson = require(
|
|
5
|
-
const log = require(
|
|
6
|
-
const file = require(
|
|
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");
|
|
7
9
|
|
|
8
10
|
async function update() {
|
|
9
|
-
log.info(
|
|
11
|
+
log.info("词条更新中...");
|
|
10
12
|
const conf = getConf();
|
|
11
|
-
const files =
|
|
13
|
+
const files = targetOutputFiles(conf);
|
|
12
14
|
|
|
13
15
|
// relationKey { key: id } key是脚本生成的临时id,id是数据库对应词条的id
|
|
14
|
-
const relationKey = file.readJson(
|
|
16
|
+
const relationKey = file.readJson("relationKey.json") || {};
|
|
15
17
|
|
|
16
|
-
const info = transformAst(
|
|
17
|
-
|
|
18
|
+
const info = transformAst("update", files, conf, relationKey);
|
|
19
|
+
const _downloadIds = [...new Set(info.downloadIds.filter(Boolean))];
|
|
20
|
+
await downloadJson(_downloadIds);
|
|
18
21
|
|
|
19
22
|
const needDelete = Boolean(Object.keys(relationKey).length);
|
|
20
23
|
if (needDelete) {
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
const package = getPkgJson();
|
|
25
|
+
const groupName = getGroupName();
|
|
23
26
|
|
|
24
|
-
|
|
27
|
+
let type = conf.newWordsFileType || "excel";
|
|
28
|
+
type = type === "json" ? "json" : "xlsx";
|
|
29
|
+
|
|
30
|
+
const version = package.version;
|
|
31
|
+
const fileName = `${groupName}_${version}.${type}`;
|
|
32
|
+
file.delete(fileName);
|
|
33
|
+
file.delete("relationKey.json");
|
|
34
|
+
log.success("已删除历史文件:" + fileName + "和relationKey.json");
|
|
25
35
|
}
|
|
26
36
|
|
|
27
|
-
log.success(
|
|
37
|
+
log.success("★★★ 脚本执行成功 ★★★");
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
// update();
|
|
31
41
|
module.exports = update;
|
|
32
|
-
|
|
33
|
-
|
|
@@ -1,18 +1,27 @@
|
|
|
1
|
-
const service = require(
|
|
2
|
-
const file = require(
|
|
3
|
-
const log = require(
|
|
4
|
-
const getGroupName = require(
|
|
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");
|
|
5
6
|
|
|
6
7
|
module.exports = async function downloadJson(downloadIds) {
|
|
7
8
|
// 读取groupName
|
|
8
9
|
const groupName = getGroupName();
|
|
10
|
+
const package = getPkgJson();
|
|
9
11
|
|
|
10
|
-
const res = await service.getJson({
|
|
12
|
+
const res = await service.getJson({
|
|
13
|
+
version: package.version,
|
|
14
|
+
groupName,
|
|
15
|
+
downloadIds,
|
|
16
|
+
});
|
|
11
17
|
if (res.code !== 10000) {
|
|
12
18
|
log.error(res.msg);
|
|
13
19
|
process.exit(1);
|
|
14
20
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
|
|
22
|
+
const data = res.data || {};
|
|
23
|
+
Object.keys(data).forEach((key) => {
|
|
24
|
+
const item = data[key];
|
|
25
|
+
file.write(`locales/${key}.json`, JSON.stringify(item));
|
|
26
|
+
});
|
|
18
27
|
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
const
|
|
1
|
+
const log = require("./log");
|
|
2
|
+
const getConf = require("../conf");
|
|
3
|
+
const getPkgJson = require("./getPkgJson");
|
|
4
4
|
|
|
5
5
|
module.exports = function getGroupName() {
|
|
6
6
|
const conf = getConf();
|
|
7
7
|
// 读取packageName
|
|
8
|
-
const package =
|
|
8
|
+
const package = getPkgJson();
|
|
9
9
|
const groupName = package?.[conf.groupNameKey];
|
|
10
10
|
if (!groupName) {
|
|
11
11
|
log.error(`package.json中,groupName不能为空`);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const path = require(
|
|
2
|
-
const glob = require(
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const glob = require("glob");
|
|
3
3
|
|
|
4
4
|
function getSourceFiles({ entry, exclude }) {
|
|
5
5
|
return glob.sync(`${entry}/**/*.{js,ts,tsx,jsx}`, {
|
|
@@ -7,19 +7,37 @@ function getSourceFiles({ entry, exclude }) {
|
|
|
7
7
|
});
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
module.exports = {
|
|
11
|
+
// 根据入口entry,获取所有需要扫描的文件
|
|
12
|
+
targetEntryFiles: function ({ entry, output, exclude }) {
|
|
13
|
+
const outputs = output ? [].concat(output) : [];
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
const targetFiles = [].concat(entry).reduce((prev, cur, index) => {
|
|
16
|
+
const files = getSourceFiles({ entry: cur, exclude }).map((file) => ({
|
|
17
|
+
filePath: file,
|
|
18
|
+
currentEntry: cur,
|
|
19
|
+
currentOutput: outputs[index] || outputs[0],
|
|
20
|
+
ext: path.extname(file),
|
|
21
|
+
}));
|
|
22
|
+
return prev.concat(files);
|
|
23
|
+
}, []);
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
return targetFiles;
|
|
26
|
+
},
|
|
27
|
+
// 根据output,获取所有需要扫描的文件
|
|
28
|
+
targetOutputFiles: function ({ output, exclude }) {
|
|
29
|
+
const outputs = output ? [].concat(output) : [];
|
|
30
|
+
|
|
31
|
+
const targetFiles = outputs.reduce((prev, cur, index) => {
|
|
32
|
+
const files = getSourceFiles({ entry: cur, exclude }).map((file) => ({
|
|
33
|
+
filePath: file,
|
|
34
|
+
currentEntry: cur,
|
|
35
|
+
currentOutput: outputs[index] || outputs[0],
|
|
36
|
+
ext: path.extname(file),
|
|
37
|
+
}));
|
|
38
|
+
return prev.concat(files);
|
|
39
|
+
}, []);
|
|
40
|
+
|
|
41
|
+
return targetFiles;
|
|
42
|
+
},
|
|
25
43
|
};
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
const fs = require(
|
|
2
|
-
const path = require(
|
|
3
|
-
const prettier = require(
|
|
4
|
-
const babel = require(
|
|
5
|
-
const generate = require(
|
|
6
|
-
const traverse = require(
|
|
7
|
-
const pluginSyntaxJSX = require(
|
|
8
|
-
const pluginSyntaxProposalOptionalChaining = require(
|
|
9
|
-
const pluginSyntaxClassProperties = require(
|
|
10
|
-
const pluginSyntaxDecorators = require(
|
|
11
|
-
const pluginSyntaxObjectRestSpread = require(
|
|
12
|
-
const pluginSyntaxAsyncGenerators = require(
|
|
13
|
-
const pluginSyntaxDoExpressions = require(
|
|
14
|
-
const pluginSyntaxDynamicImport = require(
|
|
15
|
-
const pluginSyntaxFunctionBind = require(
|
|
16
|
-
const pluginExportDefaultFrom = require(
|
|
17
|
-
const presetTypescript = require(
|
|
18
|
-
|
|
19
|
-
const makeVisitorCollect = require(
|
|
20
|
-
const makeVisitorUpdate = require(
|
|
21
|
-
const log = require(
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const prettier = require("prettier");
|
|
4
|
+
const babel = require("@babel/core");
|
|
5
|
+
const generate = require("@babel/generator").default;
|
|
6
|
+
const traverse = require("@babel/traverse").default;
|
|
7
|
+
const pluginSyntaxJSX = require("@babel/plugin-syntax-jsx");
|
|
8
|
+
const pluginSyntaxProposalOptionalChaining = require("@babel/plugin-proposal-optional-chaining");
|
|
9
|
+
const pluginSyntaxClassProperties = require("@babel/plugin-syntax-class-properties");
|
|
10
|
+
const pluginSyntaxDecorators = require("@babel/plugin-syntax-decorators");
|
|
11
|
+
const pluginSyntaxObjectRestSpread = require("@babel/plugin-syntax-object-rest-spread");
|
|
12
|
+
const pluginSyntaxAsyncGenerators = require("@babel/plugin-syntax-async-generators");
|
|
13
|
+
const pluginSyntaxDoExpressions = require("@babel/plugin-syntax-do-expressions");
|
|
14
|
+
const pluginSyntaxDynamicImport = require("@babel/plugin-syntax-dynamic-import");
|
|
15
|
+
const pluginSyntaxFunctionBind = require("@babel/plugin-syntax-function-bind");
|
|
16
|
+
const pluginExportDefaultFrom = require("@babel/plugin-proposal-export-default-from");
|
|
17
|
+
const presetTypescript = require("@babel/preset-typescript").default;
|
|
18
|
+
|
|
19
|
+
const makeVisitorCollect = require("./makeVisitorCollect");
|
|
20
|
+
const makeVisitorUpdate = require("./makeVisitorUpdate");
|
|
21
|
+
const log = require("./log");
|
|
22
22
|
|
|
23
23
|
// 获取文件中需要忽略转化通用国际化API规范的所有行号
|
|
24
24
|
function getIgnoreLines(ast) {
|
|
@@ -28,21 +28,21 @@ function getIgnoreLines(ast) {
|
|
|
28
28
|
const last = ignoreBlocks.length - 1;
|
|
29
29
|
|
|
30
30
|
// 单行注释,比如 //
|
|
31
|
-
if (type ===
|
|
31
|
+
if (type === "CommentLine" && value.trim() === "cw-i18n-disable-line") {
|
|
32
32
|
ignoreBlocks.push({
|
|
33
33
|
start: loc.start.line,
|
|
34
34
|
end: loc.start.line,
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
// 多行注释,比如 /* */
|
|
38
|
-
else if (type ===
|
|
38
|
+
else if (type === "CommentBlock" && value.trim() === "cw-i18n-disable") {
|
|
39
39
|
// 处理 di18n-disable 关闭 di18n-enable又开启的情况
|
|
40
40
|
if (last < 0 || ignoreBlocks[last].end) {
|
|
41
41
|
ignoreBlocks.push({
|
|
42
42
|
start: loc.start.line,
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
|
-
} else if (type ===
|
|
45
|
+
} else if (type === "CommentBlock" && value.trim() === "cw-i18n-enable") {
|
|
46
46
|
// 处理 di18n-disable 关闭 di18n-enable又开启的情况
|
|
47
47
|
if (last >= 0 && !ignoreBlocks[last].end) {
|
|
48
48
|
ignoreBlocks[last].end = loc.start.line;
|
|
@@ -85,15 +85,12 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
85
85
|
|
|
86
86
|
// babel配置
|
|
87
87
|
const transformOptions = {
|
|
88
|
-
sourceType:
|
|
88
|
+
sourceType: "module",
|
|
89
89
|
ast: true,
|
|
90
90
|
configFile: false,
|
|
91
91
|
presets: [
|
|
92
92
|
...babelPresets,
|
|
93
|
-
[
|
|
94
|
-
presetTypescript,
|
|
95
|
-
{ isTSX: true, allExtensions: true },
|
|
96
|
-
]
|
|
93
|
+
[presetTypescript, { isTSX: true, allExtensions: true }],
|
|
97
94
|
],
|
|
98
95
|
plugins: [
|
|
99
96
|
pluginSyntaxJSX,
|
|
@@ -115,8 +112,8 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
115
112
|
const { filePath } = file;
|
|
116
113
|
|
|
117
114
|
if (
|
|
118
|
-
specialFileReg.some(item => {
|
|
119
|
-
if (typeof item ===
|
|
115
|
+
specialFileReg.some((item) => {
|
|
116
|
+
if (typeof item === "string") {
|
|
120
117
|
return item === filePath;
|
|
121
118
|
} else {
|
|
122
119
|
return item.test(filePath);
|
|
@@ -125,7 +122,7 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
125
122
|
) {
|
|
126
123
|
file.special = true;
|
|
127
124
|
}
|
|
128
|
-
const isTSX = [
|
|
125
|
+
const isTSX = [".ts", ".tsx"].includes(path.extname(filePath));
|
|
129
126
|
|
|
130
127
|
const r = {
|
|
131
128
|
allWords,
|
|
@@ -135,30 +132,32 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
135
132
|
hasImport: false, // 是否已经引入通用国际化API,import {init} from ...;
|
|
136
133
|
hasTouch: false, // 是否存在需要替换的词条
|
|
137
134
|
};
|
|
138
|
-
const sourceCode = fs.readFileSync(filePath,
|
|
139
|
-
|
|
135
|
+
const sourceCode = fs.readFileSync(filePath, "utf8");
|
|
136
|
+
|
|
140
137
|
let ast;
|
|
141
138
|
try {
|
|
142
139
|
ast = babel.parseSync(sourceCode, transformOptions);
|
|
143
140
|
} catch (error) {
|
|
144
|
-
replaceWords &&
|
|
145
|
-
|
|
141
|
+
replaceWords &&
|
|
142
|
+
log.error(`文件解析出错:${file.filePath}
|
|
143
|
+
${error}`);
|
|
146
144
|
return;
|
|
147
145
|
}
|
|
148
146
|
|
|
149
147
|
opts.ignoreLines = getIgnoreLines(ast);
|
|
150
148
|
|
|
151
149
|
let makeVisitor = makeVisitorCollect;
|
|
152
|
-
if (type ===
|
|
153
|
-
makeVisitor = makeVisitorUpdate
|
|
150
|
+
if (type === "update") {
|
|
151
|
+
makeVisitor = makeVisitorUpdate;
|
|
154
152
|
}
|
|
155
153
|
|
|
156
154
|
try {
|
|
157
155
|
const visitor = makeVisitor(opts, r, file);
|
|
158
156
|
traverse(ast, visitor);
|
|
159
157
|
} catch (e) {
|
|
160
|
-
replaceWords &&
|
|
161
|
-
|
|
158
|
+
replaceWords &&
|
|
159
|
+
log.error(`文件解析出错:${file.filePath}
|
|
160
|
+
${e}`);
|
|
162
161
|
return;
|
|
163
162
|
}
|
|
164
163
|
|
|
@@ -174,19 +173,26 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
174
173
|
|
|
175
174
|
if (!r.hasTouch) {
|
|
176
175
|
code = sourceCode;
|
|
177
|
-
} else if (
|
|
176
|
+
} else if (
|
|
177
|
+
!r.hasImport &&
|
|
178
|
+
!file.special &&
|
|
179
|
+
importCode &&
|
|
180
|
+
Object.keys(replaceWords).length
|
|
181
|
+
) {
|
|
178
182
|
code = `${importCode}\n${code}`;
|
|
179
183
|
}
|
|
180
184
|
|
|
181
185
|
// 自定义格式化代码
|
|
182
186
|
if (r.hasTouch) {
|
|
183
187
|
if (conf.prettier) {
|
|
184
|
-
const parser = isTSX ?
|
|
188
|
+
const parser = isTSX ? "typescript" : "babel";
|
|
185
189
|
code = prettier.format(code, { ...conf.prettier, parser });
|
|
186
190
|
}
|
|
187
191
|
|
|
188
|
-
const target =
|
|
189
|
-
|
|
192
|
+
const target = file.currentOutput
|
|
193
|
+
? filePath.replace(file.currentEntry, file.currentOutput)
|
|
194
|
+
: filePath;
|
|
195
|
+
fs.writeFileSync(target, code, { encoding: "utf-8" });
|
|
190
196
|
}
|
|
191
197
|
});
|
|
192
198
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
const file = require(
|
|
2
|
-
const log = require(
|
|
3
|
-
const getGroupName = require(
|
|
1
|
+
const file = require("./file");
|
|
2
|
+
const log = require("./log");
|
|
3
|
+
const getGroupName = require("./getGroupName");
|
|
4
4
|
|
|
5
|
-
module.exports = async function writeNewWordsFile(type, newWords) {
|
|
6
|
-
const isJson = type ===
|
|
7
|
-
type = isJson?
|
|
5
|
+
module.exports = async function writeNewWordsFile(type, newWords, version) {
|
|
6
|
+
const isJson = type === "json";
|
|
7
|
+
type = isJson ? "json" : "xlsx";
|
|
8
8
|
// 读取groupName
|
|
9
9
|
const groupName = getGroupName();
|
|
10
|
-
const fileName = `${groupName}.${type}
|
|
10
|
+
const fileName = `${groupName}_${version}.${type}`;
|
|
11
11
|
|
|
12
12
|
// 写入json
|
|
13
13
|
if (isJson) {
|
|
@@ -21,9 +21,12 @@ module.exports = async function writeNewWordsFile(type, newWords) {
|
|
|
21
21
|
// 写入xlsx
|
|
22
22
|
const oldNewWords = file.readElsx(fileName)?.Sheet1 || [];
|
|
23
23
|
// 把新增的词条对象,转为数组
|
|
24
|
-
const newWordsArr = Object.entries(newWords).map(([id, word]) => ({
|
|
24
|
+
const newWordsArr = Object.entries(newWords).map(([id, word]) => ({
|
|
25
|
+
id,
|
|
26
|
+
...word,
|
|
27
|
+
}));
|
|
25
28
|
// 合并数据,并写入文件
|
|
26
|
-
file.writeXlsx(fileName, { Sheet1: [...oldNewWords, ...newWordsArr
|
|
29
|
+
file.writeXlsx(fileName, { Sheet1: [...oldNewWords, ...newWordsArr] });
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
log.info(`待翻译词条请查看: ${fileName}中`);
|
package/lib/useIntl/context.js
CHANGED
package/lib/useIntl/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chaoswise/intl",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"author": "cloudwiser",
|
|
5
5
|
"description": "intl",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
"@babel/plugin-transform-destructuring": "^7.17.7",
|
|
54
54
|
"@babel/plugin-transform-for-of": "^7.16.7",
|
|
55
55
|
"@babel/plugin-transform-parameters": "^7.16.7",
|
|
56
|
+
"@babel/plugin-transform-runtime": "^7.11.0",
|
|
56
57
|
"@babel/plugin-transform-shorthand-properties": "^7.16.7",
|
|
57
58
|
"@babel/plugin-transform-spread": "^7.16.7",
|
|
58
59
|
"@babel/preset-env": "^7.16.11",
|
|
@@ -83,5 +84,5 @@
|
|
|
83
84
|
"react-dom": "^16.13.1"
|
|
84
85
|
},
|
|
85
86
|
"license": "MIT",
|
|
86
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "6bea40aa64b55508f7efbeed5a97719232bd0ff0"
|
|
87
88
|
}
|