ztxkutils 2.10.66-22 → 2.10.66-24

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-22",
3
+ "version": "2.10.66-24",
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;