vue-i18n-extract-plugin 1.0.51 → 1.0.52
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/README.md +17 -0
- package/lib/visitors.js +26 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -98,6 +98,18 @@ export default {
|
|
|
98
98
|
}),
|
|
99
99
|
...
|
|
100
100
|
};
|
|
101
|
+
|
|
102
|
+
// ts支持
|
|
103
|
+
import { defineConfig } from "vite-i18n-extract-plugin";
|
|
104
|
+
|
|
105
|
+
export default defineConfig({
|
|
106
|
+
rewrite: false,
|
|
107
|
+
translator: new YoudaoTranslator({
|
|
108
|
+
appId: "youdao appId",
|
|
109
|
+
appKey: "youdao appKey"
|
|
110
|
+
}),
|
|
111
|
+
...
|
|
112
|
+
});
|
|
101
113
|
```
|
|
102
114
|
|
|
103
115
|
## Vite plugin
|
|
@@ -145,6 +157,8 @@ import enMessages from "@/locales/en.json";
|
|
|
145
157
|
|
|
146
158
|
const i18n = createI18n({
|
|
147
159
|
legacy: false,
|
|
160
|
+
globalInjection: true,
|
|
161
|
+
allowComposition: true,
|
|
148
162
|
fallbackLocale: "en",
|
|
149
163
|
locale: "zh",
|
|
150
164
|
messages: {
|
|
@@ -156,6 +170,9 @@ const i18n = createI18n({
|
|
|
156
170
|
// 导出一个$t方法
|
|
157
171
|
export const $t = i18n.global.t.bind(i18n.global);
|
|
158
172
|
|
|
173
|
+
// 建议在全局也挂载一个$t方法做兜底
|
|
174
|
+
globalThis.$t = $t;
|
|
175
|
+
|
|
159
176
|
export default i18n;
|
|
160
177
|
```
|
|
161
178
|
|
package/lib/visitors.js
CHANGED
|
@@ -15,16 +15,16 @@ function isTFunction(node, option) {
|
|
|
15
15
|
);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
function
|
|
18
|
+
function isVNodeCall(path, nodeName) {
|
|
19
19
|
const node = path.node;
|
|
20
20
|
if (!path.isCallExpression()) return false;
|
|
21
21
|
|
|
22
22
|
const callee = node.callee;
|
|
23
23
|
return (
|
|
24
|
-
(t.isIdentifier(callee) && callee.name ===
|
|
24
|
+
(t.isIdentifier(callee) && callee.name === nodeName) ||
|
|
25
25
|
(t.isMemberExpression(callee) &&
|
|
26
26
|
t.isIdentifier(callee.property) &&
|
|
27
|
-
callee.property.name ===
|
|
27
|
+
callee.property.name === nodeName)
|
|
28
28
|
);
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -130,8 +130,6 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
130
130
|
return;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
// console.log("CallExpression", path.node.arguments);
|
|
134
|
-
|
|
135
133
|
const hashed = generateId(keyText);
|
|
136
134
|
|
|
137
135
|
if (i18nMap) {
|
|
@@ -182,44 +180,44 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
182
180
|
return;
|
|
183
181
|
}
|
|
184
182
|
|
|
185
|
-
// console.log("StringLiteral", value);
|
|
186
|
-
|
|
187
183
|
const hashed = generateId(value);
|
|
188
184
|
|
|
189
185
|
if (i18nMap) {
|
|
190
186
|
i18nMap[hashed] = value;
|
|
191
187
|
}
|
|
192
188
|
|
|
193
|
-
|
|
189
|
+
// 生成 _ctx.$t("hashed")
|
|
190
|
+
let callExpression = t.callExpression(
|
|
191
|
+
t.memberExpression(
|
|
192
|
+
t.identifier("_ctx"),
|
|
193
|
+
t.identifier(option.translateKey)
|
|
194
|
+
),
|
|
195
|
+
[t.stringLiteral(hashed)]
|
|
196
|
+
);
|
|
194
197
|
|
|
195
198
|
// 判断是否createTextVNode或MemberExpression(如 Vue.createTextVNode)
|
|
196
|
-
if (
|
|
197
|
-
// _ctx.$t("hashed")
|
|
198
|
-
callExpression = t.callExpression(
|
|
199
|
-
t.memberExpression(
|
|
200
|
-
t.identifier("_ctx"),
|
|
201
|
-
t.identifier(option.translateKey)
|
|
202
|
-
),
|
|
203
|
-
[t.stringLiteral(hashed)]
|
|
204
|
-
);
|
|
205
|
-
|
|
199
|
+
if (isVNodeCall(parentPath, "_createTextVNode")) {
|
|
206
200
|
// 如果 createTextVNode 参数只有一个,则补充第二个参数 1
|
|
207
201
|
const args = parentPath.node.arguments;
|
|
208
202
|
if (args.length === 1) {
|
|
209
203
|
parentPath.node.arguments = [callExpression, t.numericLiteral(1)];
|
|
210
204
|
}
|
|
205
|
+
// 是否 vue.createElementVNode(...),且该 StringLiteral 是第三个参数
|
|
206
|
+
} else if (isVNodeCall(parentPath, "_createElementVNode")) {
|
|
207
|
+
const callExpr = parentPath.node;
|
|
208
|
+
const args = callExpr.arguments;
|
|
209
|
+
|
|
210
|
+
// 当前是第三个参数,且参数数量为3
|
|
211
|
+
const argIndex = args.findIndex(arg => arg === path.node);
|
|
212
|
+
if (argIndex === 2 && args.length === 3) {
|
|
213
|
+
args[2] = callExpression; // 替换第3个参数
|
|
214
|
+
args.push(t.numericLiteral(1)); // 添加第4个参数
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
211
217
|
} else {
|
|
212
218
|
const hasCreateVNode = transformDirectiveIfNeeded(path, parentPath);
|
|
213
|
-
if (hasCreateVNode) {
|
|
214
|
-
|
|
215
|
-
t.memberExpression(
|
|
216
|
-
t.identifier("_ctx"),
|
|
217
|
-
t.identifier(option.translateKey)
|
|
218
|
-
),
|
|
219
|
-
[t.stringLiteral(hashed)]
|
|
220
|
-
);
|
|
221
|
-
} else {
|
|
222
|
-
// $t("hashed")
|
|
219
|
+
if (!hasCreateVNode) {
|
|
220
|
+
// 生成 $t("hashed")
|
|
223
221
|
callExpression = t.callExpression(t.identifier(option.translateKey), [
|
|
224
222
|
t.stringLiteral(hashed)
|
|
225
223
|
]);
|
|
@@ -262,8 +260,6 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
262
260
|
|
|
263
261
|
if (option.extractFromText === false) return;
|
|
264
262
|
|
|
265
|
-
// console.log("TemplateElement", value);
|
|
266
|
-
|
|
267
263
|
const hashed = generateId(value);
|
|
268
264
|
|
|
269
265
|
if (i18nMap) {
|
|
@@ -285,8 +281,6 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
285
281
|
return;
|
|
286
282
|
}
|
|
287
283
|
|
|
288
|
-
// console.log("JSXText", path.node.value);
|
|
289
|
-
|
|
290
284
|
const hashed = generateId(text);
|
|
291
285
|
|
|
292
286
|
if (i18nMap) {
|
|
@@ -315,8 +309,6 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
315
309
|
return;
|
|
316
310
|
}
|
|
317
311
|
|
|
318
|
-
// console.log("JSXExpressionContainer", path.node.expression.expr);
|
|
319
|
-
|
|
320
312
|
const hashed = generateId(value);
|
|
321
313
|
|
|
322
314
|
if (i18nMap) {
|