diff --git a/src/i18n/index.ts b/src/i18n/index.ts index d3f7450..4203c2d 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -1,14 +1,9 @@ -// src/renderer/src/i18n/index.ts -import { ref, computed } from 'vue' +// src/i18n/index.ts +import { ref, computed, watch } from 'vue' import en from './locales/en' import zhCN from './locales/zh-CN' type Messages = typeof en -type NestedKeyOf = { - [K in keyof T & string]: T[K] extends object - ? `${K}.${NestedKeyOf}` - : K -}[keyof T & string] const messages: Record = { en, @@ -19,6 +14,13 @@ const currentLocale = ref( localStorage.getItem('locale') || 'zh-CN' ) +// Force re-render trigger +const localeVersion = ref(0) + +watch(currentLocale, () => { + localeVersion.value++ +}) + export function useI18n() { const locale = computed({ get: () => currentLocale.value, @@ -29,6 +31,9 @@ export function useI18n() { }) function t(path: string, params?: Record): string { + // Access localeVersion to make t() reactive + void localeVersion.value + const keys = path.split('.') let result: any = messages[currentLocale.value] || messages.en