Skip to content

Commit

Permalink
fix: i18n hydration on dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 15, 2023
1 parent f96a941 commit 6b90131
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
3 changes: 1 addition & 2 deletions plugins/setup-head-script.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
16 changes: 5 additions & 11 deletions plugins/setup-i18n.ts → plugins/setup-i18n.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})

0 comments on commit 6b90131

Please sign in to comment.