fix: i18n hydration on dev
This commit is contained in:
parent
f96a9417da
commit
6b90131783
|
@ -10,8 +10,7 @@ export default defineNuxtPlugin(() => {
|
|||
{
|
||||
innerHTML: `
|
||||
;(function() {
|
||||
const handle = localStorage.getItem('${STORAGE_KEY_CURRENT_USER_HANDLE}')
|
||||
if (!handle) { return }
|
||||
const handle = localStorage.getItem('${STORAGE_KEY_CURRENT_USER_HANDLE}') || '[anonymous]'
|
||||
const allSettings = JSON.parse(localStorage.getItem('${STORAGE_KEY_SETTINGS}') || '{}')
|
||||
const settings = allSettings[handle]
|
||||
if (!settings) { return }
|
||||
|
|
|
@ -4,21 +4,15 @@ import type { LocaleObject } from 'vue-i18n-routing'
|
|||
export default defineNuxtPlugin(async (nuxt) => {
|
||||
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()
|
||||
const lang = userSettings.value.language
|
||||
const lang = $computed(() => userSettings.value.language)
|
||||
|
||||
if (process.client && !supportLanguages.includes(lang))
|
||||
const supportLanguages = (locales as LocaleObject[]).map(locale => locale.code)
|
||||
if (!supportLanguages.includes(lang))
|
||||
userSettings.value.language = getDefaultLanguage(locales as string[])
|
||||
|
||||
if (process.server) {
|
||||
if (lang !== i18n.locale)
|
||||
await setLocale(lang)
|
||||
return
|
||||
}
|
||||
|
||||
watch(() => userSettings.value.language, (lang) => {
|
||||
if (lang !== i18n.locale)
|
||||
watch([$$(lang), isHydrated], () => {
|
||||
if (isHydrated.value && lang !== i18n.locale)
|
||||
setLocale(lang)
|
||||
}, { immediate: true })
|
||||
})
|
Loading…
Reference in a new issue