@@ -3,6 +3,7 @@ import { ref, watch } from 'vue'
33import { parse , stringify } from 'yaml'
44
55import {
6+ ReadDir ,
67 ReadFile ,
78 WriteFile ,
89 WindowSetSystemDefaultTheme ,
@@ -17,6 +18,7 @@ import {
1718 DefaultFontFamily ,
1819 DefaultTestURL ,
1920 UserFilePath ,
21+ LocalesFilePath ,
2022} from '@/constant/app'
2123import { CorePidFilePath , DefaultConnections , DefaultCoreConfig } from '@/constant/kernel'
2224import {
@@ -29,15 +31,12 @@ import {
2931 ControllerCloseMode ,
3032 Branch ,
3133} from '@/enums/app'
32- import i18n from '@/lang'
33- import { debounce , updateTrayMenus , APP_TITLE , ignoredError , APP_VERSION } from '@/utils'
34+ import i18n , { loadLocaleMessages } from '@/lang'
35+ import { debounce , updateTrayMenus , APP_TITLE , ignoredError , APP_VERSION , sleep } from '@/utils'
3436
3537import type { AppSettings } from '@/types/app'
3638
3739export const useAppSettingsStore = defineStore ( 'app-settings' , ( ) => {
38- let firstOpen = true
39- let latestUserConfig = ''
40-
4140 const themeMode = ref < Theme . Dark | Theme . Light > ( Theme . Light )
4241
4342 const app = ref < AppSettings > ( {
@@ -91,9 +90,46 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
9190 WriteFile ( UserFilePath , config )
9291 } , 500 )
9392
93+ const localesLoading = ref ( false )
94+ const locales = ref < { label : string ; value : string } [ ] > ( [ ] )
95+ const loadLocales = async ( delay = false ) => {
96+ localesLoading . value = true
97+ locales . value = [
98+ {
99+ label : 'settings.lang.zh' ,
100+ value : Lang . ZH ,
101+ } ,
102+ {
103+ label : 'settings.lang.en' ,
104+ value : Lang . EN ,
105+ } ,
106+ ]
107+ const dirs = await ignoredError ( ReadDir , LocalesFilePath )
108+ if ( dirs ) {
109+ const files = dirs . flatMap ( ( file ) => {
110+ if ( file . isDir ) return [ ]
111+ const [ name , ext ] = file . name . split ( '.' )
112+ return name && ext === 'json' ? { label : name , value : name } : [ ]
113+ } )
114+ locales . value . push ( ...files )
115+ }
116+ delay && ( await sleep ( 200 ) )
117+ localesLoading . value = false
118+ }
119+
120+ let latestUserSettings : string
121+
94122 const setupAppSettings = async ( ) => {
95123 const data = await ignoredError ( ReadFile , UserFilePath )
96- data && ( app . value = Object . assign ( app . value , parse ( data ) ) )
124+ if ( data ) {
125+ const settings = parse ( data )
126+ latestUserSettings = stringify ( settings )
127+ app . value = Object . assign ( app . value , settings )
128+ } else {
129+ latestUserSettings = ''
130+ }
131+
132+ await loadLocales ( )
97133
98134 if ( ! app . value . kernel . main ) {
99135 app . value . kernel . main = DefaultCoreConfig ( )
@@ -132,10 +168,6 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
132168 // @ts -expect-error(Deprecated)
133169 delete app . value . kernel . pid
134170 }
135-
136- firstOpen = ! ! data
137-
138- updateAppSettings ( app . value )
139171 }
140172
141173 const mediaQueryList = window . matchMedia ( '(prefers-color-scheme: dark)' )
@@ -157,6 +189,7 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
157189 }
158190
159191 const updateAppSettings = ( settings : AppSettings ) => {
192+ loadLocaleMessages ( settings . lang )
160193 i18n . global . locale . value = settings . lang
161194 themeMode . value =
162195 settings . theme === Theme . Auto
@@ -177,20 +210,16 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
177210 ( settings ) => {
178211 updateAppSettings ( settings )
179212
180- if ( ! firstOpen ) {
181- const lastModifiedConfig = stringify ( settings )
182- if ( latestUserConfig !== lastModifiedConfig ) {
183- saveAppSettings ( lastModifiedConfig ) . then ( ( ) => {
184- latestUserConfig = lastModifiedConfig
185- } )
186- } else {
187- saveAppSettings . cancel ( )
188- }
213+ const lastModifiedSettings = stringify ( settings )
214+ if ( latestUserSettings !== undefined && latestUserSettings !== lastModifiedSettings ) {
215+ saveAppSettings ( lastModifiedSettings ) . then ( ( ) => {
216+ latestUserSettings = lastModifiedSettings
217+ } )
218+ } else {
219+ saveAppSettings . cancel ( )
189220 }
190-
191- firstOpen = false
192221 } ,
193- { deep : true } ,
222+ { deep : true , immediate : true } ,
194223 )
195224
196225 window . addEventListener (
@@ -208,11 +237,16 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
208237 )
209238
210239 watch (
211- [ themeMode , ( ) => app . value . color , ( ) => app . value . lang , ( ) => app . value . addPluginToMenu ] ,
240+ [
241+ themeMode ,
242+ locales ,
243+ ( ) => app . value . color ,
244+ ( ) => app . value . lang ,
245+ ( ) => app . value . addPluginToMenu ,
246+ ] ,
212247 updateTrayMenus ,
213248 )
214-
215249 watch ( themeMode , setAppTheme , { immediate : true } )
216250
217- return { setupAppSettings, app, themeMode }
251+ return { setupAppSettings, app, themeMode, locales , localesLoading , loadLocales }
218252} )
0 commit comments