ztxkutils 10.0.8 → 20.0.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.
- package/dist/businessTools.js +4 -3
- package/dist/dataModel-6c68c88f.js +26 -0
- package/dist/dataModel-914b6226.js +26 -0
- package/dist/dataModel.js +2 -1
- package/dist/fileOperation.d.ts +19 -0
- package/dist/fileOperation.js +74 -10
- package/dist/hooks.js +36 -22
- package/dist/i18next.js +21 -0
- package/dist/index.js +5 -4
- package/dist/myIndexDb.d.ts +1 -1
- package/dist/print.d.ts +4 -5
- package/dist/print.js +4 -3
- package/dist/reqUrl.js +1 -1
- package/dist/request-1e442d5d.js +2982 -0
- package/dist/request-986d7090.js +2923 -0
- package/dist/request-d8d72b87.js +2982 -0
- package/dist/request-f600ad7a.js +2992 -0
- package/dist/request.d.ts +13 -0
- package/dist/request.js +2 -1
- package/dist/stompClient.js +21 -8
- package/dist/useFileIdToBase64.js +2 -0
- package/dist/validate-21164759.js +260 -0
- package/dist/validate-2de5a28f.js +260 -0
- package/dist/validate.js +2 -1
- package/dist/workflow.js +3 -5
- package/locales/en-US.json +64 -0
- package/locales/zh-CN.json +64 -0
- package/package.json +41 -4
- package/zti18n-cli/bin/index.js +3 -0
- package/zti18n-cli/index.js +23 -0
- package/zti18n-cli/src/command/collect.js +353 -0
- package/zti18n-cli/src/command/convert.js +17 -0
- package/zti18n-cli/src/command/convert2.js +35 -0
- package/zti18n-cli/src/command/initFileConf.js +133 -0
- package/zti18n-cli/src/command/publish.js +24 -0
- package/zti18n-cli/src/conf/BaseConf.js +21 -0
- package/zti18n-cli/src/conf/FileConf.js +116 -0
- package/zti18n-cli/src/index.js +75 -0
- package/zti18n-cli/src/translate/google.js +87 -0
- package/zti18n-cli/src/utils/isChinese.js +3 -0
- package/zti18n-cli/src/utils/log.js +8 -0
- package/zti18n-cli/src/utils/mergeOptions.js +45 -0
- package/zti18n-cli/src/utils/reactOptions.js +73 -0
- package/zti18n-cli/src/utils/vueOptions.js +69 -0
- package/zti18n-core/index.js +1 -0
- package/zti18n-core/src/index.js +5 -0
- package/zti18n-core/src/plugin/reactIntlToReactIntlUniversal.js +224 -0
- package/zti18n-core/src/plugin/reactIntlUniversalToDi18n.js +64 -0
- package/zti18n-core/src/transform/defaultPkMap.js +79 -0
- package/zti18n-core/src/transform/transformHtml.js +271 -0
- package/zti18n-core/src/transform/transformJs.js +489 -0
- package/zti18n-core/src/transform/transformPug.js +272 -0
- package/zti18n-core/src/transform/transformReactIntlToReactIntlUniversal.js +96 -0
- package/zti18n-core/src/transform/transformReactIntlUniveralToDi18n.js +90 -0
- package/zti18n-core/src/transform/transformToDi18n.js +22 -0
- package/zti18n-core/src/transform/transformTs.js +41 -0
- package/zti18n-core/src/transform/transformVue.js +126 -0
- package/zti18n-core/src/transform/transformZeroToDi18n.js +105 -0
- package/zti18n-core/src/translate/google.js +6 -0
- package/zti18n-core/src/utils/constants.js +3 -0
- package/zti18n-core/src/utils/getIgnoreLines.js +14 -0
- package/zti18n-core/src/utils/isChinese.js +3 -0
- package/zti18n-core/src/utils/log.js +8 -0
@@ -0,0 +1,272 @@
|
|
1
|
+
const lex = require('pug-lexer');
|
2
|
+
const parse = require('pug-parser');
|
3
|
+
const genSource = require('pug-source-gen');
|
4
|
+
const mustache = require('mustache');
|
5
|
+
const prettier = require('prettier');
|
6
|
+
const transformJs = require('./transformJs');
|
7
|
+
const getIgnoreLines = require('../utils/getIgnoreLines');
|
8
|
+
|
9
|
+
/* eslint-disable */
|
10
|
+
// override to avoid unneeded escape: pug-source-gen/lib/code-generator.js
|
11
|
+
genSource.CodeGenerator.prototype.attrs = function (attrs) {
|
12
|
+
var regularAttrs = [];
|
13
|
+
var classes = '';
|
14
|
+
var id;
|
15
|
+
attrs.forEach(
|
16
|
+
function (attr) {
|
17
|
+
var constVal = '';
|
18
|
+
try {
|
19
|
+
constVal = constantinople.toConstant(attr.val);
|
20
|
+
} catch (ex) {}
|
21
|
+
|
22
|
+
if (
|
23
|
+
attr.name === 'class' &&
|
24
|
+
!attr.escaped &&
|
25
|
+
constVal &&
|
26
|
+
/^\-?[_a-z][_a-z0-9\-]*$/i.test(constVal)
|
27
|
+
) {
|
28
|
+
classes += '.' + constVal;
|
29
|
+
} else if (
|
30
|
+
attr.name === 'id' &&
|
31
|
+
!id &&
|
32
|
+
!attr.escaped &&
|
33
|
+
constVal &&
|
34
|
+
/^[\w-]+$/.test(constVal)
|
35
|
+
) {
|
36
|
+
id = constVal;
|
37
|
+
} else {
|
38
|
+
var attrOut = '';
|
39
|
+
|
40
|
+
// name
|
41
|
+
if (/^\w[^()[\]=!,`'"\s]*$/.test(attr.name)) {
|
42
|
+
attrOut += attr.name;
|
43
|
+
} else {
|
44
|
+
var name = attr.name.replace(/\\/g, '\\\\');
|
45
|
+
attrOut += name;
|
46
|
+
}
|
47
|
+
|
48
|
+
if (!(typeof constVal === 'boolean' && constVal === true)) {
|
49
|
+
// operator
|
50
|
+
attrOut += '=';
|
51
|
+
|
52
|
+
// value
|
53
|
+
attrOut += attr.val;
|
54
|
+
}
|
55
|
+
|
56
|
+
regularAttrs.push(attrOut);
|
57
|
+
}
|
58
|
+
}.bind(this)
|
59
|
+
);
|
60
|
+
|
61
|
+
var out = '';
|
62
|
+
if (id) out += '#' + id;
|
63
|
+
out += classes;
|
64
|
+
if (regularAttrs.length) out += '(' + regularAttrs.join(' ') + ')';
|
65
|
+
|
66
|
+
return out;
|
67
|
+
};
|
68
|
+
/* eslint-enable */
|
69
|
+
|
70
|
+
function traversePug(ast, { primaryRegx, i18nMethod, ignoreLines }, returns) {
|
71
|
+
// returesallTranslatedWord, updatedTranslatedWord, keysInUse, ignores) {
|
72
|
+
const { allTranslated, allUpdated, allUsedKeys } = returns;
|
73
|
+
const existValues = Object.keys(allTranslated);
|
74
|
+
|
75
|
+
function shouldIgnore(node) {
|
76
|
+
return node.line && ignoreLines.includes(node.line);
|
77
|
+
}
|
78
|
+
|
79
|
+
function isPrimary(str) {
|
80
|
+
return primaryRegx.test(str);
|
81
|
+
}
|
82
|
+
|
83
|
+
function formatValue(value) {
|
84
|
+
// 去掉首尾空白字符,中间的连续空白字符替换成一个空格
|
85
|
+
value = value.trim().replace(/\s+/g, ' ');
|
86
|
+
|
87
|
+
// 去掉首尾引号
|
88
|
+
if (['"', "'"].includes(value.charAt(0))) {
|
89
|
+
value = value.substring(1, value.length - 1);
|
90
|
+
}
|
91
|
+
|
92
|
+
return value;
|
93
|
+
}
|
94
|
+
|
95
|
+
// 更新2个 `all*` 数组
|
96
|
+
function updateLocaleInfo(key, value) {
|
97
|
+
if (!Array.isArray(allTranslated[value])) {
|
98
|
+
// 如果该文字没有存在于已翻译列表
|
99
|
+
allTranslated[value] = [key];
|
100
|
+
existValues.push(key);
|
101
|
+
}
|
102
|
+
|
103
|
+
if (!allUsedKeys.includes(key)) {
|
104
|
+
allUsedKeys.push(key);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
function transformJsExpression(source) {
|
109
|
+
const { source: source1 } = transformJs(
|
110
|
+
source,
|
111
|
+
{
|
112
|
+
allTranslated,
|
113
|
+
allUpdated,
|
114
|
+
allUsedKeys,
|
115
|
+
},
|
116
|
+
{
|
117
|
+
primaryRegx,
|
118
|
+
i18nObject: '',
|
119
|
+
i18nMethod,
|
120
|
+
importCode: '',
|
121
|
+
}
|
122
|
+
);
|
123
|
+
|
124
|
+
let code = prettier.format(source1, {
|
125
|
+
parser: 'babel',
|
126
|
+
singleQuote: true,
|
127
|
+
semi: false,
|
128
|
+
});
|
129
|
+
|
130
|
+
code = code.trim().replace(/\s+/g, ' ');
|
131
|
+
|
132
|
+
// 去掉开头的分号
|
133
|
+
if (code[0] === ';') code = code.slice(1);
|
134
|
+
|
135
|
+
return code;
|
136
|
+
}
|
137
|
+
|
138
|
+
function traverse(node) {
|
139
|
+
if (node.type === 'Tag' && node.block) {
|
140
|
+
traverse(node.block);
|
141
|
+
}
|
142
|
+
|
143
|
+
if (node.type === 'Block' && node.nodes) {
|
144
|
+
node.nodes.forEach((nd) => traverse(nd));
|
145
|
+
}
|
146
|
+
|
147
|
+
// 处理属性
|
148
|
+
if (!shouldIgnore(node) && node.attrs) {
|
149
|
+
node.attrs.forEach((attr) => {
|
150
|
+
const { name, val } = attr;
|
151
|
+
|
152
|
+
// 非主语言,跳过
|
153
|
+
if (!isPrimary(val)) return;
|
154
|
+
|
155
|
+
if (
|
156
|
+
name.startsWith('v-') ||
|
157
|
+
name.startsWith(':') ||
|
158
|
+
name.startsWith('@')
|
159
|
+
) {
|
160
|
+
// vue 指令
|
161
|
+
let value = val;
|
162
|
+
if (['"', "'"].includes(val.charAt(0))) {
|
163
|
+
value = val.substring(1, val.length - 1);
|
164
|
+
}
|
165
|
+
|
166
|
+
// 引号里是 js 表达式,直接调用 transformJs 来转换
|
167
|
+
const source = transformJsExpression(value);
|
168
|
+
|
169
|
+
if (`"${source}"` !== val) {
|
170
|
+
attr.val = `"${source}"`;
|
171
|
+
returns.hasTouch = true;
|
172
|
+
}
|
173
|
+
} else {
|
174
|
+
// 普通属性(应该不会有人在模板里写 onclick 等事件吧,故不考虑事件)
|
175
|
+
let key = formatValue(val);
|
176
|
+
|
177
|
+
if (allUpdated.hasOwnProperty(key)) {
|
178
|
+
key = allUpdated[key];
|
179
|
+
}
|
180
|
+
|
181
|
+
attr.val = `"${i18nMethod}('${key}')"`;
|
182
|
+
attr.name = `:${name}`;
|
183
|
+
returns.hasTouch = true;
|
184
|
+
}
|
185
|
+
});
|
186
|
+
}
|
187
|
+
|
188
|
+
// 处理 innerText
|
189
|
+
if (!shouldIgnore(node) && node.type === 'Text') {
|
190
|
+
if (!isPrimary(node.val)) return;
|
191
|
+
|
192
|
+
let value = '';
|
193
|
+
const tokens = mustache.parse(node.val);
|
194
|
+
|
195
|
+
for (const token of tokens) {
|
196
|
+
// token 结构:[类型(text|name), 值, 起始位置(包含), 终止位置(不包含)]
|
197
|
+
if (!isPrimary(token[1])) {
|
198
|
+
if (token[0] === 'text') value += token[1];
|
199
|
+
else if (token[0] === 'name') value += `{{${token[1]}}}`;
|
200
|
+
} else {
|
201
|
+
if (token[0] === 'text') {
|
202
|
+
const key = token[1].trim();
|
203
|
+
value += `{{${i18nMethod}('${key}')}}`;
|
204
|
+
|
205
|
+
updateLocaleInfo(key, key);
|
206
|
+
} else if (token[0] === 'name') {
|
207
|
+
const source = transformJsExpression(token[1]);
|
208
|
+
value += `{{${source}}}`;
|
209
|
+
}
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
if (node.val !== value) {
|
214
|
+
node.val = value;
|
215
|
+
returns.hasTouch = true;
|
216
|
+
}
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
// 执行遍历
|
221
|
+
traverse(ast);
|
222
|
+
}
|
223
|
+
|
224
|
+
module.exports = function transformPug(source, localeInfo = {}, options = {}) {
|
225
|
+
const { allTranslated = {}, allUpdated = {}, allUsedKeys = [] } = localeInfo;
|
226
|
+
|
227
|
+
const {
|
228
|
+
primaryRegx = /[\u4e00-\u9fa5]/,
|
229
|
+
i18nMethod = '$t',
|
230
|
+
|
231
|
+
/* 以下暂时不需要
|
232
|
+
i18nObject = '',
|
233
|
+
importCode = '',
|
234
|
+
babelPresets = [],
|
235
|
+
babelPlugins = [],
|
236
|
+
ignoreComponents = [],
|
237
|
+
ignoreMethods = [],
|
238
|
+
以上暂时不需要 */
|
239
|
+
} = options;
|
240
|
+
|
241
|
+
const opts = {
|
242
|
+
primaryRegx,
|
243
|
+
i18nMethod,
|
244
|
+
ignoreLines: [],
|
245
|
+
};
|
246
|
+
|
247
|
+
const r = {
|
248
|
+
allTranslated,
|
249
|
+
allUpdated,
|
250
|
+
allUsedKeys,
|
251
|
+
hasTouch: false,
|
252
|
+
};
|
253
|
+
|
254
|
+
opts.ignoreLines = getIgnoreLines(source);
|
255
|
+
const tokens = lex(source.trim());
|
256
|
+
|
257
|
+
const ast = parse(tokens);
|
258
|
+
traversePug(ast, opts, r);
|
259
|
+
|
260
|
+
let code = source;
|
261
|
+
if (r.hasTouch) {
|
262
|
+
const genCode = genSource(ast);
|
263
|
+
code = prettier
|
264
|
+
.format(genCode, {
|
265
|
+
filepath: 'xxx.pug',
|
266
|
+
pugAttributeSeparator: 'none',
|
267
|
+
})
|
268
|
+
.trim();
|
269
|
+
}
|
270
|
+
|
271
|
+
return { source: code, hasTouch: r.hasTouch };
|
272
|
+
};
|
@@ -0,0 +1,96 @@
|
|
1
|
+
const fs = require('fs');
|
2
|
+
const path = require('path');
|
3
|
+
const glob = require('glob');
|
4
|
+
const prettier = require('prettier');
|
5
|
+
const babel = require('@babel/core');
|
6
|
+
const pluginSyntaxJSX = require('@babel/plugin-syntax-jsx');
|
7
|
+
const pluginSyntaxProposalOptionalChaining = require('@babel/plugin-proposal-optional-chaining');
|
8
|
+
const pluginSyntaxClassProperties = require('@babel/plugin-syntax-class-properties');
|
9
|
+
const pluginSyntaxDecorators = require('@babel/plugin-syntax-decorators');
|
10
|
+
const pluginSyntaxObjectRestSpread = require('@babel/plugin-syntax-object-rest-spread');
|
11
|
+
const pluginSyntaxAsyncGenerators = require('@babel/plugin-syntax-async-generators');
|
12
|
+
const pluginSyntaxDoExpressions = require('@babel/plugin-syntax-do-expressions');
|
13
|
+
const pluginSyntaxDynamicImport = require('@babel/plugin-syntax-dynamic-import');
|
14
|
+
const pluginSyntaxFunctionBind = require('@babel/plugin-syntax-function-bind');
|
15
|
+
const reactIntlToReactIntlUniversal = require('../plugin/reactIntlToReactIntlUniversal');
|
16
|
+
const log = require('../utils/log');
|
17
|
+
|
18
|
+
function getSourceFiles({ path, exclude }) {
|
19
|
+
return glob.sync(`${path}/**/*.{js,jsx}`, {
|
20
|
+
ignore: exclude || [],
|
21
|
+
});
|
22
|
+
}
|
23
|
+
|
24
|
+
function getLocaleFiles({ path, exclude }) {
|
25
|
+
return glob.sync(`${path}/**/zh-CN.{js,json}`, {
|
26
|
+
ignore: exclude || [],
|
27
|
+
});
|
28
|
+
}
|
29
|
+
|
30
|
+
/**
|
31
|
+
* 代码转换为使用 react-intl-universal 版本
|
32
|
+
* @param {object} option 命令行或者配置文件中传入的参数信息
|
33
|
+
*/
|
34
|
+
module.exports = function transformReactIntlToReactIntlUniversal(option) {
|
35
|
+
const { entry, localeConf, exclude, importCode } = option;
|
36
|
+
|
37
|
+
const targetFiles = getSourceFiles({ path: entry, exclude });
|
38
|
+
const targetLocales = getLocaleFiles({ path: localeConf.folder, exclude });
|
39
|
+
|
40
|
+
let zhData = {};
|
41
|
+
|
42
|
+
targetLocales.forEach((element) => {
|
43
|
+
if (path.extname(element) === '.json') {
|
44
|
+
const json = fs.readFileSync(element, {
|
45
|
+
encoding: 'utf-8',
|
46
|
+
});
|
47
|
+
zhData = JSON.parse(json);
|
48
|
+
} else if (path.extname(element) === '.js') {
|
49
|
+
zhData = require(path.resolve(path.resolve(), element));
|
50
|
+
}
|
51
|
+
});
|
52
|
+
|
53
|
+
function transformFile(filePath) {
|
54
|
+
let outObj = {
|
55
|
+
hasReactIntlUniversal: false,
|
56
|
+
needRewrite: false,
|
57
|
+
};
|
58
|
+
|
59
|
+
const transformOptions = {
|
60
|
+
babelrc: false,
|
61
|
+
sourceType: 'module',
|
62
|
+
plugins: [
|
63
|
+
pluginSyntaxJSX,
|
64
|
+
pluginSyntaxProposalOptionalChaining,
|
65
|
+
pluginSyntaxClassProperties,
|
66
|
+
[pluginSyntaxDecorators, { legacy: true }],
|
67
|
+
pluginSyntaxObjectRestSpread,
|
68
|
+
pluginSyntaxAsyncGenerators,
|
69
|
+
pluginSyntaxDoExpressions,
|
70
|
+
pluginSyntaxDynamicImport,
|
71
|
+
pluginSyntaxFunctionBind,
|
72
|
+
reactIntlToReactIntlUniversal(zhData, outObj),
|
73
|
+
],
|
74
|
+
generatorOpts: {},
|
75
|
+
};
|
76
|
+
|
77
|
+
const bableObj = babel.transformFileSync(filePath, transformOptions);
|
78
|
+
let { code } = bableObj;
|
79
|
+
|
80
|
+
if (outObj.needRewrite) {
|
81
|
+
if (!outObj.hasReactIntlUniversal) {
|
82
|
+
code = importCode + '\n' + code;
|
83
|
+
}
|
84
|
+
|
85
|
+
code = prettier.format(code, option.prettier);
|
86
|
+
|
87
|
+
fs.writeFileSync(filePath, code, { encoding: 'utf-8' });
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
targetFiles.forEach((element) => {
|
92
|
+
log.info(`start: ${element}`);
|
93
|
+
transformFile(element);
|
94
|
+
log.success(`done: ${element}`);
|
95
|
+
});
|
96
|
+
};
|
@@ -0,0 +1,90 @@
|
|
1
|
+
const glob = require('glob');
|
2
|
+
const fs = require('fs');
|
3
|
+
const prettier = require('prettier');
|
4
|
+
const babel = require('@babel/core');
|
5
|
+
const pluginSyntaxJSX = require('@babel/plugin-syntax-jsx');
|
6
|
+
const pluginSyntaxProposalOptionalChaining = require('@babel/plugin-proposal-optional-chaining');
|
7
|
+
const pluginSyntaxClassProperties = require('@babel/plugin-syntax-class-properties');
|
8
|
+
const pluginSyntaxDecorators = require('@babel/plugin-syntax-decorators');
|
9
|
+
const pluginSyntaxObjectRestSpread = require('@babel/plugin-syntax-object-rest-spread');
|
10
|
+
const pluginSyntaxAsyncGenerators = require('@babel/plugin-syntax-async-generators');
|
11
|
+
const pluginSyntaxDoExpressions = require('@babel/plugin-syntax-do-expressions');
|
12
|
+
const pluginSyntaxDynamicImport = require('@babel/plugin-syntax-dynamic-import');
|
13
|
+
const pluginSyntaxFunctionBind = require('@babel/plugin-syntax-function-bind');
|
14
|
+
const getConvert2Plugin = require('../plugin/reactIntlUniversalToDi18n');
|
15
|
+
const log = require('../utils/log');
|
16
|
+
|
17
|
+
function getSourceFiles({ entry, exclude }) {
|
18
|
+
return glob.sync(`${entry}/**/*.{js,jsx}`, {
|
19
|
+
ignore: exclude || [],
|
20
|
+
});
|
21
|
+
}
|
22
|
+
|
23
|
+
function translate(codeFileInfo, allConverted, option) {
|
24
|
+
const { filePath, currentEntry, currentOutput } = codeFileInfo;
|
25
|
+
const { intlAlias } = option;
|
26
|
+
|
27
|
+
const sourceCode = fs.readFileSync(filePath, 'utf8');
|
28
|
+
|
29
|
+
let outObj = {
|
30
|
+
translateWordsNum: 0,
|
31
|
+
};
|
32
|
+
|
33
|
+
const transformOptions = {
|
34
|
+
sourceType: 'module',
|
35
|
+
plugins: [
|
36
|
+
pluginSyntaxJSX,
|
37
|
+
pluginSyntaxProposalOptionalChaining,
|
38
|
+
pluginSyntaxClassProperties,
|
39
|
+
[pluginSyntaxDecorators, { legacy: true }],
|
40
|
+
pluginSyntaxObjectRestSpread,
|
41
|
+
pluginSyntaxAsyncGenerators,
|
42
|
+
pluginSyntaxDoExpressions,
|
43
|
+
pluginSyntaxDynamicImport,
|
44
|
+
pluginSyntaxFunctionBind,
|
45
|
+
getConvert2Plugin(outObj, allConverted, intlAlias),
|
46
|
+
],
|
47
|
+
generatorOpts: {},
|
48
|
+
};
|
49
|
+
|
50
|
+
const bableObj = babel.transform(sourceCode, transformOptions);
|
51
|
+
let { code } = bableObj;
|
52
|
+
|
53
|
+
if (outObj.translateWordsNum > 0) {
|
54
|
+
code = prettier.format(code, option.prettier);
|
55
|
+
|
56
|
+
const target = currentOutput
|
57
|
+
? filePath.replace(currentEntry, currentOutput)
|
58
|
+
: filePath;
|
59
|
+
fs.writeFileSync(target, code, { encoding: 'utf-8' });
|
60
|
+
log.success(`done: ${outObj.translateWordsNum} words collected`);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
module.exports = function transformReactIntlUniveralToDi18n(option) {
|
65
|
+
const { entry, output, exclude } = option;
|
66
|
+
|
67
|
+
if (!Array.isArray(entry) && typeof entry !== 'string') {
|
68
|
+
log.error('entry must be a string or array');
|
69
|
+
process.exit(2);
|
70
|
+
}
|
71
|
+
|
72
|
+
const allConverted = {};
|
73
|
+
|
74
|
+
const outputs = output ? [].concat(output) : [];
|
75
|
+
const targetFiles = [].concat(entry).reduce((prev, cur, index) => {
|
76
|
+
const files = getSourceFiles({ entry: cur, exclude }).map((file) => ({
|
77
|
+
filePath: file,
|
78
|
+
currentEntry: cur,
|
79
|
+
currentOutput: outputs[index],
|
80
|
+
}));
|
81
|
+
return prev.concat(files);
|
82
|
+
}, []);
|
83
|
+
|
84
|
+
targetFiles.forEach((codeFileInfo) => {
|
85
|
+
translate(codeFileInfo, allConverted, option);
|
86
|
+
log.success(`done: ${codeFileInfo.filePath}`);
|
87
|
+
});
|
88
|
+
|
89
|
+
return allConverted;
|
90
|
+
};
|
@@ -0,0 +1,22 @@
|
|
1
|
+
const transformTs = require('./transformTs');
|
2
|
+
const transformJs = require('./transformJs');
|
3
|
+
const transformVue = require('./transformVue');
|
4
|
+
const { REACT_JS, REACT_TS, VUE } = require('../utils/constants');
|
5
|
+
|
6
|
+
module.exports = function transformDi18n(
|
7
|
+
source,
|
8
|
+
sourceType,
|
9
|
+
localeInfo,
|
10
|
+
options
|
11
|
+
) {
|
12
|
+
switch (sourceType) {
|
13
|
+
case REACT_JS:
|
14
|
+
return transformJs(source, localeInfo, options);
|
15
|
+
case REACT_TS:
|
16
|
+
return transformTs(source, localeInfo, options);
|
17
|
+
case VUE:
|
18
|
+
return transformVue(source, localeInfo, options);
|
19
|
+
default:
|
20
|
+
throw new Error(`Unsupport sourceType: ${sourceType}`);
|
21
|
+
}
|
22
|
+
};
|
@@ -0,0 +1,41 @@
|
|
1
|
+
const presetTypescript = require('@babel/preset-typescript').default;
|
2
|
+
const transformJs = require('./transformJs');
|
3
|
+
|
4
|
+
module.exports = function transformTs(code, localeInfo = {}, options = {}) {
|
5
|
+
const { allTranslated = {}, allUpdated = {}, allUsedKeys = [] } = localeInfo;
|
6
|
+
|
7
|
+
const {
|
8
|
+
primaryRegx = /[\u4e00-\u9fa5]/,
|
9
|
+
i18nObject = 'intl',
|
10
|
+
i18nMethod = 't',
|
11
|
+
importCode = "import { intl } from 'di18n-react';",
|
12
|
+
babelPresets = [],
|
13
|
+
babelPlugins = [],
|
14
|
+
ignoreComponents = [],
|
15
|
+
ignoreMethods = [],
|
16
|
+
ignoreAttributes = ['style', 'className'],
|
17
|
+
} = options;
|
18
|
+
|
19
|
+
return transformJs(
|
20
|
+
code,
|
21
|
+
{
|
22
|
+
allTranslated,
|
23
|
+
allUpdated,
|
24
|
+
allUsedKeys,
|
25
|
+
},
|
26
|
+
{
|
27
|
+
primaryRegx,
|
28
|
+
i18nObject,
|
29
|
+
i18nMethod,
|
30
|
+
importCode,
|
31
|
+
babelPresets: [
|
32
|
+
...babelPresets,
|
33
|
+
[presetTypescript, { isTSX: true, allExtensions: true }],
|
34
|
+
],
|
35
|
+
babelPlugins,
|
36
|
+
ignoreComponents,
|
37
|
+
ignoreMethods,
|
38
|
+
ignoreAttributes,
|
39
|
+
}
|
40
|
+
);
|
41
|
+
};
|
@@ -0,0 +1,126 @@
|
|
1
|
+
const compiler = require('vue-template-compiler');
|
2
|
+
const transformJs = require('./transformJs');
|
3
|
+
const transformPug = require('./transformPug');
|
4
|
+
const transformHtml = require('./transformHtml');
|
5
|
+
|
6
|
+
function openTag(sfcBlock) {
|
7
|
+
const { type, lang, src, scoped, module, attrs } = sfcBlock;
|
8
|
+
|
9
|
+
let tag = `<${type}`;
|
10
|
+
if (lang) tag += ` lang="${lang}"`;
|
11
|
+
if (src) tag += ` src="${src}"`;
|
12
|
+
if (scoped) tag += ' scoped';
|
13
|
+
if (module) {
|
14
|
+
if (typeof module === 'string') tag += ` module="${module}"`;
|
15
|
+
else tag += ' module';
|
16
|
+
}
|
17
|
+
for (let k in attrs) {
|
18
|
+
if (!['type', 'lang', 'src', 'scoped', 'module'].includes(k)) {
|
19
|
+
tag += ` ${k}="${attrs[k]}"`;
|
20
|
+
}
|
21
|
+
}
|
22
|
+
tag += '>';
|
23
|
+
|
24
|
+
return tag;
|
25
|
+
}
|
26
|
+
|
27
|
+
function closeTag(sfcBlock) {
|
28
|
+
return '</' + sfcBlock.type + '>';
|
29
|
+
}
|
30
|
+
|
31
|
+
function combineVue(template, script, sytles, customBlocks) {
|
32
|
+
return [template, script, ...sytles, ...customBlocks]
|
33
|
+
.map((sfc) =>
|
34
|
+
sfc ? `${openTag(sfc)}\n${sfc.content.trim()}\n${closeTag(sfc)}\n` : ''
|
35
|
+
)
|
36
|
+
.join('\n');
|
37
|
+
}
|
38
|
+
|
39
|
+
module.exports = function transformVue(source, localeInfo = {}, options = {}) {
|
40
|
+
const { allTranslated = {}, allUpdated = {}, allUsedKeys = [] } = localeInfo;
|
41
|
+
|
42
|
+
const {
|
43
|
+
primaryRegx = /[\u4e00-\u9fa5]/,
|
44
|
+
i18nObject = 'intl',
|
45
|
+
i18nMethod = '$t',
|
46
|
+
importCode = "import { intl } from 'di18n-vue';",
|
47
|
+
babelPresets = [],
|
48
|
+
babelPlugins = [],
|
49
|
+
ignoreComponents = [],
|
50
|
+
ignoreMethods = [],
|
51
|
+
pkMap = {},
|
52
|
+
} = options;
|
53
|
+
|
54
|
+
const sfc = compiler.parseComponent(source, {
|
55
|
+
pad: 'space',
|
56
|
+
deindent: false,
|
57
|
+
});
|
58
|
+
|
59
|
+
const { template, script, styles, customBlocks } = sfc;
|
60
|
+
let hasTouch = false;
|
61
|
+
|
62
|
+
if (template) {
|
63
|
+
const templateType = (template.lang || 'html').toLowerCase();
|
64
|
+
|
65
|
+
// transform template
|
66
|
+
if (['html', 'pug'].includes(templateType)) {
|
67
|
+
const transform = templateType === 'html' ? transformHtml : transformPug;
|
68
|
+
|
69
|
+
const ret = transform(
|
70
|
+
template.content,
|
71
|
+
{
|
72
|
+
allTranslated,
|
73
|
+
allUpdated,
|
74
|
+
allUsedKeys,
|
75
|
+
},
|
76
|
+
{
|
77
|
+
primaryRegx,
|
78
|
+
i18nObject: '',
|
79
|
+
i18nMethod,
|
80
|
+
importCode: '',
|
81
|
+
babelPresets,
|
82
|
+
babelPlugins,
|
83
|
+
ignoreComponents,
|
84
|
+
ignoreMethods,
|
85
|
+
pkMap,
|
86
|
+
}
|
87
|
+
);
|
88
|
+
|
89
|
+
template.content = ret.source;
|
90
|
+
hasTouch = ret.hasTouch;
|
91
|
+
} else {
|
92
|
+
console.warn(
|
93
|
+
`Unsupport type: ${templateType}, so the template is ignored`
|
94
|
+
);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
// transform script
|
99
|
+
if (script) {
|
100
|
+
const ret = transformJs(
|
101
|
+
script.content,
|
102
|
+
{
|
103
|
+
allTranslated,
|
104
|
+
allUpdated,
|
105
|
+
allUsedKeys,
|
106
|
+
},
|
107
|
+
{
|
108
|
+
primaryRegx,
|
109
|
+
i18nObject,
|
110
|
+
i18nMethod,
|
111
|
+
importCode,
|
112
|
+
babelPresets,
|
113
|
+
babelPlugins,
|
114
|
+
ignoreComponents,
|
115
|
+
ignoreMethods,
|
116
|
+
}
|
117
|
+
);
|
118
|
+
|
119
|
+
script.content = ret.source;
|
120
|
+
hasTouch = hasTouch || ret.hasTouch;
|
121
|
+
}
|
122
|
+
|
123
|
+
const code = combineVue(template, script, styles, customBlocks);
|
124
|
+
|
125
|
+
return { source: code, hasTouch };
|
126
|
+
};
|