2023-01-12 18:52:52 +01:00
|
|
|
import type { VueI18n } from 'vue-i18n'
|
|
|
|
import type { LocaleObject } from 'vue-i18n-routing'
|
2022-12-28 04:50:29 +01:00
|
|
|
|
|
|
|
export default defineNuxtPlugin(async (nuxt) => {
|
2023-01-12 18:52:52 +01:00
|
|
|
const i18n = nuxt.vueApp.config.globalProperties.$i18n as VueI18n
|
|
|
|
const { setLocale, locales } = i18n
|
|
|
|
const supportLanguages = (locales as LocaleObject[]).map(locale => locale.code)
|
|
|
|
const userSettings = useUserSettings()
|
2023-01-15 13:23:47 +01:00
|
|
|
const lang = userSettings.value.language
|
2022-12-28 04:50:29 +01:00
|
|
|
|
2023-01-15 13:23:47 +01:00
|
|
|
if (process.client && !supportLanguages.includes(lang))
|
|
|
|
userSettings.value.language = getDefaultLanguage(locales as string[])
|
2022-12-28 04:50:29 +01:00
|
|
|
|
2023-01-15 13:23:47 +01:00
|
|
|
if (process.server) {
|
2023-01-12 18:52:52 +01:00
|
|
|
if (lang !== i18n.locale)
|
2023-01-15 13:23:47 +01:00
|
|
|
await setLocale(lang)
|
2023-01-12 18:52:52 +01:00
|
|
|
return
|
2022-12-28 04:50:29 +01:00
|
|
|
}
|
2023-01-12 18:52:52 +01:00
|
|
|
|
|
|
|
watch(() => userSettings.value.language, (lang) => {
|
|
|
|
if (lang !== i18n.locale)
|
|
|
|
setLocale(lang)
|
|
|
|
}, { immediate: true })
|
2022-12-28 04:50:29 +01:00
|
|
|
})
|