@chaoswise/intl 2.1.2 → 2.1.3
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/bin/scripts/addLocale.js
CHANGED
|
@@ -4,6 +4,7 @@ const transformAst = require('./util/transformAst');
|
|
|
4
4
|
const getWordsByIds = require('./util/getWordsByIds');
|
|
5
5
|
const log = require('./util/log');
|
|
6
6
|
const genXlsx = require('./util/genXlsx');
|
|
7
|
+
const service = require('./service');
|
|
7
8
|
|
|
8
9
|
async function update() {
|
|
9
10
|
log.info('收集全部词条...');
|
|
@@ -15,19 +16,33 @@ async function update() {
|
|
|
15
16
|
// 获取全部词条
|
|
16
17
|
const allWords = await getWordsByIds(_downloadIds, conf);
|
|
17
18
|
|
|
19
|
+
// 获取所有语种
|
|
20
|
+
const languageRes = await service.getLanguage();
|
|
21
|
+
if (languageRes.code !== 10000) {
|
|
22
|
+
log.error('请求数据出错:' + languageRes.msg);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
const languages = languageRes.data;
|
|
26
|
+
const mainLanguage = languages.filter((l) => l.isMain)[0];
|
|
27
|
+
|
|
18
28
|
const localeKeys = Object.keys(allWords);
|
|
19
|
-
const xlsxData = _downloadIds.map(id => {
|
|
29
|
+
const xlsxData = _downloadIds.map((id) => {
|
|
20
30
|
const localeObj = {};
|
|
21
|
-
localeKeys.forEach(key => {
|
|
22
|
-
|
|
31
|
+
localeKeys.forEach((key) => {
|
|
32
|
+
if (mainLanguage && key === mainLanguage.key) {
|
|
33
|
+
localeObj[key] =
|
|
34
|
+
allWords[key][id] || info.defaultKeyWordMap[id] || '待翻译';
|
|
35
|
+
} else {
|
|
36
|
+
localeObj[key] = allWords[key][id] || '待翻译';
|
|
37
|
+
}
|
|
23
38
|
});
|
|
24
39
|
return {
|
|
25
40
|
id,
|
|
26
|
-
...localeObj
|
|
27
|
-
}
|
|
41
|
+
...localeObj,
|
|
42
|
+
};
|
|
28
43
|
});
|
|
29
44
|
|
|
30
|
-
await genXlsx(xlsxData)
|
|
45
|
+
await genXlsx(xlsxData);
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
module.exports = update;
|
|
@@ -2,21 +2,14 @@ const t = require('@babel/types');
|
|
|
2
2
|
const specialMatch = require('./specialMatch');
|
|
3
3
|
|
|
4
4
|
module.exports = function f(
|
|
5
|
-
{
|
|
6
|
-
i18nObject,
|
|
7
|
-
i18nMethod,
|
|
8
|
-
importCode,
|
|
9
|
-
ignoreLines,
|
|
10
|
-
i18nDefaultFunctionKey,
|
|
11
|
-
},
|
|
5
|
+
{ i18nObject, i18nMethod, importCode, ignoreLines, i18nDefaultFunctionKey },
|
|
12
6
|
returns,
|
|
13
|
-
{ special }
|
|
7
|
+
{ special }
|
|
14
8
|
) {
|
|
15
|
-
const { replaceWords, downloadIds } = returns;
|
|
9
|
+
const { replaceWords, downloadIds, defaultKeyWordMap } = returns;
|
|
16
10
|
|
|
17
11
|
// XXX: [TRICKY] 防止中文转码为 unicode
|
|
18
12
|
function hackValue(value, id) {
|
|
19
|
-
|
|
20
13
|
return Object.assign(t.StringLiteral(value), {
|
|
21
14
|
extra: {
|
|
22
15
|
raw: `'${id}'`, // id
|
|
@@ -64,7 +57,6 @@ module.exports = function f(
|
|
|
64
57
|
|
|
65
58
|
// 替换字符串为我们使用的国际化API
|
|
66
59
|
function makeReplace(value, variables) {
|
|
67
|
-
|
|
68
60
|
// 存在临时id替换
|
|
69
61
|
returns.hasTouch = true;
|
|
70
62
|
|
|
@@ -90,25 +82,54 @@ module.exports = function f(
|
|
|
90
82
|
|
|
91
83
|
// 函数或方法调用表达式
|
|
92
84
|
function dealCallExpressionNode(node) {
|
|
85
|
+
// 记录当前key对应的默认词条
|
|
86
|
+
if (
|
|
87
|
+
node.callee.type === 'MemberExpression' &&
|
|
88
|
+
node.callee.object.type === 'CallExpression' &&
|
|
89
|
+
node.callee.property.name === i18nDefaultFunctionKey &&
|
|
90
|
+
((node.callee.object.callee.type === 'MemberExpression' &&
|
|
91
|
+
node.callee.object.callee.object.name === i18nObject &&
|
|
92
|
+
node.callee.object.callee.property.name === i18nMethod) ||
|
|
93
|
+
(!i18nObject &&
|
|
94
|
+
node.callee.object.callee.type === 'Identifier' &&
|
|
95
|
+
node.callee.object.callee.name === i18nMethod))
|
|
96
|
+
) {
|
|
97
|
+
if (
|
|
98
|
+
node.arguments.length > 0 &&
|
|
99
|
+
node.callee.object.arguments.length > 0
|
|
100
|
+
) {
|
|
101
|
+
const k = node.callee.object.arguments[0].value;
|
|
102
|
+
const v = node.arguments[0].value;
|
|
103
|
+
|
|
104
|
+
if (k) {
|
|
105
|
+
defaultKeyWordMap[k] = v;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
93
110
|
const res = {
|
|
94
111
|
needReplace: false,
|
|
95
112
|
value: null,
|
|
96
113
|
skip: false,
|
|
97
|
-
|
|
114
|
+
otherArgs: [],
|
|
115
|
+
};
|
|
98
116
|
|
|
99
117
|
if (
|
|
100
118
|
(node.callee.type === 'MemberExpression' &&
|
|
101
119
|
node.callee.object.name === i18nObject &&
|
|
102
120
|
node.callee.property.name === i18nMethod) ||
|
|
103
|
-
(!i18nObject &&
|
|
121
|
+
(!i18nObject &&
|
|
122
|
+
node.callee.type === 'Identifier' &&
|
|
123
|
+
node.callee.name === i18nMethod)
|
|
104
124
|
) {
|
|
105
125
|
// 收集现有的 key
|
|
106
126
|
const args = node.arguments;
|
|
107
|
-
if(!args.length) {
|
|
127
|
+
if (!args.length) {
|
|
108
128
|
return res;
|
|
109
129
|
}
|
|
110
130
|
|
|
111
131
|
let key = args[0].value;
|
|
132
|
+
res.otherArgs = args.slice(1);
|
|
112
133
|
|
|
113
134
|
// intl.t(key)
|
|
114
135
|
// replaceWords { key: id }
|
|
@@ -137,16 +158,21 @@ module.exports = function f(
|
|
|
137
158
|
|
|
138
159
|
if (!shouldIgnore(node) && special) {
|
|
139
160
|
// intl.get('id').d(中文) 获取id和中文
|
|
161
|
+
// TODO: 当前get()方法不支持处理有参数的场景[get('id',{a:1})],如需处理需要同时修改脚手架中的react-router-config-loader
|
|
140
162
|
const { id, defaultValue } = specialMatch(value) || {};
|
|
141
163
|
if (!id) return;
|
|
142
164
|
|
|
165
|
+
defaultKeyWordMap[id] = defaultValue;
|
|
166
|
+
|
|
143
167
|
const finalyId = replaceWords[id];
|
|
144
168
|
if (finalyId) {
|
|
145
169
|
returns.hasTouch = true;
|
|
146
170
|
downloadIds.push(finalyId);
|
|
147
171
|
|
|
148
|
-
const str = `${
|
|
149
|
-
|
|
172
|
+
const str = `${
|
|
173
|
+
i18nObject ? `${i18nObject}.` : ''
|
|
174
|
+
}${i18nMethod}("${finalyId}").${i18nDefaultFunctionKey}("${defaultValue}")`;
|
|
175
|
+
const newStr = hackValue(str, str);
|
|
150
176
|
path.replaceWith(newStr);
|
|
151
177
|
path.skip();
|
|
152
178
|
} else {
|
|
@@ -169,12 +195,13 @@ module.exports = function f(
|
|
|
169
195
|
// 函数或方法调用表达式,比如:a('中午')
|
|
170
196
|
CallExpression(path) {
|
|
171
197
|
const { node } = path;
|
|
172
|
-
|
|
173
198
|
if (!node.ignore) {
|
|
174
|
-
const { needReplace, value, skip } =
|
|
199
|
+
const { needReplace, value, skip, otherArgs } =
|
|
200
|
+
dealCallExpressionNode(node);
|
|
175
201
|
|
|
176
202
|
if (needReplace) {
|
|
177
203
|
const res = makeReplace(value);
|
|
204
|
+
res.arguments = res.arguments.concat(otherArgs);
|
|
178
205
|
path.replaceWith(res);
|
|
179
206
|
|
|
180
207
|
path.node.ignore = true;
|
|
@@ -183,4 +210,4 @@ module.exports = function f(
|
|
|
183
210
|
}
|
|
184
211
|
},
|
|
185
212
|
};
|
|
186
|
-
}
|
|
213
|
+
};
|
|
@@ -82,6 +82,7 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
82
82
|
const allWords = []; // 收集到的所有词条
|
|
83
83
|
const downloadIds = []; // 需要从平台下载的词条id
|
|
84
84
|
const specialMethod = []; // 替换的国际化特殊方法
|
|
85
|
+
const defaultKeyWordMap = {}; // addLocal指令中使用:记录当前key对应的默认词条
|
|
85
86
|
|
|
86
87
|
// babel配置
|
|
87
88
|
const transformOptions = {
|
|
@@ -129,6 +130,7 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
129
130
|
replaceWords: replaceWords || {},
|
|
130
131
|
downloadIds,
|
|
131
132
|
specialMethod,
|
|
133
|
+
defaultKeyWordMap,
|
|
132
134
|
hasImport: false, // 是否已经引入通用国际化API,import {init} from ...;
|
|
133
135
|
hasTouch: false, // 是否存在需要替换的词条
|
|
134
136
|
};
|
|
@@ -200,5 +202,6 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
200
202
|
allWords,
|
|
201
203
|
downloadIds,
|
|
202
204
|
specialMethod,
|
|
205
|
+
defaultKeyWordMap
|
|
203
206
|
};
|
|
204
207
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chaoswise/intl",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
4
4
|
"author": "cloudwiser",
|
|
5
5
|
"description": "intl",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -84,5 +84,5 @@
|
|
|
84
84
|
"react-dom": "^16.13.1"
|
|
85
85
|
},
|
|
86
86
|
"license": "MIT",
|
|
87
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "422bbd1a0545e50a6e8a6e41240536bbc5274e20"
|
|
88
88
|
}
|