Skip to content

Commit

Permalink
fix: setLocale throwing error when used in plugin (#2777)
Browse files Browse the repository at this point in the history
* fix: `setLocale` throwing error when used in plugin

* test: add regression test for `setLocale` usage from plugins
  • Loading branch information
BobbieGoede committed Feb 13, 2024
1 parent a8d5f43 commit 9596bb4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
4 changes: 4 additions & 0 deletions specs/basic_usage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,7 @@ test('dynamic parameters', async () => {
const product2dom = getDom(product2Html)
expect(product2dom.querySelector('#i18n-alt-en').href).toEqual('/products/red-mug')
})

test('(#2554) using `setLocale` in plugin should not throw an error', async () => {
await expect($fetch('/?pluginSetLocale')).resolves.to.not.toThrowError()
})
8 changes: 8 additions & 0 deletions specs/fixtures/basic_usage/plugins/set-locale-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineNuxtPlugin } from '#imports'

export default defineNuxtPlugin(async nuxtApp => {
if ('pluginSetLocale' in nuxtApp._route.query) {
const app = useNuxtApp()
await app.$i18n.setLocale('fr')
}
})
47 changes: 27 additions & 20 deletions src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,26 @@ export default defineNuxtPlugin({
notInitialSetup = false
}

const redirectPath = detectRedirect({
route: { to: route },
targetLocale: locale,
routeLocaleGetter: getLocaleFromRoute
})
const redirectPath = await nuxtContext.runWithContext(() =>
detectRedirect({
route: { to: route },
targetLocale: locale,
routeLocaleGetter: getLocaleFromRoute
})
)
__DEBUG__ && console.log('redirectPath on setLocale', redirectPath)

await navigate(
{
i18n,
redirectPath,
locale,
route
},
{ enableNavigate: true }
await nuxtContext.runWithContext(
async () =>
await navigate(
{
i18n,
redirectPath,
locale,
route
},
{ enableNavigate: true }
)
)
}
composer.differentDomains = nuxtI18nOptions.differentDomains
Expand Down Expand Up @@ -403,17 +408,19 @@ export default defineNuxtPlugin({
notInitialSetup = false
}

const redirectPath = detectRedirect({
route: { to, from },
targetLocale: locale,
routeLocaleGetter: nuxtI18nOptions.strategy === 'no_prefix' ? () => locale : getLocaleFromRoute,
calledWithRouting: true
})
const redirectPath = await nuxtContext.runWithContext(() =>
detectRedirect({
route: { to, from },
targetLocale: locale,
routeLocaleGetter: nuxtI18nOptions.strategy === 'no_prefix' ? () => locale : getLocaleFromRoute,
calledWithRouting: true
})
)
__DEBUG__ && console.log('redirectPath on locale-changing middleware', redirectPath)

routeChangeCount++

return navigate({ i18n, redirectPath, locale, route: to })
return await nuxtContext.runWithContext(async () => navigate({ i18n, redirectPath, locale, route: to }))
}),
{ global: true }
)
Expand Down

0 comments on commit 9596bb4

Please sign in to comment.