2023-01-12 20:52:52 +03:00
|
|
|
import type { VueI18n } from 'vue-i18n'
|
|
|
|
import type { LocaleObject } from 'vue-i18n-routing'
|
2022-12-28 06:50:29 +03:00
|
|
|
|
|
|
|
export default defineNuxtPlugin(async (nuxt) => {
|
2023-01-12 20:52:52 +03:00
|
|
|
const i18n = nuxt.vueApp.config.globalProperties.$i18n as VueI18n
|
|
|
|
const { setLocale, locales } = i18n
|
|
|
|
const userSettings = useUserSettings()
|
2023-01-15 18:15:03 +03:00
|
|
|
const lang = $computed(() => userSettings.value.language)
|
2022-12-28 06:50:29 +03:00
|
|
|
|
2023-01-15 18:15:03 +03:00
|
|
|
const supportLanguages = (locales as LocaleObject[]).map(locale => locale.code)
|
|
|
|
if (!supportLanguages.includes(lang))
|
2023-02-02 13:36:35 +03:00
|
|
|
userSettings.value.language = getDefaultLanguage(supportLanguages)
|
2022-12-28 06:50:29 +03:00
|
|
|
|
2023-01-15 18:15:03 +03:00
|
|
|
watch([$$(lang), isHydrated], () => {
|
|
|
|
if (isHydrated.value && lang !== i18n.locale)
|
2023-01-12 20:52:52 +03:00
|
|
|
setLocale(lang)
|
|
|
|
}, { immediate: true })
|
2022-12-28 06:50:29 +03:00
|
|
|
})
|