ztxkutils 10.0.8 → 20.0.1

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.
Files changed (63) hide show
  1. package/dist/businessTools.js +4 -3
  2. package/dist/dataModel-6c68c88f.js +26 -0
  3. package/dist/dataModel-914b6226.js +26 -0
  4. package/dist/dataModel.js +2 -1
  5. package/dist/fileOperation.d.ts +19 -0
  6. package/dist/fileOperation.js +74 -10
  7. package/dist/hooks.js +36 -22
  8. package/dist/i18next.js +21 -0
  9. package/dist/index.js +5 -4
  10. package/dist/myIndexDb.d.ts +1 -1
  11. package/dist/print.d.ts +4 -5
  12. package/dist/print.js +4 -3
  13. package/dist/reqUrl.js +1 -1
  14. package/dist/request-1e442d5d.js +2982 -0
  15. package/dist/request-986d7090.js +2923 -0
  16. package/dist/request-d8d72b87.js +2982 -0
  17. package/dist/request-f600ad7a.js +2992 -0
  18. package/dist/request.d.ts +13 -0
  19. package/dist/request.js +2 -1
  20. package/dist/stompClient.js +21 -8
  21. package/dist/useFileIdToBase64.js +2 -0
  22. package/dist/validate-21164759.js +260 -0
  23. package/dist/validate-2de5a28f.js +260 -0
  24. package/dist/validate.js +2 -1
  25. package/dist/workflow.js +3 -5
  26. package/locales/en-US.json +64 -0
  27. package/locales/zh-CN.json +64 -0
  28. package/package.json +41 -4
  29. package/zti18n-cli/bin/index.js +3 -0
  30. package/zti18n-cli/index.js +23 -0
  31. package/zti18n-cli/src/command/collect.js +353 -0
  32. package/zti18n-cli/src/command/convert.js +17 -0
  33. package/zti18n-cli/src/command/convert2.js +35 -0
  34. package/zti18n-cli/src/command/initFileConf.js +133 -0
  35. package/zti18n-cli/src/command/publish.js +24 -0
  36. package/zti18n-cli/src/conf/BaseConf.js +21 -0
  37. package/zti18n-cli/src/conf/FileConf.js +116 -0
  38. package/zti18n-cli/src/index.js +75 -0
  39. package/zti18n-cli/src/translate/google.js +87 -0
  40. package/zti18n-cli/src/utils/isChinese.js +3 -0
  41. package/zti18n-cli/src/utils/log.js +8 -0
  42. package/zti18n-cli/src/utils/mergeOptions.js +45 -0
  43. package/zti18n-cli/src/utils/reactOptions.js +73 -0
  44. package/zti18n-cli/src/utils/vueOptions.js +69 -0
  45. package/zti18n-core/index.js +1 -0
  46. package/zti18n-core/src/index.js +5 -0
  47. package/zti18n-core/src/plugin/reactIntlToReactIntlUniversal.js +224 -0
  48. package/zti18n-core/src/plugin/reactIntlUniversalToDi18n.js +64 -0
  49. package/zti18n-core/src/transform/defaultPkMap.js +79 -0
  50. package/zti18n-core/src/transform/transformHtml.js +271 -0
  51. package/zti18n-core/src/transform/transformJs.js +489 -0
  52. package/zti18n-core/src/transform/transformPug.js +272 -0
  53. package/zti18n-core/src/transform/transformReactIntlToReactIntlUniversal.js +96 -0
  54. package/zti18n-core/src/transform/transformReactIntlUniveralToDi18n.js +90 -0
  55. package/zti18n-core/src/transform/transformToDi18n.js +22 -0
  56. package/zti18n-core/src/transform/transformTs.js +41 -0
  57. package/zti18n-core/src/transform/transformVue.js +126 -0
  58. package/zti18n-core/src/transform/transformZeroToDi18n.js +105 -0
  59. package/zti18n-core/src/translate/google.js +6 -0
  60. package/zti18n-core/src/utils/constants.js +3 -0
  61. package/zti18n-core/src/utils/getIgnoreLines.js +14 -0
  62. package/zti18n-core/src/utils/isChinese.js +3 -0
  63. package/zti18n-core/src/utils/log.js +8 -0
