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