ztxkutils 2.10.66-21 → 2.10.66-23

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztxkutils",
3
- "version": "2.10.66-21",
3
+ "version": "2.10.66-23",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -70,7 +70,6 @@
70
70
  "@babel/types": "^7.18.10",
71
71
  "@prettier/plugin-pug": "^1.8.1",
72
72
  "@typescript-eslint/typescript-estree": "^3.8.0",
73
- "@vitalets/google-translate-api": "^8.0.0",
74
73
  "babel-template": "^6.26.0",
75
74
  "chalk": "^3.0.0",
76
75
  "commander": "^2.18.0",
@@ -24,7 +24,7 @@ async function syncOneNonChineseLocalesConf(
24
24
  confService
25
25
  ) {
26
26
  const confName = key;
27
- const { disableAutoTranslate, translator } = option;
27
+ const { disableAutoTranslate, translator, appid, privatekey } = option;
28
28
 
29
29
  const translate = translator
30
30
  ? require(path.join(process.cwd(), translator))
@@ -58,7 +58,7 @@ async function syncOneNonChineseLocalesConf(
58
58
  const text = mergedLocales[k];
59
59
  let targetLang = key.split(/_|-/)[0];
60
60
  if (isChinese(text)) {
61
- if (disableAutoTranslate) {
61
+ if (disableAutoTranslate || !appid || !privatekey) {
62
62
  mergedLocales[k] = text;
63
63
  } else {
64
64
  translateTasks.push([k, targetLang]);
@@ -78,9 +78,11 @@ async function syncOneNonChineseLocalesConf(
78
78
  tasks.map(([k, targetLang]) => {
79
79
  const text = mergedLocales[k];
80
80
 
81
- return translate(text, targetLang)
81
+ return translate(text, targetLang, appid, privatekey)
82
82
  .then((res) => {
83
- mergedLocales[k] = res.text;
83
+ mergedLocales[k] = res.transResult
84
+ .replaceAll('{ { ', '{{')
85
+ .replaceAll(' } }', '}}');
84
86
  })
85
87
  .catch((err) => {
86
88
  log.warning(`[AutoTranslate]: ${err.message}`);
@@ -1,6 +1,87 @@
1
- // https://github.com/matheuss/google-translate-api/issues/79#issuecomment-427841889
2
- const googleTranslate = require('@vitalets/google-translate-api');
1
+ const crypto = require('crypto');
2
+ const https = require('https');
3
+ const querystring = require('querystring');
3
4
 
4
- module.exports = function translateByGoogle(text, targetLang) {
5
- return googleTranslate(text, { client: 'gtx', to: targetLang });
6
- };
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, privatekey, appid) {
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;
@@ -18,18 +18,18 @@ function getIgnoreLines(ast) {
18
18
  const { type, value, loc } = comment;
19
19
  const last = ignoreBlocks.length - 1;
20
20
 
21
- if (type === 'CommentLine' && value.trim() === 'di18n-disable-line') {
21
+ if (type === 'CommentLine' && value.trim() === 'zti18n-disable-line') {
22
22
  ignoreBlocks.push({
23
23
  start: loc.start.line,
24
24
  end: loc.start.line,
25
25
  });
26
- } else if (type === 'CommentBlock' && value.trim() === 'di18n-disable') {
26
+ } else if (type === 'CommentBlock' && value.trim() === 'zti18n-disable') {
27
27
  if (last < 0 || ignoreBlocks[last].end) {
28
28
  ignoreBlocks.push({
29
29
  start: loc.start.line,
30
30
  });
31
31
  }
32
- } else if (type === 'CommentBlock' && value.trim() === 'di18n-enable') {
32
+ } else if (type === 'CommentBlock' && value.trim() === 'zti18n-enable') {
33
33
  if (last >= 0 && !ignoreBlocks[last].end) {
34
34
  ignoreBlocks[last].end = loc.start.line;
35
35
  }
@@ -5,8 +5,8 @@ module.exports = function getIgnoreLines(tpl) {
5
5
 
6
6
  const lines = tpl.split(/\n|\r\n/g);
7
7
  for (let i = 0; i < lines.length; i++) {
8
- if (lines[i].includes('di18n-disable')) ignoring = true;
9
- if (lines[i].includes('di18n-enable')) ignoring = false;
8
+ if (lines[i].includes('zti18n-disable')) ignoring = true;
9
+ if (lines[i].includes('zti18n-enable')) ignoring = false;
10
10
  if (ignoring) ignores.push(i + 1);
11
11
  }
12
12
 
@@ -1,82 +0,0 @@
1
- /**
2
- * @file 项目基本配置,包括各个环境的请求地址
3
- */
4
- // 环境变量
5
- function getReqUrl(processObj) {
6
- var ENV = processObj.REACT_APP_ENV;
7
- // api服务器各个环境地址
8
- var ZT_API_DEV = processObj.REACT_APP_ZT_API_DEV || window.location.origin; // 开发环境
9
- var ZT_API_TEST = processObj.REACT_APP_ZT_API_TEST || window.location.origin; // 测试环境 https://192.168.0.135:8000
10
- var ZT_API_STAGE = processObj.REACT_APP_ZT_API_STAGE || window.location.origin; // 阶段性环境 http://192.168.0.134:8000
11
- var ZT_API_SIM = processObj.REACT_APP_ZT_API_SIM || window.location.origin; // 阶段性环境 http://192.168.0.134:8000
12
- var ZT_API_PRODUCT = processObj.REACT_APP_ZT_API_PRODUCT || window.location.origin; // 生产环境 http://dz.zmd.com.cn
13
- var ZT_API_PUBLIC_PRODUCT = processObj.REACT_APP_ZT_API_PUBLIC_PRODUCT ||
14
- 'https://nportal.zmd.com.cn:18998'; // 生产环境外网api地址 https://m-portal.zmd.com.cn:18998 http://dz.zmd.com.cn:48000
15
- // 文件服务器
16
- var ZT_FILE_PREVIEW_DEV = processObj.REACT_APP_ZT_FILE_PREVIEW_DEV || 'http://192.168.0.83:88'; // 开发环境
17
- var ZT_FILE_PREVIEW_TEST = processObj.REACT_APP_ZT_FILE_PREVIEW_TEST || 'http://172.55.5.101:33013'; // 测试环境 https://192.168.0.135:18012
18
- var ZT_FILE_PREVIEW_STAGE = processObj.REACT_APP_ZT_FILE_PREVIEW_STAGE ||
19
- 'https://dzfile-prod.zmd.com.cn/'; // 阶段性环境 http://192.168.0.134:8012
20
- var ZT_FILE_PREVIEW_SIM = processObj.REACT_APP_ZT_FILE_PREVIEW_SIM ||
21
- 'https://dzfile-data.zmd.com.cn/'; // 阶段性环境 http://192.168.0.134:8012
22
- var ZT_FILE_PREVIEW_PRODUCT = processObj.REACT_APP_ZT_FILE_PREVIEW_PRODUCT ||
23
- 'https://dzfile.zmd.com.cn:8012'; // 生产环境
24
- // 字体文件附件id
25
- var ZT_FONTFAMILY_DEV = '1551733945007517697';
26
- var ZT_FONTFAMILY_TEST = '1551836774183047169';
27
- var ZT_FONTFAMILY_STAGE = '1551837561144905730';
28
- var ZT_FONTFAMILY_SIM = '1562010976138207234';
29
- var ZT_FONTFAMILY_PRODUCT = '1552851055372992513';
30
- // 实际api请求地址
31
- var ZT_API_BASEURL = ZT_API_DEV;
32
- var ZT_API_PUBLICURL = ZT_API_PUBLIC_PRODUCT;
33
- var ZT_FILE_BASEURL = ZT_FILE_PREVIEW_DEV;
34
- var ZT_FONTFAMILY_BASEURL = ZT_FONTFAMILY_DEV;
35
- if (ENV) {
36
- switch (ENV.toLowerCase()) {
37
- case 'zt-dev':
38
- ZT_API_BASEURL = ZT_API_DEV;
39
- ZT_API_PUBLICURL = ZT_API_DEV;
40
- ZT_FILE_BASEURL = ZT_FILE_PREVIEW_DEV;
41
- ZT_FONTFAMILY_BASEURL = ZT_FONTFAMILY_DEV;
42
- break;
43
- case 'zt-test':
44
- ZT_API_BASEURL = ZT_API_TEST;
45
- ZT_API_PUBLICURL = ZT_API_TEST;
46
- ZT_FILE_BASEURL = ZT_FILE_PREVIEW_TEST;
47
- ZT_FONTFAMILY_BASEURL = ZT_FONTFAMILY_TEST;
48
- break;
49
- case 'zt-stage':
50
- ZT_API_BASEURL = ZT_API_STAGE;
51
- ZT_API_PUBLICURL = ZT_API_STAGE;
52
- ZT_FILE_BASEURL = ZT_FILE_PREVIEW_STAGE;
53
- ZT_FONTFAMILY_BASEURL = ZT_FONTFAMILY_STAGE;
54
- break;
55
- case 'zt-sim':
56
- ZT_API_BASEURL = ZT_API_SIM;
57
- ZT_API_PUBLICURL = ZT_API_SIM;
58
- ZT_FILE_BASEURL = ZT_FILE_PREVIEW_SIM;
59
- ZT_FONTFAMILY_BASEURL = ZT_FONTFAMILY_SIM;
60
- break;
61
- case 'zt-product':
62
- ZT_API_BASEURL = ZT_API_PRODUCT;
63
- ZT_API_PUBLICURL = ZT_API_PUBLIC_PRODUCT;
64
- ZT_FILE_BASEURL = ZT_FILE_PREVIEW_PRODUCT;
65
- ZT_FONTFAMILY_BASEURL = ZT_FONTFAMILY_PRODUCT;
66
- break;
67
- }
68
- }
69
- return {
70
- ZT_API_BASEURL: ZT_API_BASEURL,
71
- ZT_FILE_BASEURL: ZT_FILE_BASEURL,
72
- ZT_API_PUBLICURL: ZT_API_PUBLICURL,
73
- ZT_FONTFAMILY_BASEURL: ZT_FONTFAMILY_BASEURL,
74
- };
75
- }
76
-
77
- var reqUrl = /*#__PURE__*/Object.freeze({
78
- __proto__: null,
79
- getReqUrl: getReqUrl
80
- });
81
-
82
- export { getReqUrl as g, reqUrl as r };