@@ -0,0 +1,64 @@
1
+ {
2
+ "拦截重复请求": "拦截重复请求",
3
+ "切换将清空已选数据!": "切换将清空已选数据!",
4
+ "确认": "确认",
5
+ "取消": "取消",
6
+ "数据请求成功": "数据请求成功",
7
+ "数据请求失败": "数据请求失败",
8
+ "必须传入获取Ws长连接的方法": "必须传入获取Ws长连接的方法",
9
+ "获取ws链接地址失败!": "获取ws链接地址失败!",
10
+ "获取ws地址失败!": "获取ws地址失败!",
11
+ "浏览器不支持WebSocket": "浏览器不支持WebSocket",
12
+ "连接建立": "连接建立",
13
+ "识别结果:": "识别结果:",
14
+ "ws连接出错了!": "ws连接出错了!",
15
+ "连接异常": "连接异常",
16
+ "连接关闭": "连接关闭",
17
+ "你没有引入路由相关API,请引入!": "你没有引入路由相关API,请引入!",
18
+ "握手成功": "握手成功",
19
+ "收到讯飞的语音提示": "收到讯飞的语音提示",
20
+ "出错了:": "出错了:",
21
+ "连接出错了": "连接出错了",
22
+ "收到录音的最后帧!": "收到录音的最后帧!",
23
+ "你没有传入 获取指令的接口,这将导致错误!": "你没有传入 获取指令的接口,这将导致错误!",
24
+ "不支持处理该指令,请重说!": "不支持处理该指令,请重说!",
25
+ "{{slot0}} 第{{slot1}}行{{title}} {{slot2}}": "{{slot0}} 第{{slot1}}行{{title}} {{slot2}}",
26
+ "{{slot0}} 第{{slot1}}行{{title}}为空!": "{{slot0}} 第{{slot1}}行{{title}}为空!",
27
+ "中文": "中文",
28
+ "英文": "英文",
29
+ "数据库打开成功": "数据库打开成功",
30
+ "数据库打开报错!": "数据库打开报错!",
31
+ "浙商中拓": "浙商中拓",
32
+ "{{slot0}} 微软雅黑": "{{slot0}} 微软雅黑",
33
+ "刷新token遭到频繁调用,请联系管理员查看是否接口权限出现问题,可能是{{slot0}}接口出现问题": "刷新token遭到频繁调用,请联系管理员查看是否接口权限出现问题,可能是{{slot0}}接口出现问题",
34
+ "token已经刷新成功,请重新提交!": "token已经刷新成功,请重新提交!",
35
+ "登录过期!": "登录过期!",
36
+ "请求超时,请稍后再试,请去列表确认数据是否插入成功,请勿重复提交! 接口地址:{{slot0}} 请求方式:{{slot1}} 请求超时时间:{{slot2}}": "请求超时,请稍后再试,请去列表确认数据是否插入成功,请勿重复提交! 接口地址:{{slot0}} 请求方式:{{slot1}} 请求超时时间:{{slot2}}",
37
+ "接口: {{slot0}} 未找到!": "接口: {{slot0}} 未找到!",
38
+ "你的账号已在其他地方登录!": "你的账号已在其他地方登录!",
39
+ "连接已发起,但是10秒后未成功建立连接,手动断开重连!system={{slot0}}": "连接已发起,但是10秒后未成功建立连接,手动断开重连!system={{slot0}}",
40
+ "---连接未建立的关闭出错---": "---连接未建立的关闭出错---",
41
+ "---第{{slot0}}次重连!time={{slot1}}system={{slot2}}---": "---第{{slot0}}次重连!time={{slot1}}system={{slot2}}---",
42
+ "---------连接成功 time={{slot0}} system={{slot1}} time={{slot2}}--------": "---------连接成功 time={{slot0}} system={{slot1}} time={{slot2}}--------",
43
+ "---------连接失败 system={{slot0}} time={{slot1}}--------": "---------连接失败 system={{slot0}} time={{slot1}}--------",
44
+ "用户主动关闭": "用户主动关闭",
45
+ "---连接已建立的关闭出错 time={{slot0}}system={{slot1}}---": "---连接已建立的关闭出错 time={{slot0}}system={{slot1}}---",
46
+ "---第{{slot0}}次重连!time={{slot1}}system={{slot2}}": "---第{{slot0}}次重连!time={{slot1}}system={{slot2}}",
47
+ "{{type}}超过最大字符长度200": "{{type}}超过最大字符长度200",
48
+ "{{type}}超过最大字符长度400": "{{type}}超过最大字符长度400",
49
+ "{{type}}超过最大字符长度600": "{{type}}超过最大字符长度600",
50
+ "{{type}}超过最大字符长度5000": "{{type}}超过最大字符长度5000",
51
+ "请输入正确的手机号": "请输入正确的手机号",
52
+ "请输入正确的身份证": "请输入正确的身份证",
53
+ "请输入正确的邮箱": "请输入正确的邮箱",
54
+ "密码必须包含大小写字母、数字、特殊字符且最小长度为8,且不能出现连续数字或字符,不能出现相同字符,且禁止使用用户账号作为密码": "密码必须包含大小写字母、数字、特殊字符且最小长度为8,且不能出现连续数字或字符,不能出现相同字符,且禁止使用用户账号作为密码",
55
+ "请勿输入空格": "请勿输入空格",
56
+ "密码中必须包含大小写字母、数字、特殊字符,且长度必须大于8个字符!": "密码中必须包含大小写字母、数字、特殊字符,且长度必须大于8个字符!",
57
+ "密码与用户名相同!": "密码与用户名相同!",
58
+ "密码中有相同且连续超过三次的字符!": "密码中有相同且连续超过三次的字符!",
59
+ "从第{{slot0}}个字符开始,有连续{{slot1}}超过三位的字母或数字!": "从第{{slot0}}个字符开始,有连续{{slot1}}超过三位的字母或数字!",
60
+ "从第{{slot0}}个字符开始,有按键盘横向连续{{slot1}}超过三位的字母!": "从第{{slot0}}个字符开始,有按键盘横向连续{{slot1}}超过三位的字母!",
61
+ "从第{{slot0}}个字符开始,有按键盘竖向连续{{slot1}}超过三位的字母或数字!": "从第{{slot0}}个字符开始,有按键盘竖向连续{{slot1}}超过三位的字母或数字!",
62
+ "父页面传递过来的消息": "父页面传递过来的消息",
63
+ "父页面传递过来的消息解析报错": "父页面传递过来的消息解析报错"
64
+ }
@@ -0,0 +1,64 @@
1
+ {
2
+ "拦截重复请求": "拦截重复请求",
3
+ "切换将清空已选数据!": "切换将清空已选数据!",
4
+ "确认": "确认",
5
+ "取消": "取消",
6
+ "数据请求成功": "数据请求成功",
7
+ "数据请求失败": "数据请求失败",
8
+ "必须传入获取Ws长连接的方法": "必须传入获取Ws长连接的方法",
9
+ "获取ws链接地址失败!": "获取ws链接地址失败!",
10
+ "获取ws地址失败!": "获取ws地址失败!",
11
+ "浏览器不支持WebSocket": "浏览器不支持WebSocket",
12
+ "连接建立": "连接建立",
13
+ "识别结果:": "识别结果:",
14
+ "ws连接出错了!": "ws连接出错了!",
15
+ "连接异常": "连接异常",
16
+ "连接关闭": "连接关闭",
17
+ "你没有引入路由相关API,请引入!": "你没有引入路由相关API,请引入!",
18
+ "握手成功": "握手成功",
19
+ "收到讯飞的语音提示": "收到讯飞的语音提示",
20
+ "出错了:": "出错了:",
21
+ "连接出错了": "连接出错了",
22
+ "收到录音的最后帧!": "收到录音的最后帧!",
23
+ "你没有传入 获取指令的接口,这将导致错误!": "你没有传入 获取指令的接口,这将导致错误!",
24
+ "不支持处理该指令,请重说!": "不支持处理该指令,请重说!",
25
+ "{{slot0}} 第{{slot1}}行{{title}} {{slot2}}": "{{slot0}} 第{{slot1}}行{{title}} {{slot2}}",
26
+ "{{slot0}} 第{{slot1}}行{{title}}为空!": "{{slot0}} 第{{slot1}}行{{title}}为空!",
27
+ "中文": "中文",
28
+ "英文": "英文",
29
+ "数据库打开成功": "数据库打开成功",
30
+ "数据库打开报错!": "数据库打开报错!",
31
+ "浙商中拓": "浙商中拓",
32
+ "{{slot0}} 微软雅黑": "{{slot0}} 微软雅黑",
33
+ "刷新token遭到频繁调用,请联系管理员查看是否接口权限出现问题,可能是{{slot0}}接口出现问题": "刷新token遭到频繁调用,请联系管理员查看是否接口权限出现问题,可能是{{slot0}}接口出现问题",
34
+ "token已经刷新成功,请重新提交!": "token已经刷新成功,请重新提交!",
35
+ "登录过期!": "登录过期!",
36
+ "请求超时,请稍后再试,请去列表确认数据是否插入成功,请勿重复提交! 接口地址:{{slot0}} 请求方式:{{slot1}} 请求超时时间:{{slot2}}": "请求超时,请稍后再试,请去列表确认数据是否插入成功,请勿重复提交! 接口地址:{{slot0}} 请求方式:{{slot1}} 请求超时时间:{{slot2}}",
37
+ "接口: {{slot0}} 未找到!": "接口: {{slot0}} 未找到!",
38
+ "你的账号已在其他地方登录!": "你的账号已在其他地方登录!",
39
+ "连接已发起,但是10秒后未成功建立连接,手动断开重连!system={{slot0}}": "连接已发起,但是10秒后未成功建立连接,手动断开重连!system={{slot0}}",
40
+ "---连接未建立的关闭出错---": "---连接未建立的关闭出错---",
41
+ "---第{{slot0}}次重连!time={{slot1}}system={{slot2}}---": "---第{{slot0}}次重连!time={{slot1}}system={{slot2}}---",
42
+ "---------连接成功 time={{slot0}} system={{slot1}} time={{slot2}}--------": "---------连接成功 time={{slot0}} system={{slot1}} time={{slot2}}--------",
43
+ "---------连接失败 system={{slot0}} time={{slot1}}--------": "---------连接失败 system={{slot0}} time={{slot1}}--------",
44
+ "用户主动关闭": "用户主动关闭",
45
+ "---连接已建立的关闭出错 time={{slot0}}system={{slot1}}---": "---连接已建立的关闭出错 time={{slot0}}system={{slot1}}---",
46
+ "---第{{slot0}}次重连!time={{slot1}}system={{slot2}}": "---第{{slot0}}次重连!time={{slot1}}system={{slot2}}",
47
+ "{{type}}超过最大字符长度200": "{{type}}超过最大字符长度200",
48
+ "{{type}}超过最大字符长度400": "{{type}}超过最大字符长度400",
49
+ "{{type}}超过最大字符长度600": "{{type}}超过最大字符长度600",
50
+ "{{type}}超过最大字符长度5000": "{{type}}超过最大字符长度5000",
51
+ "请输入正确的手机号": "请输入正确的手机号",
52
+ "请输入正确的身份证": "请输入正确的身份证",
53
+ "请输入正确的邮箱": "请输入正确的邮箱",
54
+ "密码必须包含大小写字母、数字、特殊字符且最小长度为8,且不能出现连续数字或字符,不能出现相同字符,且禁止使用用户账号作为密码": "密码必须包含大小写字母、数字、特殊字符且最小长度为8,且不能出现连续数字或字符,不能出现相同字符,且禁止使用用户账号作为密码",
55
+ "请勿输入空格": "请勿输入空格",
56
+ "密码中必须包含大小写字母、数字、特殊字符,且长度必须大于8个字符!": "密码中必须包含大小写字母、数字、特殊字符,且长度必须大于8个字符!",
57
+ "密码与用户名相同!": "密码与用户名相同!",
58
+ "密码中有相同且连续超过三次的字符!": "密码中有相同且连续超过三次的字符!",
59
+ "从第{{slot0}}个字符开始,有连续{{slot1}}超过三位的字母或数字!": "从第{{slot0}}个字符开始,有连续{{slot1}}超过三位的字母或数字!",
60
+ "从第{{slot0}}个字符开始,有按键盘横向连续{{slot1}}超过三位的字母!": "从第{{slot0}}个字符开始,有按键盘横向连续{{slot1}}超过三位的字母!",
61
+ "从第{{slot0}}个字符开始,有按键盘竖向连续{{slot1}}超过三位的字母或数字!": "从第{{slot0}}个字符开始,有按键盘竖向连续{{slot1}}超过三位的字母或数字!",
62
+ "父页面传递过来的消息": "父页面传递过来的消息",
63
+ "父页面传递过来的消息解析报错": "父页面传递过来的消息解析报错"
64
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztxkutils",
3
- "version": "10.0.8",
3
+ "version": "20.0.1",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -10,11 +10,17 @@
10
10
  "prepublish": "yarn build"
11
11
  },
