vite翻译插件增加翻译参数的支持
This commit is contained in:
parent
06ff855816
commit
ee8961b8e8
@ -51,10 +51,36 @@ function findBalancedBracket(code, startPos, openChar = '(', closeChar = ')') {
|
|||||||
* @returns {string} 处理后的参数代码
|
* @returns {string} 处理后的参数代码
|
||||||
*/
|
*/
|
||||||
function processNestedTranslations(paramsCode, translations, pattern) {
|
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];
|
const translation = translations[key];
|
||||||
return translation
|
return translation
|
||||||
? `${ctxPrefix || ''}$t(${quote}${escapeString(translation, quote)}${quote})`
|
? `${ctxPrefix}$t(${quote}${escapeString(translation, quote)}${quote})`
|
||||||
: match;
|
: match;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -231,11 +257,18 @@ function replaceInterpolations(code, translations) {
|
|||||||
let paramsPart = code.substring(m.afterKeyIndex, m.paramsEnd);
|
let paramsPart = code.substring(m.afterKeyIndex, m.paramsEnd);
|
||||||
|
|
||||||
// 处理参数部分中的嵌套 $t() 调用
|
// 处理参数部分中的嵌套 $t() 调用
|
||||||
|
// 先处理 {{ $t(...) }} 格式(模板插值)
|
||||||
paramsPart = processNestedTranslations(
|
paramsPart = processNestedTranslations(
|
||||||
paramsPart,
|
paramsPart,
|
||||||
translations,
|
translations,
|
||||||
/\{\{\s*[\$]t\s*\(\s*(['"])([^'"]+)\1\s*\)\s*\}\}/g
|
/\{\{\s*[\$]t\s*\(\s*(['"])([^'"]+)\1\s*\)\s*\}\}/g
|
||||||
);
|
);
|
||||||
|
// 再处理 $t(...) 格式(参数对象中的直接调用)
|
||||||
|
paramsPart = processNestedTranslations(
|
||||||
|
paramsPart,
|
||||||
|
translations,
|
||||||
|
/\$t\((['"])([^'"]+)\1\)/g
|
||||||
|
);
|
||||||
|
|
||||||
const translation = translations[m.key];
|
const translation = translations[m.key];
|
||||||
if (translation) {
|
if (translation) {
|
||||||
|
|||||||
@ -359,7 +359,7 @@ Payment Amount,支付金额,
|
|||||||
Payment Method,支付方式,
|
Payment Method,支付方式,
|
||||||
Pay with {method}, fast and convenient. Balance will be credited immediately after successful payment.,使用{method}支付,快速便捷,余额将在支付成功后立即到账。,
|
Pay with {method}, fast and convenient. Balance will be credited immediately after successful payment.,使用{method}支付,快速便捷,余额将在支付成功后立即到账。,
|
||||||
Alipay,支付宝,
|
Alipay,支付宝,
|
||||||
WeChat Pay,微信支付,
|
WeChat Pay,微信,
|
||||||
Billing Charges,账单费用,
|
Billing Charges,账单费用,
|
||||||
Next billing date,下次扣款日期,
|
Next billing date,下次扣款日期,
|
||||||
Current billing amount is,当前账单金额为,
|
Current billing amount is,当前账单金额为,
|
||||||
|
|||||||
|
Can't render this file because it has a wrong number of fields in line 338.
|
Loading…
x
Reference in New Issue
Block a user