From 27310a009c1f7d18154656c00fa31764136ed813 Mon Sep 17 00:00:00 2001 From: jingrow Date: Mon, 29 Dec 2025 22:06:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96vite=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=AD=97=E9=9D=A2=E9=87=8F=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E7=BF=BB=E8=AF=91=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dashboard/vite-plugin-translate.mjs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/dashboard/vite-plugin-translate.mjs b/dashboard/vite-plugin-translate.mjs index cef53a5..541aaed 100644 --- a/dashboard/vite-plugin-translate.mjs +++ b/dashboard/vite-plugin-translate.mjs @@ -193,6 +193,8 @@ function processVueSFC(code, translations) { replacedContent = replaceAttributeBindings(replacedContent, translations); // 处理属性绑定中的数组字面量,如 :items="[{ label: $t('key'), ... }]" replacedContent = replaceAttributeArrayLiterals(replacedContent, translations); + // 处理属性绑定中的对象字面量,如 :options="{ title: $t('key'), ... }" + replacedContent = replaceAttributeObjectLiterals(replacedContent, translations); code = code.substring(0, startIndex) + openTag + replacedContent + CONSTANTS.VUE_TEMPLATE_CLOSE_TAG + code.substring(startIndex + openTag.length + templateContent.length + CONSTANTS.VUE_TEMPLATE_CLOSE_TAG_LEN); @@ -415,6 +417,33 @@ function replaceAttributeArrayLiterals(code, translations) { ); } +/** + * 替换属性绑定中对象字面量内的 $t('xxx') + * 例如::options="{ title: $t('key'), actions: [...] }" + */ +function replaceAttributeObjectLiterals(code, translations) { + // 匹配属性绑定中的对象字面量,如 :options="{ ... }" + // 需要处理嵌套的大括号 + return code.replace( + /([:@]|v-bind:|v-on:)([a-zA-Z0-9_-]+)=(["'])\{([\s\S]*?)\}\3/g, + (match, prefix, attrName, outerQuote, objectContent) => { + // 在对象内容中查找并替换所有 $t() 调用,直接替换为翻译文本 + const replacedContent = objectContent.replace( + /(\w+:\s*)\$t\((['"])((?:\\.|(?!\2).)*)\2\)/g, + (m, keyPrefix, quote, key) => { + const unescapedKey = unescapeString(key); + const translation = translations[unescapedKey]; + if (!translation || !translation.trim()) return m; + // 直接替换为翻译文本字符串,转义引号 + const escaped = escapeString(translation, quote); + return `${keyPrefix}${quote}${escaped}${quote}`; + } + ); + return `${prefix}${attrName}=${outerQuote}{${replacedContent}}${outerQuote}`; + } + ); +} + /** * 去除字符串中的转义字符(将 \' 转换为 ',\" 转换为 " 等) */