12
12
  "files": [
13
- "dist"
13
+ "dist",
14
+ "./zti18n-cli",
15
+ "./zti18n-core",
16
+ "./locales"
14
17
  ],
15
18
  "keywords": [],
16
19
  "author": "ZTXK",
17
20
  "license": "MIT",
21
+ "bin": {
22
+ "zti18n": "./zti18n-cli/bin/index.js"
23
+ },
18
24
  "devDependencies": {
19
25
  "@rollup/plugin-commonjs": "^19.0.0",
20
26
  "@rollup/plugin-json": "^4.1.0",
@@ -47,14 +53,45 @@
47
53
  "ztxkui": "^0.4.1"
48
54
  },
49
55
  "dependencies": {
56
+ "@babel/core": "^7.18.10",
57
+ "@babel/generator": "^7.18.0",
58
+ "@babel/plugin-proposal-optional-chaining": "^7.18.9",
59
+ "@babel/plugin-syntax-async-generators": "^7.8.4",
60
+ "@babel/plugin-syntax-class-properties": "^7.12.13",
61
+ "@babel/plugin-syntax-decorators": "^7.18.6",
62
+ "@babel/plugin-syntax-do-expressions": "^7.18.6",
63
+ "@babel/plugin-syntax-dynamic-import": "^7.8.3",
64
+ "@babel/plugin-syntax-flow": "^7.18.6",
65
+ "@babel/plugin-syntax-function-bind": "^7.18.6",
66
+ "@babel/plugin-syntax-function-sent": "^7.18.6",
67
+ "@babel/plugin-syntax-jsx": "^7.18.6",
68
+ "@babel/plugin-syntax-object-rest-spread": "^7.8.3",
69
+ "@babel/preset-typescript": "^7.18.6",
70
+ "@babel/traverse": "^7.18.10",
71
+ "@babel/types": "^7.18.10",
72
+ "@prettier/plugin-pug": "^1.8.1",
73
+ "@typescript-eslint/typescript-estree": "^3.8.0",
74
+ "babel-template": "^6.26.0",
75
+ "chalk": "^3.0.0",
76
+ "commander": "^2.18.0",
50
77
  "crypto-js": "^4.1.1",
51
78
  "decimal.js-light": "^2.5.1",
52
79
  "dexie": "^3.2.3",
80
+ "di18n-core": "^0.1.29",
81
+ "glob": "^7.1.6",
82
+ "i18next": "^23.16.4",
83
+ "inquirer": "^6.2.2",
53
84
  "js-base64": "^3.6.0",
54
85
  "jwt-decode": "^3.1.2",
86
+ "mustache": "^4.0.1",
87
+ "parse5": "^5.1.1",
88
+ "pug-lexer": "^4.1.0",
89
+ "pug-parser": "^5.0.1",
90
+ "pug-source-gen": "0.0.2",
55
91
  "screenfull": "^5.1.0",
56
92
  "sockjs-client": "^1.5.1",
57
93
  "stompjs": "^2.3.3",
94
+ "vue-template-compiler": "^2.7.16",
58
95
  "zt-sockjs-client": "^0.0.2",
59
96
  "zt-stompjs": "^1.1.2"
60
97
  },
