优化vite翻译插件
This commit is contained in:
parent
114f6211c7
commit
1731025744
@ -180,8 +180,8 @@ function vitePluginTranslate(options = {}) {
|
|||||||
code = replaceCompiledFormat(code, translations);
|
code = replaceCompiledFormat(code, translations);
|
||||||
code = replaceAttributeBindings(code, translations);
|
code = replaceAttributeBindings(code, translations);
|
||||||
} else if (isScriptModule) {
|
} else if (isScriptModule) {
|
||||||
// Vue 编译后的 script 模块 - 需要处理 this.$t()、$t() 以及编译后的函数调用(如 le()、t())
|
// Vue 编译后的 script 模块 - 处理 this.$t() 和 $t() 调用
|
||||||
// 先处理 this.$t() 和 $t()
|
// 注意:t() 函数调用已在原始 Vue SFC 文件中处理,编译后应该已经是翻译后的字符串
|
||||||
code = code.replace(
|
code = code.replace(
|
||||||
/(?:this\.)?\$t\((['"])((?:\\.|(?!\1).)*)\1\)/g,
|
/(?:this\.)?\$t\((['"])((?:\\.|(?!\1).)*)\1\)/g,
|
||||||
(match, quote, key) => {
|
(match, quote, key) => {
|
||||||
@ -193,23 +193,6 @@ function vitePluginTranslate(options = {}) {
|
|||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
// 再处理编译后的函数调用(如 le("key")、t("key"))
|
|
||||||
// 匹配函数名后跟括号和字符串参数
|
|
||||||
code = code.replace(
|
|
||||||
/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\((['"])((?:\\.|(?!\2).)*)\2\)/g,
|
|
||||||
(match, funcName, quote, key) => {
|
|
||||||
// 只处理可能是翻译函数的调用(通常是单字母或短名称,如 t, le, i 等)
|
|
||||||
// 但排除明显的非翻译函数(如 console.log, JSON.parse 等)
|
|
||||||
if (funcName.length <= 3 && !['log', 'warn', 'error', 'parse', 'stringify', 'keys', 'values', 'entries'].includes(funcName)) {
|
|
||||||
const unescapedKey = unescapeString(key);
|
|
||||||
const translation = translations[unescapedKey];
|
|
||||||
if (translation) {
|
|
||||||
return funcName + '(' + quote + escapeString(translation, quote) + quote + ')';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return match;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
// 原始 Vue SFC 文件
|
// 原始 Vue SFC 文件
|
||||||
code = processVueSFC(code, translations);
|
code = processVueSFC(code, translations);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user