From 65722f376ac0972b63e9b77cca623f7db66c820a Mon Sep 17 00:00:00 2001 From: InpaintRegionEditor Dev Date: Tue, 3 Mar 2026 20:13:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20i18n=20=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E8=B7=AF=E5=BE=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 locales 目录移到 web 目录下(WEB_DIRECTORY 映射) - 添加 console.log 调试加载状态 - 在 beforeRegisterNodeDef 中确保语言加载完成 --- web/extension.js | 19 +++++++++++-------- {locales => web/locales}/en/main.json | 0 {locales => web/locales}/zh/main.json | 0 3 files changed, 11 insertions(+), 8 deletions(-) rename {locales => web/locales}/en/main.json (100%) rename {locales => web/locales}/zh/main.json (100%) diff --git a/web/extension.js b/web/extension.js index 298ffcf..6a6b4d8 100644 --- a/web/extension.js +++ b/web/extension.js @@ -10,35 +10,36 @@ import { api } from "../../scripts/api.js"; // ==================== i18n 多语言支持 ==================== const i18n = { - data: {}, + data: null, + loaded: false, // 获取当前语言 getLocale() { return localStorage['AGL.Locale'] || localStorage['Comfy.Settings.AGL.Locale'] || 'en-US'; }, - // 是否是中文 - isZh() { - const locale = this.getLocale(); - return locale.startsWith('zh'); - }, - // 加载语言文件 async load() { + if (this.loaded) return; const locale = this.getLocale(); const lang = locale.startsWith('zh') ? 'zh' : 'en'; try { const resp = await fetch(`/extensions/InpaintRegionEditor/locales/${lang}/main.json`); if (resp.ok) { this.data = await resp.json(); + console.log('i18n loaded:', lang, this.data); + } else { + console.warn('i18n response not ok:', resp.status); } } catch (e) { console.warn('Failed to load locale:', e); } + this.loaded = true; }, // 获取翻译 t(key) { + if (!this.data) return key; const keys = key.split('.'); let value = this.data; for (const k of keys) { @@ -52,7 +53,7 @@ const i18n = { } }; -// 快捷函数 +// 快捷函数(延迟获取,确保已加载) function t(key) { return i18n.t(key); } @@ -1235,6 +1236,8 @@ app.registerExtension({ }, async beforeRegisterNodeDef(nodeType, nodeData) { + // 确保语言文件加载完成 + await i18n.load(); if (nodeData.name !== "InpaintRegionEditor") return; // 右键菜单 diff --git a/locales/en/main.json b/web/locales/en/main.json similarity index 100% rename from locales/en/main.json rename to web/locales/en/main.json diff --git a/locales/zh/main.json b/web/locales/zh/main.json similarity index 100% rename from locales/zh/main.json rename to web/locales/zh/main.json