|
|
@ -1,5 +1,7 @@ |
|
|
|
import { defineNuxtPlugin, useApi, useGlobal } from '#imports' |
|
|
|
import { loadLocaleMessages, setI18nLanguage } from './a.i18n' |
|
|
|
import { loadLocaleMessages, setI18nLanguage } from '~/plugins/a.i18n' |
|
|
|
import { defineNuxtPlugin, useApi, useGlobal, watch } from '#imports' |
|
|
|
|
|
|
|
import { useUnstorage } from '~/composables/useUnstorage' |
|
|
|
|
|
|
|
import type { StoredState } from '~/composables/useGlobal' |
|
|
|
import { Language, LanguageAlias } from '~/lib' |
|
|
|
import { Language, LanguageAlias } from '~/lib' |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -14,9 +16,11 @@ import { Language, LanguageAlias } from '~/lib' |
|
|
|
* console.log($state.lang.value) // 'en'
|
|
|
|
* console.log($state.lang.value) // 'en'
|
|
|
|
* ``` |
|
|
|
* ``` |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
export default defineNuxtPlugin(async () => { |
|
|
|
export default defineNuxtPlugin(async (nuxt) => { |
|
|
|
const state = useGlobal() |
|
|
|
const state = useGlobal() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nuxt.provide('state', state) |
|
|
|
|
|
|
|
|
|
|
|
const { api } = useApi({ useGlobalInstance: true }) |
|
|
|
const { api } = useApi({ useGlobalInstance: true }) |
|
|
|
|
|
|
|
|
|
|
|
let currentLang = state.lang.value |
|
|
|
let currentLang = state.lang.value |
|
|
@ -30,6 +34,17 @@ export default defineNuxtPlugin(async () => { |
|
|
|
/** set i18n locale to stored language */ |
|
|
|
/** set i18n locale to stored language */ |
|
|
|
await setI18nLanguage(currentLang) |
|
|
|
await setI18nLanguage(currentLang) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const storedState = await useUnstorage.getItem('global:state') |
|
|
|
|
|
|
|
state.storage.value = { ...state.storage.value, ...(storedState as StoredState) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
watch( |
|
|
|
|
|
|
|
state.storage, |
|
|
|
|
|
|
|
(nextState) => { |
|
|
|
|
|
|
|
useUnstorage.setItem('global:state', nextState) |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ deep: true, flush: 'post' }, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
state.appInfo.value = await api.utils.appInfo() |
|
|
|
state.appInfo.value = await api.utils.appInfo() |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |