2024-11-24 16:36:16 +03:00
|
|
|
import type { Locale } from '#i18n'
|
|
|
|
|
2024-02-24 15:24:21 +03:00
|
|
|
export default defineNuxtPlugin(async (nuxt) => {
|
|
|
|
const t = nuxt.vueApp.config.globalProperties.$t
|
|
|
|
const d = nuxt.vueApp.config.globalProperties.$d
|
|
|
|
const n = nuxt.vueApp.config.globalProperties.$n
|
|
|
|
|
|
|
|
nuxt.vueApp.config.globalProperties.$t = wrapI18n(t)
|
|
|
|
nuxt.vueApp.config.globalProperties.$d = wrapI18n(d)
|
|
|
|
nuxt.vueApp.config.globalProperties.$n = wrapI18n(n)
|
|
|
|
|
2024-02-24 19:46:14 +03:00
|
|
|
if (import.meta.client) {
|
|
|
|
const i18n = useNuxtApp().$i18n
|
2024-02-24 15:24:21 +03:00
|
|
|
const { setLocale, locales } = i18n
|
|
|
|
const userSettings = useUserSettings()
|
2024-11-24 16:36:16 +03:00
|
|
|
const lang = computed(() => userSettings.value.language as Locale)
|
2024-02-24 15:24:21 +03:00
|
|
|
|
2024-02-24 19:46:14 +03:00
|
|
|
const supportLanguages = unref(locales).map(locale => locale.code)
|
2024-02-24 15:24:21 +03:00
|
|
|
if (!supportLanguages.includes(lang.value))
|
|
|
|
userSettings.value.language = getDefaultLanguage(supportLanguages)
|
|
|
|
|
|
|
|
if (lang.value !== i18n.locale)
|
2024-11-24 16:36:16 +03:00
|
|
|
await setLocale(userSettings.value.language as Locale)
|
2024-02-24 15:24:21 +03:00
|
|
|
|
|
|
|
watch([lang, isHydrated], () => {
|
|
|
|
if (isHydrated.value && lang.value !== i18n.locale)
|
|
|
|
setLocale(lang.value)
|
|
|
|
}, { immediate: true })
|
|
|
|
}
|
|
|
|
})
|