♻️ Refactor configuration service

This commit is contained in:
2025-05-28 00:37:18 +08:00
parent 72a222f932
commit 5f102edcf7
18 changed files with 721 additions and 657 deletions

View File

@@ -2,7 +2,7 @@ import {createI18n} from 'vue-i18n';
import messages from './locales';
import { ConfigService } from '@/../bindings/voidraft/internal/services';
import { LanguageType } from '@/../bindings/voidraft/internal/models';
import { useConfigStore } from '@/stores/configStore';
import { ConfigUtils } from '@/utils/configUtils';
// 定义支持的语言类型
export type SupportedLocaleType = 'zh-CN' | 'en-US';
@@ -40,40 +40,41 @@ const i18n = createI18n({
messages
});
// 立即从后端获取语言设置
ConfigService.GetLanguage().then(lang => {
if (lang) {
i18n.global.locale = lang as any;
// 从后端获取语言设置
const initializeLanguage = async () => {
try {
// 使用新的配置服务方法获取语言设置
const language = await ConfigService.Get('editor.language') as LanguageType;
if (language) {
const frontendLocale = ConfigUtils.backendLanguageToFrontend(language);
i18n.global.locale = frontendLocale as any;
}
} catch (error) {
console.error('Failed to get language from backend:', error);
// 如果获取失败,使用浏览器语言作为后备
const browserLang = getBrowserLanguage();
i18n.global.locale = browserLang as any;
}
}).catch(error => {
console.error('Failed to get language from backend:', error);
// 如果获取失败,使用浏览器语言作为后备
const browserLang = getBrowserLanguage();
i18n.global.locale = browserLang as any;
});
};
// 立即初始化语言
initializeLanguage();
// 切换语言的方法
export const setLocale = (locale: SupportedLocaleType) => {
export const setLocale = async (locale: SupportedLocaleType) => {
if (SUPPORTED_LOCALES.some(l => l.code === locale)) {
// 更新后端配置
ConfigService.SetLanguage(locale as LanguageType)
.then(() => {
i18n.global.locale = locale;
document.documentElement.setAttribute('lang', locale);
// 同时更新configStore中的语言设置
try {
const configStore = useConfigStore();
if (configStore.configLoaded) {
configStore.config.language = locale as LanguageType;
}
} catch (error) {
console.error('Failed to update configStore language:', error);
}
})
.catch(error => {
console.error('Failed to set language:', error);
});
try {
// 转换为后端语言类型
const backendLanguage = ConfigUtils.frontendLanguageToBackend(locale);
// 使用新的配置服务方法设置语言
await ConfigService.Set('editor.language', backendLanguage);
// 更新前端语言
i18n.global.locale = locale;
} catch (error) {
console.error('Failed to set language:', error);
}
}
};

View File

@@ -26,7 +26,9 @@ export default {
fontSizeFixed: 'Font size ({value}) has been corrected to {fixed}',
tabSizeFixed: 'Tab size ({value}) has been corrected to {fixed}',
tabTypeFixed: 'Tab type ({value}) is invalid, corrected to spaces',
alwaysOnTopFailed: 'Failed to set window always on top'
alwaysOnTopFailed: 'Failed to set window always on top',
languageChanged: 'Language setting updated',
languageChangeFailed: 'Failed to update language setting'
},
languages: {
'zh-CN': '简体中文',
@@ -44,4 +46,37 @@ export default {
saveFailed: 'Failed to update save settings'
}
},
settings: {
title: 'Settings',
general: 'General',
editing: 'Editor',
appearance: 'Appearance',
keyBindings: 'Key Bindings',
updates: 'Updates',
comingSoon: 'Coming Soon...',
save: 'Save',
reset: 'Reset',
globalHotkey: 'Global Keyboard Shortcuts',
enableGlobalHotkey: 'Enable Global Hotkeys',
window: 'Window/Application',
showInSystemTray: 'Show in System Tray',
alwaysOnTop: 'Always on Top',
bufferFiles: 'Buffer Files Path',
useCustomLocation: 'Use custom location for buffer files',
selectDirectory: 'Select Directory',
fontSize: 'Font Size',
fontSizeDescription: 'Editor font size',
tabSettings: 'Tab Settings',
tabSize: 'Tab Size',
tabType: 'Tab Type',
spaces: 'Spaces',
tabs: 'Tabs',
enableTabIndent: 'Enable Tab Indent',
language: 'Interface Language',
restartRequired: '(Restart required)',
saveOptions: 'Save Options',
autoSaveDelay: 'Auto Save Delay (ms)',
changeThreshold: 'Change Threshold',
minSaveInterval: 'Min Save Interval (ms)'
}
};

View File

@@ -26,7 +26,9 @@ export default {
fontSizeFixed: '字体大小值({value})已被修正为{fixed}',
tabSizeFixed: 'Tab大小值({value})已被修正为{fixed}',
tabTypeFixed: 'Tab类型({value})不合法,已修正为空格',
alwaysOnTopFailed: '无法设置窗口置顶状态'
alwaysOnTopFailed: '无法设置窗口置顶状态',
languageChanged: '语言设置已更新',
languageChangeFailed: '语言设置更新失败'
},
languages: {
'zh-CN': '简体中文',