Просмотр исходного кода

fix: 改回静态import加载YAML + 修复i18n配置

1. siphonI18n 改用静态 import 替代 import.meta.glob 加载 YAML 文件,
   避免 Vite 8 / Windows 下 glob 匹配失败导致资源未加载
2. transformI18n 中 t() 调用移除 .call() 改为直接调用,
   兼容 vue-i18n v11
3. VueI18nPlugin include 使用简洁相对路径 locales/**

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline 2 недель назад
Родитель
Сommit
0cc1604c7f
2 измененных файлов с 11 добавлено и 12 удалено
  1. 1 1
      haha-admin-web/build/plugins.ts
  2. 10 11
      haha-admin-web/src/plugins/i18n.ts

+ 1 - 1
haha-admin-web/build/plugins.ts

@@ -31,7 +31,7 @@ export async function getPluginsList(
     // jsx、tsx语法支持
     vueJsx(),
     VueI18nPlugin({
-      include: ["locales/**/*.yaml", "locales/**/*.yml"]
+      include: ["locales/**"]
     }),
     /**
      * 在页面上按住组合键时,鼠标在页面移动即会在 DOM 上出现遮罩层并显示相关信息,点击一下将自动打开 IDE 并将光标定位到元素对应的代码位置

+ 10 - 11
haha-admin-web/src/plugins/i18n.ts

@@ -8,16 +8,15 @@ import { storageLocal, isObject } from "@pureadmin/utils";
 import enLocale from "element-plus/es/locale/lang/en";
 import zhLocale from "element-plus/es/locale/lang/zh-cn";
 
+// 项目国际化资源(静态导入,兼容 Vite 8)
+import zhCN from "../../locales/zh-CN.yaml";
+import en from "../../locales/en.yaml";
+
 const siphonI18n = (function () {
-  // 仅初始化一次国际化配置
-  const cache = Object.fromEntries(
-    Object.entries(
-      import.meta.glob("../../locales/*.y(a)?ml", { eager: true })
-    ).map(([key, value]: any) => {
-      const matched = key.match(/([A-Za-z0-9-_]+)\./i)[1];
-      return [matched, value.default];
-    })
-  );
+  const cache: Record<string, any> = {
+    "zh-CN": zhCN,
+    "en": en
+  };
   return (prefix = "zh-CN") => {
     return cache[prefix];
   };
@@ -89,10 +88,10 @@ export function transformI18n(message: any = "") {
   const key = message.match(/(\S*)\./)?.input;
 
   if (key && flatI18n("zh-CN").has(key)) {
-    return i18n.global.t.call(i18n.global.locale, message);
+    return i18n.global.t(message);
   } else if (!key && Object.hasOwn(siphonI18n("zh-CN"), message)) {
     // 兼容非嵌套形式的国际化写法
-    return i18n.global.t.call(i18n.global.locale, message);
+    return i18n.global.t(message);
   } else {
     return message;
   }