From ee8961b8e82c3e5c65f1481a558c25de2299ae16 Mon Sep 17 00:00:00 2001 From: jingrow Date: Mon, 29 Dec 2025 03:59:37 +0800 Subject: [PATCH] =?UTF-8?q?vite=E7=BF=BB=E8=AF=91=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BF=BB=E8=AF=91=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=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 | 37 +++++++++++++++++++++++++++-- jcloud/translations/zh.csv | 2 +- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/dashboard/vite-plugin-translate.mjs b/dashboard/vite-plugin-translate.mjs index 9ea751e..78f0341 100644 --- a/dashboard/vite-plugin-translate.mjs +++ b/dashboard/vite-plugin-translate.mjs @@ -51,10 +51,36 @@ function findBalancedBracket(code, startPos, openChar = '(', closeChar = ')') { * @returns {string} 处理后的参数代码 */ function processNestedTranslations(paramsCode, translations, pattern) { - return paramsCode.replace(pattern, (match, ctxPrefix, quote, key) => { + return paramsCode.replace(pattern, (match, ...args) => { + // String.replace 回调的参数:match, ...captureGroups, index, input + // 需要根据捕获组数量判断格式 + // 格式1: /([a-zA-Z_$][a-zA-Z0-9_$]*\.)?[\$]t\((['"])([^'"]+)\2\)/g -> 3个捕获组 + // 格式2: /\$t\((['"])([^'"]+)\1\)/g -> 2个捕获组 + // 格式3: /\{\{\s*[\$]t\s*\(\s*(['"])([^'"]+)\1\s*\)\s*\}\}/g -> 2个捕获组 + + // 获取捕获组数量(排除最后两个参数:index 和 input) + const captureGroups = args.slice(0, -2); + let ctxPrefix = ''; + let quote; + let key; + + if (captureGroups.length === 3) { + // 格式1:带前缀的 (ctxPrefix, quote, key) + ctxPrefix = captureGroups[0] || ''; + quote = captureGroups[1]; + key = captureGroups[2]; + } else if (captureGroups.length === 2) { + // 格式2或3:不带前缀的 (quote, key) + quote = captureGroups[0]; + key = captureGroups[1]; + } else { + // 无法解析,返回原匹配 + return match; + } + const translation = translations[key]; return translation - ? `${ctxPrefix || ''}$t(${quote}${escapeString(translation, quote)}${quote})` + ? `${ctxPrefix}$t(${quote}${escapeString(translation, quote)}${quote})` : match; }); } @@ -231,11 +257,18 @@ function replaceInterpolations(code, translations) { let paramsPart = code.substring(m.afterKeyIndex, m.paramsEnd); // 处理参数部分中的嵌套 $t() 调用 + // 先处理 {{ $t(...) }} 格式(模板插值) paramsPart = processNestedTranslations( paramsPart, translations, /\{\{\s*[\$]t\s*\(\s*(['"])([^'"]+)\1\s*\)\s*\}\}/g ); + // 再处理 $t(...) 格式(参数对象中的直接调用) + paramsPart = processNestedTranslations( + paramsPart, + translations, + /\$t\((['"])([^'"]+)\1\)/g + ); const translation = translations[m.key]; if (translation) { diff --git a/jcloud/translations/zh.csv b/jcloud/translations/zh.csv index 0e056f6..19fb005 100644 --- a/jcloud/translations/zh.csv +++ b/jcloud/translations/zh.csv @@ -359,7 +359,7 @@ Payment Amount,支付金额, Payment Method,支付方式, Pay with {method}, fast and convenient. Balance will be credited immediately after successful payment.,使用{method}支付,快速便捷,余额将在支付成功后立即到账。, Alipay,支付宝, -WeChat Pay,微信支付, +WeChat Pay,微信, Billing Charges,账单费用, Next billing date,下次扣款日期, Current billing amount is,当前账单金额为,