Add i18n support

This commit is contained in:
2025-04-28 11:49:58 +08:00
parent 618bae3afe
commit 0d3df25a94
8 changed files with 440 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
import {defineStore} from 'pinia';
import {ref, watch} from 'vue';
import {ref, watch, inject} from 'vue';
import {useDebounceFn} from '@vueuse/core';
import {
GetEditorConfig,
@@ -7,6 +7,8 @@ import {
UpdateEditorConfig
} from '@/../bindings/voidraft/internal/services/configservice';
import {EditorConfig, TabType} from '@/../bindings/voidraft/internal/models/models';
import {useLogStore} from './logStore';
import { useI18n } from 'vue-i18n';
// 字体大小范围
const MIN_FONT_SIZE = 12;
@@ -19,6 +21,10 @@ const MIN_TAB_SIZE = 2;
const MAX_TAB_SIZE = 8;
export const useConfigStore = defineStore('config', () => {
// 获取日志store
const logStore = useLogStore();
const { t } = useI18n();
// 配置状态
const config = ref<EditorConfig>(new EditorConfig({
fontSize: DEFAULT_FONT_SIZE,
@@ -34,11 +40,66 @@ export const useConfigStore = defineStore('config', () => {
// 从后端加载配置
async function loadConfigFromBackend() {
try {
const editorConfig = await GetEditorConfig();
config.value = editorConfig;
config.value = await GetEditorConfig();
// 验证并纠正配置
validateAndFixConfig();
configLoaded.value = true;
logStore.info(t('config.loadSuccess'));
} catch (error) {
console.error('Failed to load configuration:', error);
logStore.error(t('config.loadFailed'));
}
}
// 验证配置是否在合理范围内,并修正无效值
function validateAndFixConfig() {
let hasChanges = false;
// 验证字体大小
if (config.value.fontSize < MIN_FONT_SIZE || config.value.fontSize > MAX_FONT_SIZE) {
const oldValue = config.value.fontSize;
config.value.fontSize = oldValue < MIN_FONT_SIZE ? MIN_FONT_SIZE :
oldValue > MAX_FONT_SIZE ? MAX_FONT_SIZE :
DEFAULT_FONT_SIZE;
logStore.warning(t('config.fontSizeFixed', {
value: oldValue,
fixed: config.value.fontSize
}));
hasChanges = true;
}
// 验证Tab大小
if (config.value.tabSize < MIN_TAB_SIZE || config.value.tabSize > MAX_TAB_SIZE) {
const oldValue = config.value.tabSize;
config.value.tabSize = oldValue < MIN_TAB_SIZE ? MIN_TAB_SIZE :
oldValue > MAX_TAB_SIZE ? MAX_TAB_SIZE :
DEFAULT_TAB_SIZE;
logStore.warning(t('config.tabSizeFixed', {
value: oldValue,
fixed: config.value.tabSize
}));
hasChanges = true;
}
// 验证TabType是否合法
const validTabTypes = [TabType.TabTypeSpaces, TabType.TabTypeTab];
if (!validTabTypes.includes(config.value.tabType)) {
const oldValue = config.value.tabType;
config.value.tabType = TabType.TabTypeSpaces;
logStore.warning(t('config.tabTypeFixed', { value: oldValue }));
hasChanges = true;
}
// 如果配置被修正,保存回后端
if (hasChanges && configLoaded.value) {
saveConfigToBackend();
}
}
@@ -46,8 +107,10 @@ export const useConfigStore = defineStore('config', () => {
const saveConfigToBackend = useDebounceFn(async () => {
try {
await UpdateEditorConfig(config.value);
logStore.info(t('config.saveSuccess'));
} catch (error) {
console.error('Failed to save configuration:', error);
logStore.error(t('config.saveFailed'));
}
}, 500); // 500ms防抖
@@ -110,8 +173,14 @@ export const useConfigStore = defineStore('config', () => {
// 重置为默认配置
async function resetToDefaults() {
await ResetToDefault();
await loadConfigFromBackend();
try {
await ResetToDefault();
await loadConfigFromBackend();
logStore.info(t('config.resetSuccess'));
} catch (error) {
console.error('Failed to reset configuration:', error);
logStore.error(t('config.resetFailed'));
}
}
return {
// 状态