优化vite翻译插件,增加处理对象字面量中的翻译支持
This commit is contained in:
parent
86a7d6de98
commit
27310a009c
@ -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}`;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除字符串中的转义字符(将 \' 转换为 ',\" 转换为 " 等)
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user