1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-19 07:19:58 +00:00
elk/plugins/setup-i18n.client.ts

22 lines
765 B
TypeScript
Raw Normal View History

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