Skip to content

Commit

Permalink
fix: replace deprecated onBeforeLanguageSwitch with Nuxt hook
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed May 20, 2024
1 parent e16ef38 commit ee8202c
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions plugins/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ export default defineNuxtPlugin(async (nuxtApp) => {
const site = useSite()
const i18n = nuxtApp.$i18n as NuxtApp['$i18n']

// Initially load the site data
if (import.meta.server) {
// Load the site data server-side
await updateSite()
}

// Update the site data on locale change
if (import.meta.client) {
i18n.onBeforeLanguageSwitch = async (
oldLocale: string,
newLocale: string,
) => {
if (oldLocale !== newLocale) {
if (import.meta.dev)
// eslint-disable-next-line no-console
console.log('Locale changed:', oldLocale, '->', newLocale)
} else if (import.meta.client) {
// Update the site data on locale change
nuxtApp.hook(
'i18n:beforeLocaleSwitch',
async ({ oldLocale, newLocale }) => {
if (oldLocale !== newLocale) {
if (import.meta.dev)
// eslint-disable-next-line no-console
console.log('Locale changed:', oldLocale, '->', newLocale)

await updateSite(newLocale)
}
}
await updateSite(newLocale)
}
},
)
}

async function updateSite(newLocale?: string) {
Expand Down

0 comments on commit ee8202c

Please sign in to comment.