diff --git a/electron/welcome/lang/en.ts b/electron/welcome/lang/en.ts index 4e11c32d2f16787a2e5d024bef39f1e486abc76a..d79422a9f8b20e43ec56bb93291053f910356d2d 100644 --- a/electron/welcome/lang/en.ts +++ b/electron/welcome/lang/en.ts @@ -8,6 +8,7 @@ export default { pleaseInput: 'Please Input', validUrl: 'Please enter a valid URL', validationFailure: 'Validation failure', + connectionFailed: 'Connection failed', }, localDeploy: { model: 'Large model', @@ -24,6 +25,14 @@ export default { stopInstall: 'Stop installation', complete: 'Complete', retry: 'Retry', + validationFailed: 'Validation Failed', + authError: 'Authentication failed, please check if the API Key is correct', + functionCallNotSupported: + 'Function Call not supported, please use a model that supports this feature', + connectionError: 'Connection failed, please check if the URL is correct', + modelError: 'Model validation failed', + llmValidationFailed: 'LLM validation failed', + embeddingValidationFailed: 'Embedding model validation failed', }, onlineService: { serviceUrl: 'Backend Service Links', diff --git a/electron/welcome/lang/index.ts b/electron/welcome/lang/index.ts index 736df67cb4b0bec3e07de197972c362b5d6c6eea..2c1acbd4894b86151d3a0ca5b4c36a025e6e7a90 100644 --- a/electron/welcome/lang/index.ts +++ b/electron/welcome/lang/index.ts @@ -3,7 +3,130 @@ import { createI18n } from 'vue-i18n'; import zh from './zh'; import en from './en'; -const locale = 'zh'; +/** + * 开发模式日志输出 + * 只在开发环境下输出日志 + */ +function devLog(...args: any[]) { + if (import.meta.env.DEV || process.env.NODE_ENV === 'development') { + console.log(...args); + } +} + +/** + * 开发模式警告输出 + * 只在开发环境下输出警告 + */ +function devWarn(...args: any[]) { + if (import.meta.env.DEV || process.env.NODE_ENV === 'development') { + console.warn(...args); + } +} + +/** + * 检测系统语言 + * 支持多种操作系统的语言检测 + * @returns {'zh' | 'en'} 语言代码 'zh' 或 'en' + */ +function detectSystemLanguage(): 'zh' | 'en' { + let systemLanguage: 'zh' | 'en' = 'zh'; // 默认中文 + + try { + // 优先级1: 检查 Electron 环境中的系统信息 + if (typeof window !== 'undefined' && window.eulercopilotWelcome?.system) { + const { platform, env } = window.eulercopilotWelcome.system; + + devLog(`检测到系统平台: ${platform}`); + + // 根据不同操作系统检测语言 + if (platform === 'win32') { + // Windows 系统语言检测 + const winLang = env.LANG || env.LC_ALL || env.LANGUAGE || ''; + if ( + winLang.toLowerCase().includes('zh') || + winLang.toLowerCase().includes('chinese') || + winLang.toLowerCase().includes('chs') || + winLang.toLowerCase().includes('cn') + ) { + systemLanguage = 'zh'; + } else if (winLang && !winLang.toLowerCase().includes('zh')) { + systemLanguage = 'en'; + } + } else if (platform === 'darwin') { + // macOS 系统语言检测 + const macLang = + env.LANG || env.LC_ALL || env.LC_MESSAGES || env.LANGUAGE || ''; + if ( + macLang.toLowerCase().includes('zh') || + macLang.toLowerCase().includes('chinese') || + macLang.toLowerCase().includes('cn') + ) { + systemLanguage = 'zh'; + } else if (macLang && !macLang.toLowerCase().includes('zh')) { + systemLanguage = 'en'; + } + } else if (platform === 'linux') { + // Linux 系统语言检测 + const linuxLang = + env.LANG || env.LC_ALL || env.LC_MESSAGES || env.LANGUAGE || ''; + if ( + linuxLang.toLowerCase().includes('zh') || + linuxLang.toLowerCase().includes('chinese') || + linuxLang.toLowerCase().includes('cn') + ) { + systemLanguage = 'zh'; + } else if (linuxLang && !linuxLang.toLowerCase().includes('zh')) { + systemLanguage = 'en'; + } + } + + devLog(`根据环境变量检测到语言: ${systemLanguage}`); + return systemLanguage; // 如果 Electron 环境可用,直接返回结果 + } + + // 优先级2: 浏览器语言检测 (作为后备方案) + if (typeof navigator !== 'undefined' && navigator.language) { + const browserLang = navigator.language.toLowerCase(); + + devLog(`浏览器语言: ${browserLang}`); + + // 检测是否为中文相关语言 + if ( + browserLang.startsWith('zh') || + browserLang.includes('china') || + browserLang.includes('chinese') || + browserLang === 'zh-cn' || + browserLang === 'zh-tw' || + browserLang === 'zh-hk' + ) { + systemLanguage = 'zh'; + } + // 检测是否为英文相关语言 + else if ( + browserLang.startsWith('en') || + browserLang.includes('english') || + browserLang.includes('us') || + browserLang.includes('gb') + ) { + systemLanguage = 'en'; + } + // 其他语言默认使用英文 + else { + systemLanguage = 'en'; + } + } + + devLog(`最终检测到的系统语言: ${systemLanguage}`); + } catch (error) { + devWarn('检测系统语言失败,使用默认中文:', error); + systemLanguage = 'zh'; + } + + return systemLanguage; +} + +// 检测系统语言,默认中文 +const locale = detectSystemLanguage(); const i18n_welcome = createI18n({ legacy: false, // 设置为 false,启用 composition API 模式 @@ -13,4 +136,41 @@ const i18n_welcome = createI18n({ en, }, }); -export default i18n_welcome; \ No newline at end of file + +/** + * 在 Electron 环境准备好后重新检测语言 + * 这个函数会在应用初始化后调用,确保能获取到正确的系统语言 + */ +export function redetectLanguageOnReady() { + // 等待一小段时间让 Electron preload 脚本完成初始化 + setTimeout(() => { + const newLocale = detectSystemLanguage(); + if (newLocale !== i18n_welcome.global.locale.value) { + devLog( + `重新检测到语言变化,从 ${i18n_welcome.global.locale.value} 切换到 ${newLocale}`, + ); + i18n_welcome.global.locale.value = newLocale; + } + }, 100); +} + +/** + * 切换语言 + * @param {string} newLocale 新的语言代码 'zh' 或 'en' + */ +export function changeLanguage(newLocale: 'zh' | 'en') { + if (i18n_welcome.global.locale) { + i18n_welcome.global.locale.value = newLocale; + devLog(`语言已切换到: ${newLocale}`); + } +} + +/** + * 获取当前语言 + * @returns {'zh' | 'en'} 当前语言代码 + */ +export function getCurrentLanguage(): 'zh' | 'en' { + return i18n_welcome.global.locale.value as 'zh' | 'en'; +} + +export default i18n_welcome; diff --git a/electron/welcome/lang/zh.ts b/electron/welcome/lang/zh.ts index 8ee6c69c8f2cf46776cb6cce87b76bda7df8de5a..6bf89e1af890edfd6c2e0a7295c9351b9b937144 100644 --- a/electron/welcome/lang/zh.ts +++ b/electron/welcome/lang/zh.ts @@ -8,6 +8,7 @@ export default { pleaseInput: '请输入', validUrl: '请输入有效的 URL', validationFailure: '检验失败', + connectionFailed: '连接失败', }, localDeploy: { model: '大模型', @@ -24,6 +25,13 @@ export default { stopInstall: '停止安装', complete: '完成', retry: '重试', + validationFailed: '校验失败', + authError: '鉴权失败,请检查 API Key 是否正确', + functionCallNotSupported: '不支持 Function Call,请使用支持此功能的模型', + connectionError: '连接失败,请检查 URL 是否正确', + modelError: '模型验证失败', + llmValidationFailed: '大模型校验失败', + embeddingValidationFailed: 'Embedding 模型校验失败', }, onlineService: { serviceUrl: '后端服务链接', diff --git a/electron/welcome/localDeploy.vue b/electron/welcome/localDeploy.vue index e9fde6021c7db08d489c928e05373ea5ded71cb5..2a55e26edb0981c34cd1259e34ce379bbd3ff123 100644 --- a/electron/welcome/localDeploy.vue +++ b/electron/welcome/localDeploy.vue @@ -20,7 +20,28 @@ >
{{ $t('localDeploy.model') }} - success + validating + success + error
@@ -64,7 +100,28 @@ >
{{ $t('localDeploy.embeddingModel') }} - success + validating + success + error