ztxkutils 10.0.11 → 20.0.2
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-b3629ef3.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 +7 -19
- package/dist/index.js +5 -4
- package/dist/myIndexDb.d.ts +1 -1
- package/dist/print.d.ts +4 -5
- package/dist/print.js +12 -19
- package/dist/reqUrl.js +1 -1
- package/dist/request-d1972b41.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-21b58a69.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,224 @@
|
|
1
|
+
const t = require('@babel/types');
|
2
|
+
const log = require('../utils/log');
|
3
|
+
|
4
|
+
const isChinese = function (text) {
|
5
|
+
return /[\u4e00-\u9fa5]/.test(text);
|
6
|
+
};
|
7
|
+
|
8
|
+
/**
|
9
|
+
* 替换为 intl.get('xxxxxxxx').d('基本信息')
|
10
|
+
*/
|
11
|
+
function makeReplace({ value, variableObj, id }) {
|
12
|
+
let key = id;
|
13
|
+
|
14
|
+
// 用于防止中文转码为 unicode
|
15
|
+
const v = Object.assign(t.StringLiteral(value), {
|
16
|
+
extra: {
|
17
|
+
raw: `'${value}'`,
|
18
|
+
rawValue: value,
|
19
|
+
},
|
20
|
+
});
|
21
|
+
|
22
|
+
return t.CallExpression(
|
23
|
+
t.MemberExpression(
|
24
|
+
t.CallExpression(
|
25
|
+
t.MemberExpression(t.Identifier('intl'), t.Identifier('get')),
|
26
|
+
variableObj
|
27
|
+
? [typeof key === 'string' ? t.StringLiteral(key) : key, variableObj]
|
28
|
+
: [typeof key === 'string' ? t.StringLiteral(key) : key]
|
29
|
+
),
|
30
|
+
t.Identifier('d')
|
31
|
+
),
|
32
|
+
[v]
|
33
|
+
);
|
34
|
+
}
|
35
|
+
|
36
|
+
/**
|
37
|
+
* 获取代码转换的插件
|
38
|
+
* @param {object} zhData 中文文案资源
|
39
|
+
* @param {object} outObj 传出的参数对象
|
40
|
+
*/
|
41
|
+
function getPlugin(zhData, outObj) {
|
42
|
+
const cache = {};
|
43
|
+
|
44
|
+
// 防止中文转码为 unicode
|
45
|
+
function handleChinese(value, key) {
|
46
|
+
cache[key] = true;
|
47
|
+
return Object.assign(t.StringLiteral(value), {
|
48
|
+
extra: {
|
49
|
+
raw: `'${value}'`,
|
50
|
+
rawValue: value,
|
51
|
+
},
|
52
|
+
});
|
53
|
+
}
|
54
|
+
|
55
|
+
const plugin = function ({ types: t }) {
|
56
|
+
return {
|
57
|
+
visitor: {
|
58
|
+
ImportDeclaration(path) {
|
59
|
+
const { node } = path;
|
60
|
+
if (node.source.value === 'di18n-react') {
|
61
|
+
outObj.hasReactIntlUniversal = true;
|
62
|
+
}
|
63
|
+
|
64
|
+
if (node.source.value === 'react-intl') {
|
65
|
+
outObj.needRewrite = true;
|
66
|
+
log.info('remove: injectIntl');
|
67
|
+
path.remove();
|
68
|
+
}
|
69
|
+
},
|
70
|
+
Decorator(path) {
|
71
|
+
const { node } = path;
|
72
|
+
if (node.expression.name === 'injectIntl') {
|
73
|
+
outObj.needRewrite = true;
|
74
|
+
log.info('remove: injectIntl decorator');
|
75
|
+
path.remove();
|
76
|
+
}
|
77
|
+
},
|
78
|
+
BinaryExpression(path) {
|
79
|
+
const { node } = path;
|
80
|
+
|
81
|
+
// 替换类似 this.props.intl.locale === 'en' 为 intl.options.currentLocale === 'en-US'
|
82
|
+
if (
|
83
|
+
node.operator === '===' &&
|
84
|
+
node.right.type === 'StringLiteral' &&
|
85
|
+
node.right.value === 'en' &&
|
86
|
+
node.left.type === 'MemberExpression' &&
|
87
|
+
node.left.property.name === 'locale'
|
88
|
+
) {
|
89
|
+
outObj.needRewrite = true;
|
90
|
+
log.info("replace intl.locale === 'en'");
|
91
|
+
|
92
|
+
node.left = t.MemberExpression(
|
93
|
+
t.MemberExpression(t.Identifier('intl'), t.Identifier('options')),
|
94
|
+
t.Identifier('currentLocale')
|
95
|
+
);
|
96
|
+
node.right = t.StringLiteral('en-US');
|
97
|
+
}
|
98
|
+
},
|
99
|
+
ObjectPattern(path) {
|
100
|
+
const { node } = path;
|
101
|
+
|
102
|
+
const parent = path.parent;
|
103
|
+
if (!parent.init) {
|
104
|
+
return;
|
105
|
+
}
|
106
|
+
|
107
|
+
if (
|
108
|
+
(parent.init.type === 'Identifier' &&
|
109
|
+
parent.init.name === 'props') ||
|
110
|
+
(parent.init.type === 'MemberExpression' &&
|
111
|
+
parent.init.property.name === 'props')
|
112
|
+
) {
|
113
|
+
// 处理掉 let { params, intl } = this.props; 中的 intl
|
114
|
+
log.info('remove: this.props.intl');
|
115
|
+
node.properties = node.properties.filter(
|
116
|
+
(p) => !p.value || p.value.name !== 'intl'
|
117
|
+
);
|
118
|
+
}
|
119
|
+
},
|
120
|
+
JSXElement(path) {
|
121
|
+
const { node } = path;
|
122
|
+
const { openingElement } = node;
|
123
|
+
if (openingElement.name.name === 'FormattedMessage') {
|
124
|
+
outObj.needRewrite = true;
|
125
|
+
|
126
|
+
const idNode = openingElement.attributes.find(
|
127
|
+
(atr) => atr.name.name === 'id'
|
128
|
+
);
|
129
|
+
|
130
|
+
const id = idNode.value.value
|
131
|
+
? idNode.value.value
|
132
|
+
: idNode.value.expression;
|
133
|
+
|
134
|
+
const valuesNode = openingElement.attributes.find(
|
135
|
+
(atr) => atr.name.name === 'values'
|
136
|
+
);
|
137
|
+
let callExpression;
|
138
|
+
|
139
|
+
if (valuesNode) {
|
140
|
+
callExpression = makeReplace({
|
141
|
+
value: zhData[id] || 'TBD',
|
142
|
+
id: id,
|
143
|
+
variableObj: valuesNode.value.expression,
|
144
|
+
});
|
145
|
+
} else {
|
146
|
+
callExpression = makeReplace({
|
147
|
+
value: zhData[id] || 'TBD',
|
148
|
+
id: id,
|
149
|
+
});
|
150
|
+
}
|
151
|
+
|
152
|
+
if (path.parent.type === 'JSXExpressionContainer') {
|
153
|
+
path.replaceWith(callExpression);
|
154
|
+
} else if (path.parent.type === 'JSXElement') {
|
155
|
+
path.replaceWith(t.JSXExpressionContainer(callExpression));
|
156
|
+
} else {
|
157
|
+
path.replaceWith(callExpression);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
},
|
161
|
+
StringLiteral(path) {
|
162
|
+
const { node } = path;
|
163
|
+
const { value } = node;
|
164
|
+
const key = value + node.start + node.end;
|
165
|
+
if (isChinese(value) && !cache[key]) {
|
166
|
+
if (path.parent.type === 'JSXAttribute') {
|
167
|
+
path.replaceWith(handleChinese(value, key));
|
168
|
+
}
|
169
|
+
}
|
170
|
+
},
|
171
|
+
CallExpression(path) {
|
172
|
+
const { node } = path;
|
173
|
+
|
174
|
+
const handleFormatMessageMethod = () => {
|
175
|
+
const id = node.arguments[0].properties.find(
|
176
|
+
(prop) => prop.key.name === 'id'
|
177
|
+
).value.value;
|
178
|
+
outObj.needRewrite = true;
|
179
|
+
|
180
|
+
log.info(`replace: ${id}`);
|
181
|
+
|
182
|
+
if (node.arguments.length === 1) {
|
183
|
+
path.replaceWith(
|
184
|
+
makeReplace({ value: zhData[id] || 'TBD', id: id })
|
185
|
+
);
|
186
|
+
} else {
|
187
|
+
path.replaceWith(
|
188
|
+
makeReplace({
|
189
|
+
value: zhData[id] || 'TBD',
|
190
|
+
id: id,
|
191
|
+
variableObj: node.arguments[1],
|
192
|
+
})
|
193
|
+
);
|
194
|
+
}
|
195
|
+
};
|
196
|
+
|
197
|
+
if (node.callee.type === 'MemberExpression') {
|
198
|
+
if (node.callee.property.name === 'formatMessage') {
|
199
|
+
if (
|
200
|
+
(node.callee.object.property &&
|
201
|
+
node.callee.object.property.name === 'intl') ||
|
202
|
+
(node.callee.object.type === 'Identifier' &&
|
203
|
+
node.callee.object.name === 'intl')
|
204
|
+
) {
|
205
|
+
handleFormatMessageMethod();
|
206
|
+
}
|
207
|
+
}
|
208
|
+
} else {
|
209
|
+
if (node.callee.name === 'formatMessage') {
|
210
|
+
handleFormatMessageMethod();
|
211
|
+
} else if (node.callee.name === 'injectIntl') {
|
212
|
+
outObj.needRewrite = true;
|
213
|
+
path.replaceWith(node.arguments[0]);
|
214
|
+
}
|
215
|
+
}
|
216
|
+
},
|
217
|
+
},
|
218
|
+
};
|
219
|
+
};
|
220
|
+
|
221
|
+
return plugin;
|
222
|
+
}
|
223
|
+
|
224
|
+
module.exports = getPlugin;
|
@@ -0,0 +1,64 @@
|
|
1
|
+
const t = require('@babel/types');
|
2
|
+
|
3
|
+
const replaceLineBreak = function (value) {
|
4
|
+
if (typeof value !== 'string') return value;
|
5
|
+
return value.replace(/\n/g, ' ');
|
6
|
+
};
|
7
|
+
|
8
|
+
/**
|
9
|
+
* 获取代码转换的插件
|
10
|
+
*/
|
11
|
+
function getPlugin(outObj, allConverted, intlAlias = 'intl') {
|
12
|
+
function makeReplace({ orignKey, value, variableObj }) {
|
13
|
+
outObj.translateWordsNum++;
|
14
|
+
|
15
|
+
const value2 = replaceLineBreak(value);
|
16
|
+
|
17
|
+
// 用于防止中文转码为 unicode
|
18
|
+
const v = Object.assign(t.StringLiteral(value2), {
|
19
|
+
extra: {
|
20
|
+
raw: `'${value2}'`,
|
21
|
+
rawValue: value2,
|
22
|
+
},
|
23
|
+
});
|
24
|
+
|
25
|
+
allConverted[orignKey] = value;
|
26
|
+
|
27
|
+
return t.CallExpression(
|
28
|
+
t.MemberExpression(t.Identifier(intlAlias), t.Identifier('t')),
|
29
|
+
variableObj ? [v, variableObj] : [v]
|
30
|
+
);
|
31
|
+
}
|
32
|
+
|
33
|
+
const plugin = function () {
|
34
|
+
return {
|
35
|
+
visitor: {
|
36
|
+
CallExpression(path) {
|
37
|
+
const { node } = path;
|
38
|
+
|
39
|
+
if (node.callee.type === 'MemberExpression') {
|
40
|
+
if (
|
41
|
+
path.node.callee.object.name === intlAlias &&
|
42
|
+
path.node.callee.property.name === 'get'
|
43
|
+
) {
|
44
|
+
const args = path.node.arguments;
|
45
|
+
|
46
|
+
const orignKey = args[0].value;
|
47
|
+
const variableObj = args[1] ? args[1] : null;
|
48
|
+
|
49
|
+
const value = path.parentPath.parent.arguments[0].value;
|
50
|
+
|
51
|
+
path.parentPath.parentPath.replaceWith(
|
52
|
+
makeReplace({ orignKey, value, variableObj })
|
53
|
+
);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
},
|
57
|
+
},
|
58
|
+
};
|
59
|
+
};
|
60
|
+
|
61
|
+
return plugin;
|
62
|
+
}
|
63
|
+
|
64
|
+
module.exports = getPlugin;
|
@@ -0,0 +1,79 @@
|
|
1
|
+
// 当使用 HTML 模板时,由于大小写不敏感,为了避免转换中丢失大小写,需要提供组件的 Pascal 到 kebab 形式映射
|
2
|
+
// 默认的如下(iview: https://github.com/iview/iview-loader/blob/master/src/tag-map.js)
|
3
|
+
module.exports = {
|
4
|
+
Switch: 'i-switch',
|
5
|
+
Circle: 'i-circle',
|
6
|
+
Affix: 'i-affix',
|
7
|
+
Alert: 'i-alert',
|
8
|
+
AnchorLink: 'i-anchor-link',
|
9
|
+
Anchor: 'i-anchor',
|
10
|
+
AutoComplete: 'i-auto-complete',
|
11
|
+
Avatar: 'i-avatar',
|
12
|
+
BackTop: 'i-back-top',
|
13
|
+
Badge: 'i-badge',
|
14
|
+
BreadcrumbItem: 'i-breadcrumb-item',
|
15
|
+
Breadcrumb: 'i-breadcrumb',
|
16
|
+
ButtonGroup: 'i-button-group',
|
17
|
+
Button: 'i-button',
|
18
|
+
Card: 'i-card',
|
19
|
+
CarouselItem: 'i-carousel-item',
|
20
|
+
Carousel: 'i-carousel',
|
21
|
+
Cascader: 'i-cascader',
|
22
|
+
CellGroup: 'i-cell-group',
|
23
|
+
Cell: 'i-cell',
|
24
|
+
CheckboxGroup: 'i-checkbox-group',
|
25
|
+
Checkbox: 'i-checkbox',
|
26
|
+
Col: 'i-col',
|
27
|
+
Collapse: 'i-collapse',
|
28
|
+
ColorPicker: 'i-color-picker',
|
29
|
+
Content: 'i-content',
|
30
|
+
DatePicker: 'i-date-picker',
|
31
|
+
Divider: 'i-divider',
|
32
|
+
Drawer: 'i-drawer',
|
33
|
+
DropdownItem: 'i-dropdown-item',
|
34
|
+
DropdownMenu: 'i-dropdown-menu',
|
35
|
+
Dropdown: 'i-dropdown',
|
36
|
+
Footer: 'i-footer',
|
37
|
+
FormItem: 'i-form-item',
|
38
|
+
Form: 'i-form',
|
39
|
+
Header: 'i-header',
|
40
|
+
Icon: 'i-icon',
|
41
|
+
InputNumber: 'i-input-number',
|
42
|
+
Input: 'i-input',
|
43
|
+
Layout: 'i-layout',
|
44
|
+
Menu: 'i-menu',
|
45
|
+
MenuGroup: 'i-menu-group',
|
46
|
+
MenuItem: 'i-menu-item',
|
47
|
+
Sider: 'i-sider',
|
48
|
+
Submenu: 'i-submenu',
|
49
|
+
Modal: 'i-modal',
|
50
|
+
OptionGroup: 'i-option-group',
|
51
|
+
Option: 'i-option',
|
52
|
+
Page: 'i-page',
|
53
|
+
Panel: 'i-panel',
|
54
|
+
Poptip: 'i-poptip',
|
55
|
+
Progress: 'i-progress',
|
56
|
+
RadioGroup: 'i-radio-group',
|
57
|
+
Radio: 'i-radio',
|
58
|
+
Rate: 'i-rate',
|
59
|
+
Row: 'i-row',
|
60
|
+
Scroll: 'i-scroll',
|
61
|
+
Select: 'i-select',
|
62
|
+
Slider: 'i-slider',
|
63
|
+
Spin: 'i-spin',
|
64
|
+
Split: 'i-split',
|
65
|
+
Step: 'i-step',
|
66
|
+
Steps: 'i-steps',
|
67
|
+
Table: 'i-table',
|
68
|
+
Tabs: 'i-tabs',
|
69
|
+
TabPane: 'i-tab-pane',
|
70
|
+
Tag: 'i-tag',
|
71
|
+
TimelineItem: 'i-timeline-item',
|
72
|
+
Timeline: 'i-timeline',
|
73
|
+
TimePicker: 'i-time-picker',
|
74
|
+
Time: 'i-time',
|
75
|
+
Tooltip: 'i-tooltip',
|
76
|
+
Transfer: 'i-transfer',
|
77
|
+
Tree: 'i-tree',
|
78
|
+
Upload: 'i-upload',
|
79
|
+
};
|
@@ -0,0 +1,271 @@
|
|
1
|
+
const parse5 = require('parse5');
|
2
|
+
const Serializer = require('parse5/lib/serializer');
|
3
|
+
const { NAMESPACES: NS } = require('parse5/lib/common/html');
|
4
|
+
const prettier = require('prettier');
|
5
|
+
const mustache = require('mustache');
|
6
|
+
const transformJs = require('./transformJs');
|
7
|
+
const getIgnoreLines = require('../utils/getIgnoreLines');
|
8
|
+
const defaultPkMap = require('./defaultPkMap');
|
9
|
+
|
10
|
+
class MySerializer extends Serializer {
|
11
|
+
_serializeAttributes(node) {
|
12
|
+
const attrs = this.treeAdapter.getAttrList(node);
|
13
|
+
|
14
|
+
for (let i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
|
15
|
+
const attr = attrs[i];
|
16
|
+
const value = Serializer.escapeString(attr.value, true);
|
17
|
+
|
18
|
+
this.html += ' ';
|
19
|
+
|
20
|
+
if (!attr.namespace) {
|
21
|
+
this.html += attr.name;
|
22
|
+
} else if (attr.namespace === NS.XML) {
|
23
|
+
this.html += 'xml:' + attr.name;
|
24
|
+
} else if (attr.namespace === NS.XMLNS) {
|
25
|
+
if (attr.name !== 'xmlns') {
|
26
|
+
this.html += 'xmlns:';
|
27
|
+
}
|
28
|
+
|
29
|
+
this.html += attr.name;
|
30
|
+
} else if (attr.namespace === NS.XLINK) {
|
31
|
+
this.html += 'xlink:' + attr.name;
|
32
|
+
} else {
|
33
|
+
this.html += attr.prefix + ':' + attr.name;
|
34
|
+
}
|
35
|
+
|
36
|
+
// 避免出现 <p v-else="">xxx</p> 的情况
|
37
|
+
if (value) {
|
38
|
+
this.html += '="' + value + '"';
|
39
|
+
}
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
function parse5Serialize(node, options) {
|
45
|
+
const serializer = new MySerializer(node, options);
|
46
|
+
|
47
|
+
return serializer.serialize();
|
48
|
+
}
|
49
|
+
|
50
|
+
function toKebab(tpl, pkMap = {}) {
|
51
|
+
pkMap = { ...defaultPkMap, ...pkMap };
|
52
|
+
|
53
|
+
Object.keys(pkMap).forEach((i) => {
|
54
|
+
tpl = tpl
|
55
|
+
.replace(new RegExp(`<${i}(?![a-zA-Z0-9-])`, 'g'), `<${pkMap[i]}`)
|
56
|
+
.replace(new RegExp(`</${i}>`, 'g'), `</${pkMap[i]}>`);
|
57
|
+
});
|
58
|
+
return tpl;
|
59
|
+
}
|
60
|
+
|
61
|
+
function toPascal(tpl, pkMap = {}) {
|
62
|
+
pkMap = { ...defaultPkMap, ...pkMap };
|
63
|
+
|
64
|
+
Object.keys(pkMap).forEach((i) => {
|
65
|
+
tpl = tpl
|
66
|
+
.replace(new RegExp(`<${pkMap[i]}(?![a-zA-Z0-9-])`, 'g'), `<${i}`)
|
67
|
+
.replace(new RegExp(`</${pkMap[i]}>`, 'g'), `</${i}>`);
|
68
|
+
});
|
69
|
+
return tpl;
|
70
|
+
}
|
71
|
+
|
72
|
+
function traverseHtml(ast, { primaryRegx, i18nMethod, ignoreLines }, returns) {
|
73
|
+
const { allTranslated, allUpdated, allUsedKeys } = returns;
|
74
|
+
const existValues = Object.keys(allTranslated);
|
75
|
+
|
76
|
+
function shouldIgnore(node) {
|
77
|
+
return (
|
78
|
+
node.sourceCodeLocation &&
|
79
|
+
ignoreLines.includes(node.sourceCodeLocation.startLine)
|
80
|
+
);
|
81
|
+
}
|
82
|
+
|
83
|
+
function isPrimary(str) {
|
84
|
+
return primaryRegx.test(str);
|
85
|
+
}
|
86
|
+
|
87
|
+
function formatValue(value) {
|
88
|
+
// 去掉首尾空白字符,中间的连续空白字符替换成一个空格
|
89
|
+
value = value.trim().replace(/\s+/g, ' ');
|
90
|
+
|
91
|
+
// 去掉首尾引号
|
92
|
+
if (['"', "'"].includes(value.charAt(0))) {
|
93
|
+
value = value.substring(1, value.length - 1);
|
94
|
+
}
|
95
|
+
|
96
|
+
return value;
|
97
|
+
}
|
98
|
+
|
99
|
+
// 更新2个 `all*` 数组
|
100
|
+
function updateLocaleInfo(key, value) {
|
101
|
+
if (!Array.isArray(allTranslated[value])) {
|
102
|
+
// 如果该文字没有存在于已翻译列表
|
103
|
+
allTranslated[value] = [key];
|
104
|
+
existValues.push(key);
|
105
|
+
}
|
106
|
+
|
107
|
+
if (!allUsedKeys.includes(key)) {
|
108
|
+
allUsedKeys.push(key);
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
function transformJsExpression(source) {
|
113
|
+
const { source: source1, hasTouch } = transformJs(
|
114
|
+
source,
|
115
|
+
{
|
116
|
+
allTranslated,
|
117
|
+
allUpdated,
|
118
|
+
allUsedKeys,
|
119
|
+
},
|
120
|
+
{
|
121
|
+
primaryRegx,
|
122
|
+
i18nObject: '',
|
123
|
+
i18nMethod,
|
124
|
+
importCode: '',
|
125
|
+
}
|
126
|
+
);
|
127
|
+
|
128
|
+
if (!hasTouch) return source;
|
129
|
+
|
130
|
+
return prettier
|
131
|
+
.format(source1, {
|
132
|
+
parser: 'babel',
|
133
|
+
singleQuote: true,
|
134
|
+
semi: false,
|
135
|
+
})
|
136
|
+
.trim();
|
137
|
+
}
|
138
|
+
|
139
|
+
function traverse(node) {
|
140
|
+
if (node.childNodes) {
|
141
|
+
node.childNodes.forEach((childNode) => traverse(childNode));
|
142
|
+
}
|
143
|
+
|
144
|
+
// 处理属性
|
145
|
+
if (!shouldIgnore(node) && node.attrs) {
|
146
|
+
node.attrs.forEach((attr) => {
|
147
|
+
const { name, value } = attr;
|
148
|
+
|
149
|
+
// 非主语言或空,跳过
|
150
|
+
if (!isPrimary(value) || !value) return;
|
151
|
+
|
152
|
+
if (
|
153
|
+
name.startsWith('v-') ||
|
154
|
+
name.startsWith(':') ||
|
155
|
+
name.startsWith('@')
|
156
|
+
) {
|
157
|
+
// vue 指令
|
158
|
+
// 引号里是 js 表达式,直接调用 transformJs 来转换
|
159
|
+
const source = transformJsExpression(value);
|
160
|
+
|
161
|
+
if (value !== source) {
|
162
|
+
attr.value = source;
|
163
|
+
returns.hasTouch = true;
|
164
|
+
}
|
165
|
+
} else {
|
166
|
+
// 普通属性(不考虑事件)
|
167
|
+
let key = formatValue(value);
|
168
|
+
|
169
|
+
if (allUpdated.hasOwnProperty(key)) {
|
170
|
+
key = allUpdated[key];
|
171
|
+
}
|
172
|
+
|
173
|
+
attr.value = `${i18nMethod}('${key}')`;
|
174
|
+
attr.name = `:${name}`;
|
175
|
+
returns.hasTouch = true;
|
176
|
+
|
177
|
+
updateLocaleInfo(key, key);
|
178
|
+
}
|
179
|
+
});
|
180
|
+
}
|
181
|
+
|
182
|
+
// 处理 innerText
|
183
|
+
if (!shouldIgnore(node) && node.nodeName === '#text') {
|
184
|
+
if (!isPrimary(node.value)) return;
|
185
|
+
|
186
|
+
let value = '';
|
187
|
+
const tokens = mustache.parse(node.value);
|
188
|
+
|
189
|
+
for (const token of tokens) {
|
190
|
+
// token 结构:[类型(text|name), 值, 起始位置(包含), 终止位置(不包含)]
|
191
|
+
if (!isPrimary(token[1])) {
|
192
|
+
if (token[0] === 'text') value += token[1];
|
193
|
+
else if (token[0] === 'name') value += `{{${token[1]}}}`;
|
194
|
+
} else {
|
195
|
+
if (token[0] === 'text') {
|
196
|
+
const key = token[1].trim();
|
197
|
+
value += `{{${i18nMethod}('${key}')}}`;
|
198
|
+
|
199
|
+
updateLocaleInfo(key, key);
|
200
|
+
} else if (token[0] === 'name') {
|
201
|
+
value += `{{${transformJsExpression(token[1])}}}`;
|
202
|
+
}
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
if (node.value !== value) {
|
207
|
+
node.value = value;
|
208
|
+
returns.hasTouch = true;
|
209
|
+
}
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
// 可能有 #comment 节点,需要找到 html 节点
|
214
|
+
const html = ast.childNodes.find((nd) => nd.nodeName === 'html');
|
215
|
+
|
216
|
+
if (html) {
|
217
|
+
// 再找 body 节点
|
218
|
+
const body = html.childNodes.find((nd) => nd.nodeName === 'body');
|
219
|
+
|
220
|
+
// 遍历
|
221
|
+
if (body) traverse(body);
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
module.exports = function transformHtml(source, localeInfo = {}, options = {}) {
|
226
|
+
const { allTranslated = {}, allUpdated = {}, allUsedKeys = [] } = localeInfo;
|
227
|
+
|
228
|
+
const {
|
229
|
+
primaryRegx = /[\u4e00-\u9fa5]/,
|
230
|
+
i18nMethod = '$t',
|
231
|
+
pkMap = {},
|
232
|
+
|
233
|
+
/* 以下暂时不需要
|
234
|
+
i18nObject = '',
|
235
|
+
importCode = '',
|
236
|
+
babelPresets = [],
|
237
|
+
babelPlugins = [],
|
238
|
+
ignoreComponents = [],
|
239
|
+
ignoreMethods = [],
|
240
|
+
以上暂时不需要 */
|
241
|
+
} = options;
|
242
|
+
|
243
|
+
const opts = {
|
244
|
+
primaryRegx,
|
245
|
+
i18nMethod,
|
246
|
+
ignoreLines: [],
|
247
|
+
};
|
248
|
+
|
249
|
+
const r = {
|
250
|
+
allTranslated,
|
251
|
+
allUpdated,
|
252
|
+
allUsedKeys,
|
253
|
+
hasTouch: false,
|
254
|
+
};
|
255
|
+
|
256
|
+
opts.ignoreLines = getIgnoreLines(source);
|
257
|
+
|
258
|
+
const ast = parse5.parse(toKebab(source, pkMap), {
|
259
|
+
sourceCodeLocationInfo: true,
|
260
|
+
});
|
261
|
+
traverseHtml(ast, opts, r);
|
262
|
+
|
263
|
+
let code = toPascal(parse5Serialize(ast), pkMap);
|
264
|
+
|
265
|
+
// 只需要 body 内的
|
266
|
+
code = code.split('<body>')[1].split('</body>')[0];
|
267
|
+
|
268
|
+
code = r.hasTouch ? code : source;
|
269
|
+
|
270
|
+
return { source: code, hasTouch: r.hasTouch };
|
271
|
+
};
|