@@ -68,8 +105,8 @@
68
105
  "react": ">=17.0.1",
69
106
  "react-dom": ">=17.0.1",
70
107
  "react-router-dom": ">=5.2.0",
71
- "ztxkui": ">=0.4.1",
72
108
  "zt-sockjs-client": ">=0.0.1",
73
- "zt-stompjs": ">=1.0.3"
109
+ "zt-stompjs": ">=1.0.3",
110
+ "ztxkui": ">=0.4.1"
74
111
  }
75
112
  }
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../');
@@ -0,0 +1,23 @@
1
+ const program = require('./src');
2
+ const initFileConf = require('./src/command/initFileConf');
3
+
4
+ program
5
+ .command('init')
6
+ .alias('i')
7
+ .description('init locales conf')
8
+ .option('--vue', 'init for vue project')
9
+ .action(function (options) {
10
+ initFileConf(options.vue);
11
+ })
12
+ .on('--help', function () {
13
+ console.log(' Examples:');
14
+ console.log();
15
+ console.log(' $ zti18n init -c ./config/prod.config.js');
16
+ console.log();
17
+ });
18
+
19
+ program.command('*').action(function (cmd) {
20
+ console.log('unknown command "%s"', cmd);
21
+ });
22
+
23
+ program.parse(process.argv);
@@ -0,0 +1,353 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const glob = require('glob');
4
+ const { transformZeroToDi18n } = require('../../../zti18n-core');
5
+ const mergeOptions = require('../utils/mergeOptions');
6
+ const isChinese = require('../utils/isChinese');
7
+ const log = require('../utils/log');
8
+
9
+ function getSourceFiles({ entry, exclude }) {
10
+ return glob.sync(`${entry}/**/*.{js,ts,tsx,jsx,vue}`, {
11
+ ignore: exclude || [],
12
+ });
13
+ }
14
+
15
+ /**
16
+ * 同步一个非中文语言的配置
17
+ */
18
+ async function syncOneNonChineseLocalesConf(
19
+ key,
20
+ option,
21
+ chineseLocales,
22
+ updatedTranslatedWord,
23
+ publish,
24
+ confService
25
+ ) {
26
+ const confName = key;
27
+ const { disableAutoTranslate, translator, appid, privatekey } = option;
28
+
29
+ const translate = translator
30
+ ? require(path.join(process.cwd(), translator))
31
+ : require('../translate/google');
32
+
33
+ log.warning(`start to update conf ${confName}`);
34
+
35
+ const res = await confService.getConf(confName, key);
36
+
37
+ if (res.code !== 0) {
38
+ log.error(`! failed to read apollo conf, ${res.message}`);
39
+ return null;
40
+ }
41
+
42
+ const allKeys = Object.keys(chineseLocales);
43
+ let mergedLocales = {};
44
+
45
+ res.data.forEach((item) => {
46
+ if (updatedTranslatedWord.hasOwnProperty(item.key)) {
47
+ mergedLocales[updatedTranslatedWord[item.key]] = item.value;
48
+ } else if (allKeys.indexOf(item.key) >= 0) {
49
+ mergedLocales[item.key] = item.value;
50
+ }
51
+ });
52
+
53
+ mergedLocales = { ...chineseLocales, ...mergedLocales };
54
+
55
+ const translateTasks = [];
56
+
57
+ Object.keys(mergedLocales).forEach((k) => {
58
+ const text = mergedLocales[k];
59
+ let targetLang = key.split(/_|-/)[0];
60
+ if (isChinese(text)) {
61
+ if (disableAutoTranslate || !appid || !privatekey) {
62
+ mergedLocales[k] = text;
63
+ } else {
64
+ translateTasks.push([k, targetLang]);
65
+ }
66
+ }
67
+ });
68
+
69
+ const total = translateTasks.length;
70
+
71
+ // 控制并发数
72
+ const concurrenceCount = 3;
73
+
74
+ while (translateTasks.length > 0) {
75
+ const tasks = translateTasks.splice(0, concurrenceCount);
76
+
77
+ await Promise.all(
78
+ tasks.map(([k, targetLang]) => {
79
+ const text = mergedLocales[k];
80
+
81
+ return translate(text, targetLang, appid, privatekey)
82
+ .then((res) => {
83
+ mergedLocales[k] = res.transResult
84
+ .replaceAll('{ { ', '{{')
85
+ .replaceAll(' } }', '}}');
86
+ })
87
+ .catch((err) => {
88
+ log.warning(`[AutoTranslate]: ${err.message}`);
89
+ mergedLocales[k] = text;
90
+ });
91
+ })
92
+ );
93
+
94
+ log.info(`[AutoTranslate]: ${total - translateTasks.length}/${total}`);
95
+ }
96
+
97
+ const updateConfRes = await confService.updateConf(
98
+ confName,
99
+ mergedLocales,
100
+ chineseLocales,
101
+ key
102
+ );
103
+ if (updateConfRes.code !== 0) {
104
+ log.error(`update conf ${confName} fail, ${res.message}`);
105
+ return [];
106
+ }
107
+
108
+ log.success(`updated conf ${confName}`);
109
+
110
+ if (publish) {
111
+ log.warning(`start to publish conf ${confName}`);
112
+ const publishRes = await confService.publishConf(confName);
113
+ if (publishRes.code === 0) {
114
+ log.success(`published conf ${confName}`);
115
+ } else {
116
+ log.error(`publish conf ${confName} failed, ${publishRes.message}`);
117
+ }
118
+ } else {
119
+ log.warning(`${confName} is updated but unpublished!`);
120
+ }
121
+
122
+ return {
123
+ key,
124
+ locales: mergedLocales,
125
+ };
126
+ }
127
+
128
+ /**
129
+ * 同步更新非中文配置
130
+ * @param {object} chineseLocales 中文资源
131
+ * @param {object} updatedTranslatedWord 在远端做过中文文案修改的 key/value
132
+ * @param {object} option 参数
133
+ * @param {boolean} publish 更新后是否立即发布配置
134
+ */
135
+ async function syncNonChineseLocalesConf(
136
+ chineseLocales,
137
+ updatedTranslatedWord,
138
+ option,
139
+ publish,
140
+ confService
141
+ ) {
142
+ const { primaryLocale, supportedLocales } = option;
143
+
144
+ const syncTasks = supportedLocales
145
+ .filter((key) => key !== primaryLocale)
146
+ .map((key) =>
147
+ syncOneNonChineseLocalesConf(
148
+ key,
149
+ option,
150
+ chineseLocales,
151
+ updatedTranslatedWord,
152
+ publish,
153
+ confService
154
+ )
155
+ );
156
+
157
+ const nonChineseLocales = await Promise.all(syncTasks);
158
+
159
+ return nonChineseLocales;
160
+ }
161
+
162
+ /**
163
+ * 收集源码中的中文字符
164
+ * @param {object} option 命令行或者配置文件中传入的参数信息
165
+ * @param {boolean} publish 同步后,是否立即发布新的配置
166
+ */
167
+ async function collectChineseWords(option, publish, confService) {
168
+ const { entry, output, exclude, entryNoImport = [] } = option;
169
+
170
+ if (!Array.isArray(entry) && typeof entry !== 'string') {
171
+ log.error('entry must be a string or array');
172
+ process.exit(2);
173
+ }
174
+
175
+ let zhData = {};
176
+ const allTranslatedWord = {};
177
+ const updatedTranslatedWord = {}; // 在远端做过中文文案修改的 key/value
178
+ const keysInUse = [];
179
+
180
+ const zhConfName = 'zh-CN';
181
+
182
+ const res = await confService.getConf(zhConfName, 'zh-CN');
183
+
184
+ if (res.code !== 0) {
185
+ log.error(`failed to read apollo conf, ${res.message}`);
186
+ process.exit(2);
187
+ }
188
+
189
+ res.data.forEach((item) => {
190
+ zhData[item.key] = item.value;
191
+ });
192
+
193
+ Object.keys(zhData).forEach((k) => {
194
+ const chineseValue = zhData[k];
195
+ if (chineseValue.trim() !== k.replace(/\{context,\s*\S+\}$/, '').trim()) {
196
+ updatedTranslatedWord[k] = chineseValue;
197
+ } else {
198
+ if (Array.isArray(allTranslatedWord[chineseValue])) {
199
+ allTranslatedWord[chineseValue].push(k);
200
+ } else {
201
+ allTranslatedWord[chineseValue] = [k];
202
+ }
203
+ }
204
+ });
205
+ const outputs = output ? [].concat(output) : [];
206
+ let targetFiles = [].concat(entry).reduce((prev, cur, index) => {
207
+ const files = getSourceFiles({ entry: cur, exclude }).map((file) => ({
208
+ filePath: file,
209
+ currentEntry: cur,
210
+ currentOutput: outputs[index],
211
+ ext: path.extname(file),
212
+ }));
213
+ return prev.concat(files);
214
+ }, []);
215
+ targetFiles = targetFiles.concat(
216
+ entryNoImport.reduce((prev, cur, index) => {
217
+ const files = getSourceFiles({ entry: cur, exclude: [] }).map((file) => ({
218
+ filePath: file,
219
+ currentEntry: cur,
220
+ ext: path.extname(file),
221
+ noImport: true,
222
+ }));
223
+ return prev.concat(files);
224
+ }, [])
225
+ );
226
+
227
+ targetFiles.forEach((codeFileInfo) => {
228
+ transformZeroToDi18n(
229
+ codeFileInfo,
230
+ allTranslatedWord,
231
+ updatedTranslatedWord,
232
+ keysInUse,
233
+ option
234
+ );
235
+ log.success(`done: ${codeFileInfo.filePath}`);
236
+ });
237
+
238
+ const localeResouce = {};
239
+ const unusedKeys = [];
240
+ Object.keys(allTranslatedWord).forEach((k) => {
241
+ allTranslatedWord[k].forEach((key) => {
242
+ if (keysInUse.includes(key)) {
243
+ localeResouce[key] = k;
244
+ } else {
245
+ unusedKeys.push(key);
246
+ }
247
+ });
248
+ });
249
+
250
+ if (unusedKeys.length > 0) {
251
+ log.info('the following keys are unused and will be removed:');
252
+ log.info(unusedKeys);
253
+ }
254
+
255
+ const allTranslatedWordKeysCount = Object.keys(allTranslatedWord).reduce(
256
+ (prev, cur) => prev + allTranslatedWord[cur].length,
257
+ 0
258
+ );
259
+ const allLocalesKeyCount = Object.keys(localeResouce).length;
260
+ const allTranstedInUseCount = allTranslatedWordKeysCount - unusedKeys.length;
261
+
262
+ log.info(`keys in use count: ${allTranstedInUseCount}`);
263
+ log.info(`locales in use count: ${allLocalesKeyCount}`);
264
+
265
+ if (allTranstedInUseCount !== allLocalesKeyCount) {
266
+ log.error('ATTENTION:keys and locales are not match');
267
+ }
268
+
269
+ log.info(`start to update conf ${zhConfName}`);
270
+ // 更新配置
271
+ const updateConfRes = await confService.updateConf(
272
+ zhConfName,
273
+ localeResouce,
274
+ {},
275
+ 'zh-CN'
276
+ );
277
+
278
+ if (updateConfRes.code !== 0) {
279
+ log.error(`update conf ${zhConfName} failed, ${updateConfRes.message}`);
280
+ return null;
281
+ }
282
+
283
+ log.success(`updated conf ${zhConfName}`);
284
+
285
+ const unChineseLocales = await syncNonChineseLocalesConf(
286
+ localeResouce,
287
+ updatedTranslatedWord,
288
+ option,
289
+ publish,
290
+ confService
291
+ );
292
+
293
+ // XXX: 英文 publish 也放到这里
294
+ if (publish) {
295
+ log.warning(`start to publish conf ${zhConfName}`);
296
+ const publishRes = await confService.publishConf(zhConfName);
297
+ if (publishRes.code === 0) {
298
+ log.success(`published conf ${zhConfName}`);
299
+ } else {
300
+ log.error(`publish conf ${zhConfName} failed, ${publishRes.message}`);
301
+ }
302
+ } else {
303
+ log.warning(`${zhConfName} is synced, but unpublihsed !!`);
304
+ }
305
+
306
+ return unChineseLocales.concat({
307
+ key: 'zh-CN',
308
+ locales: localeResouce,
309
+ });
310
+ }
311
+
312
+ module.exports = function doCollectChineseWords(
313
+ programOption,
314
+ publish = false
315
+ ) {
316
+ const options = mergeOptions(programOption, ['entry', 'exclude', 'locales']);
317
+
318
+ let confService = null;
319
+ if (options.localeConf.type === 'file') {
320
+ const FileConf = require('../conf/FileConf');
321
+ confService = new FileConf(options.localeConf.folder);
322
+ } else {
323
+ const Conf = require(options.localeConf.path);
324
+ confService = new Conf(options.localeConf);
325
+ }
326
+
327
+ if (!options.entry) {
328
+ log.error('no entry is specified');
329
+ process.exit(2);
330
+ }
331
+
332
+ if (Array.isArray(options.entry)) {
333
+ if (!Array.isArray(options.output)) {
334
+ log.error('output should be array too, since entry is array');
335
+ process.exit(2);
336
+ }
337
+
338
+ if (options.output.length !== options.entry.length) {
339
+ log.error('output length are not equal to entry length');
340
+ process.exit(2);
341
+ }
342
+ }
343
+
344
+ if (options.output) {
345
+ [].concat(options.output).forEach((outputDir) => {
346
+ if (!fs.existsSync(outputDir)) {
347
+ fs.mkdirSync(outputDir);
348
+ }
349
+ });
350
+ }
351
+
352
+ return collectChineseWords(options, publish, confService);
353
+ };
@@ -0,0 +1,17 @@
1
+ const {
2
+ transformReactIntlToReactIntlUniversal,
3
+ } = require('../../../zti18n-core');
4
+ const mergeOptions = require('../utils/mergeOptions');
5
+ const log = require('../utils/log');
6
+
7
+ module.exports = function doConvertToReactIntlUniversal(programOption) {
8
+ const options = mergeOptions(programOption, ['entry', 'exclude', 'locales']);
9
+
10
+ if (!options.entry) {
11
+ log.error('\n · 使用 -e [path] 或 --entry [path] 命令增加待转换的源码目录');
12
+ log.error(" · 或在配置文件中设置 'entry' 字段\n");
13
+ process.exit(2);
14
+ }
15
+
16
+ transformReactIntlToReactIntlUniversal(options);
17
+ };
@@ -0,0 +1,35 @@
1
+ const fs = require('fs');
2
+ const { transformReactIntlUniveralToDi18n } = require('../../../zti18n-core');
3
+ const mergeOptions = require('../utils/mergeOptions');
4
+ const log = require('../utils/log');
5
+
6
+ module.exports = function doConvert(programOption) {
7
+ const options = mergeOptions(programOption, ['entry', 'exclude', 'locales']);
8
+
9
+ if (!options.entry) {
10
+ log.error('no entry is specified');
11
+ process.exit(2);
12
+ }
13
+
14
+ if (Array.isArray(options.entry)) {
15
+ if (!Array.isArray(options.output)) {
16
+ log.error('output should be array too, since entry is array');
17
+ process.exit(2);
18
+ }
19
+
20
+ if (options.output.length !== options.entry.length) {
21
+ log.error('output length are not equal to entry length');
22
+ process.exit(2);
23
+ }
24
+ }
25
+
26
+ if (options.output) {
27
+ [].concat(options.output).forEach((outputDir) => {
28
+ if (!fs.existsSync(outputDir)) {
29
+ fs.mkdirSync(outputDir);
30
+ }
31
+ });
32
+ }
33
+
34
+ return transformReactIntlUniveralToDi18n(options);
35
+ };