dev #3
@ -191,6 +191,8 @@ function processVueSFC(code, translations) {
|
||||
let replacedContent = templateContent;
|
||||
replacedContent = replaceInterpolations(replacedContent, translations);
|
||||
replacedContent = replaceAttributeBindings(replacedContent, translations);
|
||||
// 处理属性绑定中的数组字面量,如 :items="[{ label: $t('key'), ... }]"
|
||||
replacedContent = replaceAttributeArrayLiterals(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);
|
||||
@ -203,11 +205,14 @@ function processVueSFC(code, translations) {
|
||||
/(<script[^>]*>)([\s\S]*?)(<\/script>)/gi,
|
||||
(match, openTag, scriptContent, closeTag) => {
|
||||
const replacedContent = scriptContent.replace(
|
||||
/\$t\((['"])((?:\\.|(?!\1).)*)\1\)/g,
|
||||
/(?:this\.)?\$t\((['"])((?:\\.|(?!\1).)*)\1\)/g,
|
||||
(match, quote, key) => {
|
||||
const unescapedKey = unescapeString(key);
|
||||
const translation = translations[unescapedKey];
|
||||
return translation ? `$t(${quote}${escapeString(translation, quote)}${quote})` : match;
|
||||
// 如果匹配包含 this.,保留它
|
||||
const hasThis = match.startsWith('this.');
|
||||
const prefix = hasThis ? 'this.' : '';
|
||||
return translation ? `${prefix}$t(${quote}${escapeString(translation, quote)}${quote})` : match;
|
||||
}
|
||||
);
|
||||
return openTag + replacedContent + closeTag;
|
||||
@ -383,6 +388,33 @@ function replaceAttributeBindings(code, translations) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换属性绑定中数组字面量内的 $t('xxx')
|
||||
* 例如::items="[{ label: $t('key'), ... }]"
|
||||
*/
|
||||
function replaceAttributeArrayLiterals(code, translations) {
|
||||
// 匹配属性绑定中的数组字面量,如 :items="[...]"
|
||||
// 使用更宽松的匹配,支持单引号和双引号
|
||||
return code.replace(
|
||||
/([:@]|v-bind:|v-on:)([a-zA-Z0-9_-]+)=(["'])\[([\s\S]*?)\]\3/g,
|
||||
(match, prefix, attrName, outerQuote, arrayContent) => {
|
||||
// 在数组内容中查找并替换所有 $t() 调用,直接替换为翻译文本
|
||||
const replacedContent = arrayContent.replace(
|
||||
/\$t\((['"])((?:\\.|(?!\1).)*)\1\)/g,
|
||||
(m, quote, key) => {
|
||||
const unescapedKey = unescapeString(key);
|
||||
const translation = translations[unescapedKey];
|
||||
if (!translation || !translation.trim()) return m;
|
||||
// 直接替换为翻译文本字符串,转义引号
|
||||
const escaped = escapeString(translation, quote);
|
||||
return `${quote}${escaped}${quote}`;
|
||||
}
|
||||
);
|
||||
return `${prefix}${attrName}=${outerQuote}[${replacedContent}]${outerQuote}`;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除字符串中的转义字符(将 \' 转换为 ',\" 转换为 " 等)
|
||||
*/
|
||||
|
||||
@ -293,7 +293,6 @@ Site Update,站点更新,
|
||||
Site update started,站点更新已启动,
|
||||
Size,尺寸,
|
||||
Servers,服务器,
|
||||
Servers,服务器,
|
||||
Skip,跳跃,
|
||||
Social Login Key,社交登录密钥,
|
||||
Source,源,
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user