更新vite翻译插件,支持通过 useI18n composable 使用 t 函数

This commit is contained in:
jingrow 2025-12-30 04:02:17 +08:00
parent 1731025744
commit 4e34df7876

View File

@ -285,11 +285,16 @@ function processVueSFC(code, translations) {
);
// 再处理 t() 函数调用(从 i18n.js 导入的翻译函数)
// 检查是否有从 i18n 导入 t 函数
// 检查是否有从 i18n 导入 t 函数,或通过 useI18n composable 使用
const hasTImport = /import\s*\{[^}]*\bt\b[^}]*\}\s*from\s*['"]\.\.?\/.*i18n['"]/i.test(scriptContent) ||
/import\s*\{[^}]*\bt\b[^}]*\}\s*from\s*['"]@\/.*i18n['"]/i.test(scriptContent);
if (hasTImport) {
// 检查是否通过 useI18n composable 使用 t 函数
const hasUseI18n = /import\s*\{[^}]*useI18n[^}]*\}\s*from\s*['"]\.\.?\/.*useI18n['"]/i.test(scriptContent) ||
/import\s*\{[^}]*useI18n[^}]*\}\s*from\s*['"]@\/.*useI18n['"]/i.test(scriptContent) ||
/const\s*\{[^}]*\bt\b[^}]*\}\s*=\s*useI18n\(\)/i.test(scriptContent);
if (hasTImport || hasUseI18n) {
// 匹配 t('key') 或 t("key"),但排除 t.something() 或 t['something']()
replacedContent = replacedContent.replace(
/\bt\((['"])((?:\\.|(?!\1).)*)\1\)/g,