vue-i18n-extract-plugin 1.0.47 → 1.0.48
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 +1 -1
- package/lib/cli.js +1 -1
- package/lib/options.js +1 -1
- package/lib/visitors.js +31 -41
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ extractI18n(options)
|
|
|
48
48
|
```javascript
|
|
49
49
|
const defaultOptions = {
|
|
50
50
|
translateKey: "$t", // 提取的函数的名称
|
|
51
|
-
rewrite:
|
|
51
|
+
rewrite: false, // 是否将提取到的内容转换为id后重写入源文件
|
|
52
52
|
extractFromText: true, // 是否允许从纯文本节点中提取翻译内容
|
|
53
53
|
autoImportI18n: true, // 是否自动导入 i18n 模块
|
|
54
54
|
autoTranslate: true, // 提取完成后是否自动翻译
|
package/lib/cli.js
CHANGED
package/lib/options.js
CHANGED
|
@@ -2,7 +2,7 @@ const { GoogleTranslator } = require("./translators");
|
|
|
2
2
|
|
|
3
3
|
const defaultOptions = {
|
|
4
4
|
translateKey: "$t", // 提取的函数的名称
|
|
5
|
-
rewrite:
|
|
5
|
+
rewrite: false, // 是否将提取到的内容转换为id后重写入源文件
|
|
6
6
|
extractFromText: true, // 是否允许从纯文本节点中提取翻译内容
|
|
7
7
|
autoImportI18n: true, // 是否自动导入 i18n 模块
|
|
8
8
|
autoTranslate: true, // 提取完成后是否自动翻译
|
package/lib/visitors.js
CHANGED
|
@@ -58,10 +58,8 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
58
58
|
i18nMap[hashed] = keyText;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
path.node.arguments[0] = newArg;
|
|
64
|
-
}
|
|
61
|
+
const newArg = t.stringLiteral(hashed);
|
|
62
|
+
path.node.arguments[0] = newArg;
|
|
65
63
|
},
|
|
66
64
|
|
|
67
65
|
StringLiteral(path) {
|
|
@@ -113,19 +111,17 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
113
111
|
i18nMap[hashed] = value;
|
|
114
112
|
}
|
|
115
113
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
path.replaceWith(callExpression);
|
|
128
|
-
}
|
|
114
|
+
const callExpression = t.callExpression(
|
|
115
|
+
t.identifier(option.translateKey),
|
|
116
|
+
[t.stringLiteral(hashed)]
|
|
117
|
+
);
|
|
118
|
+
// 如果是 JSX 属性值,需要包裹在 JSXExpressionContainer 中
|
|
119
|
+
if (parentPath.isJSXAttribute()) {
|
|
120
|
+
const jsxExpression = t.jsxExpressionContainer(callExpression);
|
|
121
|
+
path.replaceWith(jsxExpression);
|
|
122
|
+
} else {
|
|
123
|
+
// 其他情况直接替换
|
|
124
|
+
path.replaceWith(callExpression);
|
|
129
125
|
}
|
|
130
126
|
},
|
|
131
127
|
|
|
@@ -163,11 +159,9 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
163
159
|
i18nMap[hashed] = value;
|
|
164
160
|
}
|
|
165
161
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
node.value.raw = node.value.cooked = `\${${tCallExpression}}`;
|
|
170
|
-
}
|
|
162
|
+
// 替换为字符类型翻译节点
|
|
163
|
+
const tCallExpression = `${option.translateKey}('${hashed}')`;
|
|
164
|
+
node.value.raw = node.value.cooked = `\${${tCallExpression}}`;
|
|
171
165
|
},
|
|
172
166
|
JSXText(path) {
|
|
173
167
|
// <div>Hello world</div>
|
|
@@ -188,16 +182,14 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
188
182
|
i18nMap[hashed] = text;
|
|
189
183
|
}
|
|
190
184
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
t.
|
|
195
|
-
t.
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
);
|
|
200
|
-
}
|
|
185
|
+
// 替换为表达式 {$t("hashed")}
|
|
186
|
+
path.replaceWith(
|
|
187
|
+
t.jsxExpressionContainer(
|
|
188
|
+
t.callExpression(t.identifier(option.translateKey), [
|
|
189
|
+
t.stringLiteral(hashed)
|
|
190
|
+
])
|
|
191
|
+
)
|
|
192
|
+
);
|
|
201
193
|
},
|
|
202
194
|
JSXExpressionContainer(path) {
|
|
203
195
|
// <div>{"Hi"}</div>
|
|
@@ -216,15 +208,13 @@ function createI18nVisitor(option, i18nMap) {
|
|
|
216
208
|
i18nMap[hashed] = expr.value;
|
|
217
209
|
}
|
|
218
210
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
t.
|
|
222
|
-
t.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
);
|
|
227
|
-
}
|
|
211
|
+
path.replaceWith(
|
|
212
|
+
t.jsxExpressionContainer(
|
|
213
|
+
t.callExpression(t.identifier(option.translateKey), [
|
|
214
|
+
t.stringLiteral(hashed)
|
|
215
|
+
])
|
|
216
|
+
)
|
|
217
|
+
);
|
|
228
218
|
}
|
|
229
219
|
}
|
|
230
220
|
};
|