Skip to content

Commit afd9ab2

Browse files
borgholkhaled.borghol
andauthored
fix: alwaysRedirect does not work with redirectOn 'all' and 'no prefix' (#1884)
* fix always redirect * fix --------- Co-authored-by: khaled.borghol <khaled.borghol@abyatonline.com>
1 parent 183ae15 commit afd9ab2

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { test, expect } from 'vitest'
2+
import { fileURLToPath } from 'node:url'
3+
import { setup, url, createPage } from '@nuxt/test-utils'
4+
import { getText } from '../../helper'
5+
6+
await setup({
7+
rootDir: fileURLToPath(new URL(`../../fixtures/basic`, import.meta.url)),
8+
browser: true,
9+
// overrides
10+
nuxtConfig: {
11+
i18n: {
12+
strategy: 'prefix_and_default',
13+
detectBrowserLanguage: {
14+
alwaysRedirect: true,
15+
redirectOn: 'all'
16+
}
17+
}
18+
}
19+
})
20+
21+
test('alwaysRedirect: all', async () => {
22+
const blog = url('/blog/article')
23+
const page = await createPage(undefined, { locale: 'en' }) // set browser locale
24+
await page.goto(blog)
25+
26+
// detect locale from navigator language
27+
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en')
28+
29+
// click `fr` lang switch link
30+
await page.locator('#set-locale-link-fr').click()
31+
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')
32+
33+
// go to `en` home page
34+
await page.goto(blog)
35+
expect(page.url().endsWith('/fr/blog/article'))
36+
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')
37+
})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { test, expect } from 'vitest'
2+
import { fileURLToPath } from 'node:url'
3+
import { setup, url, createPage } from '@nuxt/test-utils'
4+
import { getText } from '../../helper'
5+
6+
await setup({
7+
rootDir: fileURLToPath(new URL(`../../fixtures/fallback`, import.meta.url)),
8+
browser: true,
9+
// overrides
10+
nuxtConfig: {
11+
i18n: {
12+
strategy: 'prefix_and_default',
13+
detectBrowserLanguage: {
14+
alwaysRedirect: true,
15+
redirectOn: 'no prefix'
16+
}
17+
}
18+
}
19+
})
20+
21+
test('alwaysRedirect: no prefix', async () => {
22+
const blog = url('/about')
23+
const page = await createPage(undefined, { locale: 'en' }) // set browser locale
24+
await page.goto(blog)
25+
26+
// detect locale from navigator language
27+
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en')
28+
29+
// click `fr` lang switch link
30+
await page.locator('#set-locale-link-fr').click()
31+
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')
32+
33+
// go to `en` home page
34+
await page.goto(url('/ja/about'))
35+
expect(page.url().endsWith('/ja/about'))
36+
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('ja')
37+
38+
await page.goto(url('/about'))
39+
expect(page.url().endsWith('/ja/about'))
40+
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('ja')
41+
})

src/runtime/internal.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,11 @@ export function detectBrowserLanguage<Context extends NuxtApp = NuxtApp>(
406406
if (finalLocale !== vueI18nLocale /* && path !== '/'*/) {
407407
__DEBUG__ && console.log('detectBrowserLanguage: finalLocale !== vueI18nLocale', finalLocale)
408408
return { locale: finalLocale, stat: true, from: localeFrom }
409-
} else {
410-
if (alwaysRedirect && path === '/') {
409+
} else if (alwaysRedirect) {
410+
const redirectOnRoot = path === '/'
411+
const redirectOnAll = redirectOn === 'all'
412+
const redirectOnNoPrefix = redirectOn === 'no prefix' && !path.match(getLocalesRegex(localeCodes as string[]))
413+
if (redirectOnRoot || redirectOnAll || redirectOnNoPrefix) {
411414
return { locale: finalLocale, stat: true, from: localeFrom }
412415
}
413416
}

0 commit comments

Comments
 (0)