diff --git a/specs/fixtures/issues/2758/app.vue b/specs/fixtures/issues/2758/app.vue
new file mode 100644
index 000000000..6b95f8b2f
--- /dev/null
+++ b/specs/fixtures/issues/2758/app.vue
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/specs/fixtures/issues/2758/nuxt.config.ts b/specs/fixtures/issues/2758/nuxt.config.ts
new file mode 100644
index 000000000..8a74d8086
--- /dev/null
+++ b/specs/fixtures/issues/2758/nuxt.config.ts
@@ -0,0 +1,23 @@
+// https://nuxt.com/docs/guide/directory-structure/nuxt.config
+export default defineNuxtConfig({
+ modules: ['@nuxtjs/i18n'],
+ debug: false,
+ i18n: {
+ defaultLocale: 'en',
+ strategy: 'prefix',
+ rootRedirect: {
+ statusCode: 418,
+ path: 'test-route'
+ },
+ locales: [
+ {
+ code: 'en',
+ iso: 'en-US'
+ },
+ {
+ code: 'fr',
+ iso: 'fr-FR'
+ }
+ ]
+ }
+})
diff --git a/specs/fixtures/issues/2758/package.json b/specs/fixtures/issues/2758/package.json
new file mode 100644
index 000000000..c151d99b2
--- /dev/null
+++ b/specs/fixtures/issues/2758/package.json
@@ -0,0 +1,14 @@
+{
+ "name": "nuxt3-test-basic-usage-layers",
+ "private": true,
+ "type": "module",
+ "scripts": {
+ "dev": "nuxi dev",
+ "build": "nuxt build",
+ "start": "node .output/server/index.mjs"
+ },
+ "devDependencies": {
+ "@nuxtjs/i18n": "latest",
+ "nuxt": "latest"
+ }
+}
diff --git a/specs/fixtures/issues/2758/pages/index.vue b/specs/fixtures/issues/2758/pages/index.vue
new file mode 100644
index 000000000..bdd7d3e39
--- /dev/null
+++ b/specs/fixtures/issues/2758/pages/index.vue
@@ -0,0 +1,3 @@
+
+ Home
+
diff --git a/specs/fixtures/issues/2758/pages/test-route.vue b/specs/fixtures/issues/2758/pages/test-route.vue
new file mode 100644
index 000000000..78ed6aa59
--- /dev/null
+++ b/specs/fixtures/issues/2758/pages/test-route.vue
@@ -0,0 +1,3 @@
+
+ Test route
+
diff --git a/specs/issues/2758.spec.ts b/specs/issues/2758.spec.ts
new file mode 100644
index 000000000..ad95279c2
--- /dev/null
+++ b/specs/issues/2758.spec.ts
@@ -0,0 +1,15 @@
+import { test, expect, describe } from 'vitest'
+import { fileURLToPath } from 'node:url'
+import { setup, fetch, url } from '../utils'
+
+describe('#2758', async () => {
+ await setup({
+ rootDir: fileURLToPath(new URL(`../fixtures/issues/2758`, import.meta.url))
+ })
+
+ test('`statusCode` in `rootRedirect` should work with strategy "prefix"', async () => {
+ const res = await fetch(url('/'))
+ expect(res.status).toEqual(418)
+ expect(res.headers.get('location')).toEqual('/en/test-route')
+ })
+})
diff --git a/src/runtime/plugins/i18n.ts b/src/runtime/plugins/i18n.ts
index e922b823d..f9437b64b 100644
--- a/src/runtime/plugins/i18n.ts
+++ b/src/runtime/plugins/i18n.ts
@@ -178,6 +178,7 @@ export default defineNuxtPlugin({
async () =>
await navigate(
{
+ nuxtApp: nuxtContext,
i18n,
redirectPath,
locale,
@@ -424,7 +425,9 @@ export default defineNuxtPlugin({
routeChangeCount++
- return await nuxtContext.runWithContext(async () => navigate({ i18n, redirectPath, locale, route: to }))
+ return await nuxtContext.runWithContext(async () =>
+ navigate({ nuxtApp: nuxtContext, i18n, redirectPath, locale, route: to })
+ )
}),
{ global: true }
)
diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts
index 2a82e819f..3e364ca8c 100644
--- a/src/runtime/utils.ts
+++ b/src/runtime/utils.ts
@@ -321,6 +321,7 @@ function isRootRedirectOptions(rootRedirect: unknown): rootRedirect is RootRedir
const useRedirectState = () => useState(NUXT_I18N_MODULE_ID + ':redirect', () => '')
type NavigateArgs = {
+ nuxtApp: NuxtApp
i18n: I18n
redirectPath: string
locale: string
@@ -339,7 +340,7 @@ export async function navigate(
const differentDomains = nuxtI18nOptions.differentDomains ?? nuxtI18nOptionsDefault.differentDomains
const skipSettingLocaleOnNavigate =
nuxtI18nOptions.skipSettingLocaleOnNavigate ?? nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate
- const { i18n, locale, route } = args
+ const { nuxtApp, i18n, locale, route } = args
let { redirectPath } = args
__DEBUG__ &&
@@ -360,6 +361,7 @@ export async function navigate(
redirectPath = '/' + rootRedirect.path
status = rootRedirect.statusCode
}
+ redirectPath = nuxtApp.$localePath(redirectPath, locale)
__DEBUG__ && console.log('navigate: rootRedirect mode redirectPath -> ', redirectPath, ' status -> ', status)
return _navigate(redirectPath, status)
}