ztxkutils 2.10.66-12-feat → 2.10.66-13-beta
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/package.json +5 -3
- package/zti18n-cli/index.js +23 -0
- package/zti18n-cli/src/command/collect.js +342 -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 +135 -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 +6 -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 +74 -0
- package/zti18n-cli/src/utils/vueOptions.js +70 -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 +59 -0
- package/zti18n-core/src/transform/defaultPkMap.js +79 -0
- package/zti18n-core/src/transform/transformHtml.js +275 -0
- package/zti18n-core/src/transform/transformJs.js +502 -0
- package/zti18n-core/src/transform/transformPug.js +262 -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 +45 -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
- /package/{src/zti18n-cli → zti18n-cli}/bin/index.js +0 -0
|
@@ -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,275 @@
|
|
|
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 = (
|
|
55
|
+
tpl
|
|
56
|
+
.replace(new RegExp(`<${i}(?![a-zA-Z0-9-])`, 'g'), `<${pkMap[i]}`)
|
|
57
|
+
.replace(new RegExp(`</${i}>`, 'g'), `</${pkMap[i]}>`)
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
return tpl;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function toPascal(tpl, pkMap = {}) {
|
|
64
|
+
pkMap = { ...defaultPkMap, ...pkMap };
|
|
65
|
+
|
|
66
|
+
Object.keys(pkMap).forEach(i => {
|
|
67
|
+
tpl = (
|
|
68
|
+
tpl
|
|
69
|
+
.replace(new RegExp(`<${pkMap[i]}(?![a-zA-Z0-9-])`, 'g'), `<${i}`)
|
|
70
|
+
.replace(new RegExp(`</${pkMap[i]}>`, 'g'), `</${i}>`)
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
return tpl;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function traverseHtml(ast, {
|
|
77
|
+
primaryRegx,
|
|
78
|
+
i18nMethod,
|
|
79
|
+
ignoreLines,
|
|
80
|
+
}, returns) {
|
|
81
|
+
const { allTranslated, allUpdated, allUsedKeys } = returns;
|
|
82
|
+
const existValues = Object.keys(allTranslated);
|
|
83
|
+
|
|
84
|
+
function shouldIgnore(node) {
|
|
85
|
+
return (
|
|
86
|
+
node.sourceCodeLocation
|
|
87
|
+
&& ignoreLines.includes(node.sourceCodeLocation.startLine)
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isPrimary(str) {
|
|
92
|
+
return primaryRegx.test(str);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function formatValue(value) {
|
|
96
|
+
// 去掉首尾空白字符,中间的连续空白字符替换成一个空格
|
|
97
|
+
value = value.trim().replace(/\s+/g, ' ');
|
|
98
|
+
|
|
99
|
+
// 去掉首尾引号
|
|
100
|
+
if (['"', "'"].includes(value.charAt(0))) {
|
|
101
|
+
value = value.substring(1, value.length - 1);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 更新2个 `all*` 数组
|
|
108
|
+
function updateLocaleInfo(key, value) {
|
|
109
|
+
if (!Array.isArray(allTranslated[value])) {
|
|
110
|
+
// 如果该文字没有存在于已翻译列表
|
|
111
|
+
allTranslated[value] = [key];
|
|
112
|
+
existValues.push(key);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!allUsedKeys.includes(key)) {
|
|
116
|
+
allUsedKeys.push(key);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function transformJsExpression(source) {
|
|
121
|
+
const { source: source1, hasTouch } = transformJs(
|
|
122
|
+
source,
|
|
123
|
+
{
|
|
124
|
+
allTranslated,
|
|
125
|
+
allUpdated,
|
|
126
|
+
allUsedKeys,
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
primaryRegx,
|
|
130
|
+
i18nObject: '',
|
|
131
|
+
i18nMethod,
|
|
132
|
+
importCode: '',
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
if (!hasTouch) return source;
|
|
137
|
+
|
|
138
|
+
return prettier.format(source1, {
|
|
139
|
+
parser: 'babel',
|
|
140
|
+
singleQuote: true,
|
|
141
|
+
semi: false,
|
|
142
|
+
}).trim();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function traverse(node) {
|
|
146
|
+
if (node.childNodes) {
|
|
147
|
+
node.childNodes.forEach(childNode => traverse(childNode));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 处理属性
|
|
151
|
+
if (!shouldIgnore(node) && node.attrs) {
|
|
152
|
+
node.attrs.forEach(attr => {
|
|
153
|
+
const { name, value } = attr;
|
|
154
|
+
|
|
155
|
+
// 非主语言或空,跳过
|
|
156
|
+
if (!isPrimary(value) || !value) return;
|
|
157
|
+
|
|
158
|
+
if (name.startsWith('v-') || name.startsWith(':') || name.startsWith('@')) {
|
|
159
|
+
// vue 指令
|
|
160
|
+
// 引号里是 js 表达式,直接调用 transformJs 来转换
|
|
161
|
+
const source = transformJsExpression(value);
|
|
162
|
+
|
|
163
|
+
if (value !== source) {
|
|
164
|
+
attr.value = source;
|
|
165
|
+
returns.hasTouch = true;
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
// 普通属性(不考虑事件)
|
|
169
|
+
let key = formatValue(value);
|
|
170
|
+
|
|
171
|
+
if (allUpdated.hasOwnProperty(key)) {
|
|
172
|
+
key = allUpdated[key];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
attr.value = `${i18nMethod}('${key}')`;
|
|
176
|
+
attr.name = `:${name}`;
|
|
177
|
+
returns.hasTouch = true;
|
|
178
|
+
|
|
179
|
+
updateLocaleInfo(key, key);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 处理 innerText
|
|
185
|
+
if (!shouldIgnore(node) && node.nodeName === '#text') {
|
|
186
|
+
if (!isPrimary(node.value)) return;
|
|
187
|
+
|
|
188
|
+
let value = '';
|
|
189
|
+
const tokens = mustache.parse(node.value);
|
|
190
|
+
|
|
191
|
+
for (const token of tokens) {
|
|
192
|
+
// token 结构:[类型(text|name), 值, 起始位置(包含), 终止位置(不包含)]
|
|
193
|
+
if (!isPrimary(token[1])) {
|
|
194
|
+
if (token[0] === 'text') value += token[1];
|
|
195
|
+
else if (token[0] === 'name') value += `{{${token[1]}}}`;
|
|
196
|
+
} else {
|
|
197
|
+
if (token[0] === 'text') {
|
|
198
|
+
const key = token[1].trim();
|
|
199
|
+
value += `{{${i18nMethod}('${key}')}}`;
|
|
200
|
+
|
|
201
|
+
updateLocaleInfo(key, key);
|
|
202
|
+
} else if (token[0] === 'name') {
|
|
203
|
+
value += `{{${transformJsExpression(token[1])}}}`;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (node.value !== value) {
|
|
209
|
+
node.value = value;
|
|
210
|
+
returns.hasTouch = true;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 可能有 #comment 节点,需要找到 html 节点
|
|
216
|
+
const html = ast.childNodes.find(nd => nd.nodeName === 'html');
|
|
217
|
+
|
|
218
|
+
if (html) {
|
|
219
|
+
// 再找 body 节点
|
|
220
|
+
const body = html.childNodes.find(nd => nd.nodeName === 'body');
|
|
221
|
+
|
|
222
|
+
// 遍历
|
|
223
|
+
if (body) traverse(body);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
module.exports = function transformHtml(source, localeInfo = {}, options = {}) {
|
|
228
|
+
const {
|
|
229
|
+
allTranslated = {},
|
|
230
|
+
allUpdated = {},
|
|
231
|
+
allUsedKeys = [],
|
|
232
|
+
} = localeInfo;
|
|
233
|
+
|
|
234
|
+
const {
|
|
235
|
+
primaryRegx = /[\u4e00-\u9fa5]/,
|
|
236
|
+
i18nMethod = '$t',
|
|
237
|
+
pkMap = {},
|
|
238
|
+
|
|
239
|
+
/* 以下暂时不需要
|
|
240
|
+
i18nObject = '',
|
|
241
|
+
importCode = '',
|
|
242
|
+
babelPresets = [],
|
|
243
|
+
babelPlugins = [],
|
|
244
|
+
ignoreComponents = [],
|
|
245
|
+
ignoreMethods = [],
|
|
246
|
+
以上暂时不需要 */
|
|
247
|
+
} = options;
|
|
248
|
+
|
|
249
|
+
const opts = {
|
|
250
|
+
primaryRegx,
|
|
251
|
+
i18nMethod,
|
|
252
|
+
ignoreLines: [],
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const r = {
|
|
256
|
+
allTranslated,
|
|
257
|
+
allUpdated,
|
|
258
|
+
allUsedKeys,
|
|
259
|
+
hasTouch: false,
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
opts.ignoreLines = getIgnoreLines(source);
|
|
263
|
+
|
|
264
|
+
const ast = parse5.parse(toKebab(source, pkMap), { sourceCodeLocationInfo: true });
|
|
265
|
+
traverseHtml(ast, opts, r);
|
|
266
|
+
|
|
267
|
+
let code = toPascal(parse5Serialize(ast), pkMap);
|
|
268
|
+
|
|
269
|
+
// 只需要 body 内的
|
|
270
|
+
code = code.split('<body>')[1].split('</body>')[0];
|
|
271
|
+
|
|
272
|
+
code = r.hasTouch ? code : source;
|
|
273
|
+
|
|
274
|
+
return { source: code, hasTouch: r.hasTouch };
|
|
275
|
+
};
|