ztxkutils 2.10.66-3 → 2.10.66-30
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-b3629ef3.js +26 -0
- package/dist/dataModel-f1ef06bc.js +26 -0
- package/dist/dataModel.js +2 -1
- package/dist/fileOperation.d.ts +19 -0
- package/dist/fileOperation.js +60 -18
- package/dist/hooks.js +34 -22
- package/dist/i18next.d.ts +2 -0
- package/dist/i18next.js +2401 -0
- package/dist/index.js +5 -4
- package/dist/myIndexDb.d.ts +1 -1
- package/dist/print.d.ts +2 -4
- package/dist/print.js +3 -2
- package/dist/reqUrl-ea7ef876.js +83 -0
- package/dist/reqUrl.js +1 -1
- package/dist/{request-dc69f021.js → request-1e442d5d.js} +26 -16
- package/dist/request-40e9ca7b.js +2982 -0
- package/dist/{request-87d93572.js → request-4c29d6de.js} +3 -0
- package/dist/request-80d1ac80.js +2992 -0
- package/dist/request-986d7090.js +2923 -0
- package/dist/{request-3c906c2d.js → request-c0970aae.js} +5 -1
- package/dist/request-d1972b41.js +2992 -0
- package/dist/request-d8d72b87.js +2982 -0
- package/dist/request-f600ad7a.js +2992 -0
- package/dist/request.js +2 -1
- package/dist/stompClient.js +21 -8
- package/dist/useHistory.js +7 -0
- package/dist/validate-21164759.js +260 -0
- package/dist/validate-2138d94a.js +263 -0
- package/dist/validate-21b58a69.js +260 -0
- package/dist/validate-2de5a28f.js +260 -0
- package/dist/validate-ab47ebe9.js +260 -0
- package/dist/validate.d.ts +1 -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,75 @@
|
|
1
|
+
const program = require('commander');
|
2
|
+
const convert = require('./command/convert');
|
3
|
+
const convert2 = require('./command/convert2');
|
4
|
+
const collect = require('./command/collect');
|
5
|
+
const publish = require('./command/publish');
|
6
|
+
const option = require('../../package.json');
|
7
|
+
|
8
|
+
module.exports = program;
|
9
|
+
|
10
|
+
program
|
11
|
+
.version(option.version)
|
12
|
+
.option(
|
13
|
+
'-c, --config <path>',
|
14
|
+
'set config path. defaults to ./i18n.config.js'
|
15
|
+
);
|
16
|
+
|
17
|
+
program
|
18
|
+
.command('sync')
|
19
|
+
.alias('s')
|
20
|
+
.description('pick chinese words and snyc with locale conf')
|
21
|
+
.option('-p, --publish', 'to publish the synced conf or not, defautl : false')
|
22
|
+
.action(function (options) {
|
23
|
+
collect(program.opts(), !!options.publish);
|
24
|
+
})
|
25
|
+
.on('--help', function () {
|
26
|
+
console.log(' Examples:');
|
27
|
+
console.log();
|
28
|
+
console.log(' sync without publish:');
|
29
|
+
console.log(' $ zti18n sync -c ./config/prod.config.js');
|
30
|
+
console.log(' sync and then publish:');
|
31
|
+
console.log(' $ zti18n sync -p -c ./config/prod.config.js');
|
32
|
+
console.log();
|
33
|
+
});
|
34
|
+
|
35
|
+
program
|
36
|
+
.command('publish')
|
37
|
+
.description('publish locale confs')
|
38
|
+
.action(function () {
|
39
|
+
publish(program.opts());
|
40
|
+
})
|
41
|
+
.on('--help', function () {
|
42
|
+
console.log(' Examples:');
|
43
|
+
console.log();
|
44
|
+
console.log(' $ zti18n publish');
|
45
|
+
console.log();
|
46
|
+
});
|
47
|
+
|
48
|
+
// XXX: 不表意,需要改一下
|
49
|
+
program
|
50
|
+
.command('convert')
|
51
|
+
.alias('c')
|
52
|
+
.description('convert to react-intl-universal')
|
53
|
+
.action(function () {
|
54
|
+
convert(program.opts());
|
55
|
+
})
|
56
|
+
.on('--help', function () {
|
57
|
+
console.log(' Examples:');
|
58
|
+
console.log();
|
59
|
+
console.log(' $ zti18n convert -c ./config/prod.config.js');
|
60
|
+
console.log();
|
61
|
+
});
|
62
|
+
|
63
|
+
program
|
64
|
+
.command('convert2')
|
65
|
+
.alias('c2')
|
66
|
+
.description('convert to intl.t')
|
67
|
+
.action(function () {
|
68
|
+
convert2(program.opts());
|
69
|
+
})
|
70
|
+
.on('--help', function () {
|
71
|
+
console.log(' Examples:');
|
72
|
+
console.log();
|
73
|
+
console.log(' $ zti18n convert2 -c ./config/prod.config.js');
|
74
|
+
console.log();
|
75
|
+
});
|
@@ -0,0 +1,87 @@
|
|
1
|
+
const crypto = require('crypto');
|
2
|
+
const https = require('https');
|
3
|
+
const querystring = require('querystring');
|
4
|
+
|
5
|
+
function MD5(string) {
|
6
|
+
return crypto.createHash('md5').update(string).digest('hex');
|
7
|
+
}
|
8
|
+
|
9
|
+
function generateRandomString(length) {
|
10
|
+
// 定义包含所有可能字符的字符串
|
11
|
+
const characters =
|
12
|
+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
13
|
+
let result = '';
|
14
|
+
const charactersLength = characters.length;
|
15
|
+
for (let i = 0; i < length; i++) {
|
16
|
+
// 生成一个0到字符集长度之间的随机索引
|
17
|
+
const randomIndex = Math.floor(Math.random() * charactersLength);
|
18
|
+
// 从字符集中取出对应索引的字符添加到结果字符串中
|
19
|
+
result += characters.charAt(randomIndex);
|
20
|
+
}
|
21
|
+
return result;
|
22
|
+
}
|
23
|
+
|
24
|
+
function translateByGoogle(text, targetLang, appid, privatekey) {
|
25
|
+
const data = {
|
26
|
+
appid,
|
27
|
+
nonce_str: generateRandomString(10),
|
28
|
+
from: 'zh',
|
29
|
+
to: targetLang,
|
30
|
+
text: text,
|
31
|
+
};
|
32
|
+
const translateData = {
|
33
|
+
...data,
|
34
|
+
privatekey,
|
35
|
+
};
|
36
|
+
const stringA = Object.keys(translateData)
|
37
|
+
.sort()
|
38
|
+
.map((key) => `${key}=${translateData[key]}`)
|
39
|
+
.join('&');
|
40
|
+
const token = MD5(stringA).toUpperCase();
|
41
|
+
|
42
|
+
const agent = new https.Agent({
|
43
|
+
rejectUnauthorized: false,
|
44
|
+
});
|
45
|
+
const postData = querystring.stringify({
|
46
|
+
...data,
|
47
|
+
token,
|
48
|
+
});
|
49
|
+
return new Promise((resolve, reject) => {
|
50
|
+
const req = https.request(
|
51
|
+
{
|
52
|
+
hostname: 'aify.zmd.com.cn',
|
53
|
+
path: '/TranslateApi/api/transText',
|
54
|
+
method: 'POST',
|
55
|
+
agent,
|
56
|
+
headers: {
|
57
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
58
|
+
'Content-Length': Buffer.byteLength(postData),
|
59
|
+
},
|
60
|
+
},
|
61
|
+
(res) => {
|
62
|
+
let data = [];
|
63
|
+
|
64
|
+
res.on('data', (chunk) => {
|
65
|
+
data.push(chunk);
|
66
|
+
});
|
67
|
+
|
68
|
+
res.on('end', () => {
|
69
|
+
const buffer = Buffer.concat(data);
|
70
|
+
const json = JSON.parse(buffer.toString());
|
71
|
+
resolve(json.data);
|
72
|
+
});
|
73
|
+
}
|
74
|
+
);
|
75
|
+
|
76
|
+
req.on('error', (error) => {
|
77
|
+
console.error(error);
|
78
|
+
reject(error);
|
79
|
+
});
|
80
|
+
// 写入请求数据
|
81
|
+
req.write(postData);
|
82
|
+
req.end();
|
83
|
+
});
|
84
|
+
}
|
85
|
+
// translateByGoogle('你好', 'en');
|
86
|
+
|
87
|
+
module.exports = translateByGoogle;
|
@@ -0,0 +1,45 @@
|
|
1
|
+
const fs = require('fs');
|
2
|
+
const path = require('path');
|
3
|
+
const defaultOptions = require('./reactOptions');
|
4
|
+
const log = require('./log');
|
5
|
+
|
6
|
+
const cwdPath = process.cwd();
|
7
|
+
|
8
|
+
module.exports = function mergeOptions(programOption, programParameter) {
|
9
|
+
const options = defaultOptions;
|
10
|
+
const configFileName = programOption.config || 'zti18n.config.js';
|
11
|
+
|
12
|
+
const configFilePath = path.join(cwdPath, configFileName);
|
13
|
+
|
14
|
+
// 读取 zti18n-ast.config.js 中设置的参数,然后并入 options
|
15
|
+
if (fs.existsSync(configFilePath)) {
|
16
|
+
let configurationFile = {};
|
17
|
+
try {
|
18
|
+
configurationFile = require(configFilePath);
|
19
|
+
} catch (err) {
|
20
|
+
log.error(`请检查 ${configFileName} 配置文件是否正确\n`);
|
21
|
+
}
|
22
|
+
|
23
|
+
Object.assign(options, configurationFile);
|
24
|
+
} else {
|
25
|
+
log.error(`配置文件 ${configFileName} 不存在\n`);
|
26
|
+
}
|
27
|
+
|
28
|
+
if (!Object.keys(programOption).length) {
|
29
|
+
return options;
|
30
|
+
}
|
31
|
+
|
32
|
+
// 处理命令行参数
|
33
|
+
programParameter.forEach((k) => {
|
34
|
+
const value = programOption[k];
|
35
|
+
if (value) {
|
36
|
+
if (k === 'exclude' && typeof value === 'string') {
|
37
|
+
options[k] = value.split(',');
|
38
|
+
} else {
|
39
|
+
options[k] = value;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
});
|
43
|
+
|
44
|
+
return options;
|
45
|
+
};
|
@@ -0,0 +1,73 @@
|
|
1
|
+
// default options
|
2
|
+
module.exports = {
|
3
|
+
// source path, <string array>
|
4
|
+
// e.g. ['src']
|
5
|
+
entry: ['src'],
|
6
|
+
|
7
|
+
// exclude pattern, <string array>
|
8
|
+
// e.g. ['**/dist/**', '**/*.config.js', '**/*.data.js']
|
9
|
+
exclude: [],
|
10
|
+
|
11
|
+
// output path, <string array>
|
12
|
+
// e.g. ['dist']
|
13
|
+
output: ['src'], // default i18n in place
|
14
|
+
|
15
|
+
// auto translate flag, <boolean>
|
16
|
+
// e.g. true
|
17
|
+
disableAutoTranslate: true,
|
18
|
+
|
19
|
+
// only extract locales, not touch source code, <boolean>
|
20
|
+
// e.g. true
|
21
|
+
extractOnly: false,
|
22
|
+
|
23
|
+
// translator, <null | string>
|
24
|
+
// null: default google translator
|
25
|
+
// e.g. ../translate/google.js
|
26
|
+
translator: null,
|
27
|
+
|
28
|
+
// ignored components, <string array>
|
29
|
+
// e.g. ['EventTracker']
|
30
|
+
ignoreComponents: [],
|
31
|
+
|
32
|
+
// ignored methods, <string array>
|
33
|
+
// e.g. ['MirrorTrack']
|
34
|
+
ignoreMethods: [],
|
35
|
+
|
36
|
+
// XXX: json loose
|
37
|
+
// primaryRegx: /[\u4e00-\u9fa5]/,
|
38
|
+
|
39
|
+
// primary locale, <string>
|
40
|
+
// e.g. 'zh-CN'
|
41
|
+
primaryLocale: 'zh-CN',
|
42
|
+
|
43
|
+
// supported locales, <string array>
|
44
|
+
// e.g. ['zh-CN', 'en-US']
|
45
|
+
supportedLocales: ['zh-CN', 'en-US'],
|
46
|
+
|
47
|
+
// import codes, <string>
|
48
|
+
importCode: "import i18next from 'ztxkutils/dist/i18next';",
|
49
|
+
|
50
|
+
// i18n object, <string>
|
51
|
+
// e.g. 'intl'
|
52
|
+
i18nObject: 'i18next',
|
53
|
+
|
54
|
+
// i18n method, <string>
|
55
|
+
// e.g. 't'
|
56
|
+
i18nMethod: 't',
|
57
|
+
|
58
|
+
// prettier conf, <null | object>
|
59
|
+
// e.g. {}
|
60
|
+
prettier: {
|
61
|
+
singleQuote: true,
|
62
|
+
trailingComma: 'es5',
|
63
|
+
endOfLine: 'lf',
|
64
|
+
},
|
65
|
+
|
66
|
+
// i18n saving conf, <object>
|
67
|
+
// if localeConf.type !== 'file', localeConf.path is required
|
68
|
+
// e.g. { type: 'apollo', path: '../conf/ApolloConf.js', ... }
|
69
|
+
localeConf: {
|
70
|
+
type: 'file',
|
71
|
+
// others...
|
72
|
+
},
|
73
|
+
};
|
@@ -0,0 +1,69 @@
|
|
1
|
+
// default options
|
2
|
+
module.exports = {
|
3
|
+
// source path, <string array>
|
4
|
+
// e.g. ['src']
|
5
|
+
entry: ['src'],
|
6
|
+
|
7
|
+
// exclude pattern, <string array>
|
8
|
+
// e.g. ['**/dist/**', '**/*.config.js', '**/*.data.js']
|
9
|
+
exclude: [],
|
10
|
+
|
11
|
+
// output path, <string array>
|
12
|
+
// e.g. ['dist']
|
13
|
+
output: ['src'], // default i18n in place
|
14
|
+
|
15
|
+
// auto translate flag, <boolean>
|
16
|
+
// e.g. true
|
17
|
+
disableAutoTranslate: true,
|
18
|
+
|
19
|
+
// only extract locales, not touch source code, <boolean>
|
20
|
+
// e.g. true
|
21
|
+
extractOnly: false,
|
22
|
+
|
23
|
+
// translator, <null | string>
|
24
|
+
// null: default google translator
|
25
|
+
// e.g. ../translate/google.js
|
26
|
+
translator: null,
|
27
|
+
|
28
|
+
// ignored components, <string array>
|
29
|
+
// e.g. ['EventTracker']
|
30
|
+
ignoreComponents: [],
|
31
|
+
|
32
|
+
// ignored methods, <string array>
|
33
|
+
// e.g. ['MirrorTrack']
|
34
|
+
ignoreMethods: [],
|
35
|
+
|
36
|
+
// XXX: json loose
|
37
|
+
// primaryRegx: /[\u4e00-\u9fa5]/,
|
38
|
+
|
39
|
+
// primary locale, <string>
|
40
|
+
// e.g. 'zh-CN'
|
41
|
+
primaryLocale: 'zh-CN',
|
42
|
+
|
43
|
+
// supported locales, <string array>
|
44
|
+
// e.g. ['zh-CN', 'en-US']
|
45
|
+
supportedLocales: ['zh-CN', 'en-US'],
|
46
|
+
|
47
|
+
// import codes, <string>
|
48
|
+
importCode: "import { intl } from 'di18n-vue';",
|
49
|
+
|
50
|
+
// i18n object, <string>
|
51
|
+
// e.g. 'intl'
|
52
|
+
i18nObject: 'intl',
|
53
|
+
|
54
|
+
// i18n method, <string>
|
55
|
+
// e.g. '$t'
|
56
|
+
i18nMethod: '$t',
|
57
|
+
|
58
|
+
// prettier conf, <null | object>
|
59
|
+
// e.g. {}
|
60
|
+
prettier: null,
|
61
|
+
|
62
|
+
// i18n saving conf, <object>
|
63
|
+
// if localeConf.type !== 'file', localeConf.path is required
|
64
|
+
// e.g. { type: 'apollo', path: '../conf/ApolloConf.js', ... }
|
65
|
+
localeConf: {
|
66
|
+
type: 'file',
|
67
|
+
// others...
|
68
|
+
},
|
69
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
module.exports = require('./src');
|
@@ -0,0 +1,5 @@
|
|
1
|
+
exports.transformReactIntlToReactIntlUniversal = require('./transform/transformReactIntlToReactIntlUniversal');
|
2
|
+
exports.transformReactIntlUniveralToDi18n = require('./transform/transformReactIntlUniveralToDi18n');
|
3
|
+
exports.transformZeroToDi18n = require('./transform/transformZeroToDi18n');
|
4
|
+
exports.transformToDi18n = require('./transform/transformToDi18n');
|
5
|
+
exports.googleTranslate = require('./translate/google');
|
@@ -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;
|