Skip to content

Commit

Permalink
fix: alwaysRedirect does not work with redirectOn 'all' and 'no prefi…
Browse files Browse the repository at this point in the history
…x' (#1884)

* fix always redirect

* fix

---------

Co-authored-by: khaled.borghol <khaled.borghol@abyatonline.com>
  • Loading branch information
borghol and khaled.borghol authored Feb 25, 2023
1 parent 183ae15 commit afd9ab2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { test, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, url, createPage } from '@nuxt/test-utils'
import { getText } from '../../helper'

await setup({
rootDir: fileURLToPath(new URL(`../../fixtures/basic`, import.meta.url)),
browser: true,
// overrides
nuxtConfig: {
i18n: {
strategy: 'prefix_and_default',
detectBrowserLanguage: {
alwaysRedirect: true,
redirectOn: 'all'
}
}
}
})

test('alwaysRedirect: all', async () => {
const blog = url('/blog/article')
const page = await createPage(undefined, { locale: 'en' }) // set browser locale
await page.goto(blog)

// detect locale from navigator language
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en')

// click `fr` lang switch link
await page.locator('#set-locale-link-fr').click()
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')

// go to `en` home page
await page.goto(blog)
expect(page.url().endsWith('/fr/blog/article'))
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { test, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, url, createPage } from '@nuxt/test-utils'
import { getText } from '../../helper'

await setup({
rootDir: fileURLToPath(new URL(`../../fixtures/fallback`, import.meta.url)),
browser: true,
// overrides
nuxtConfig: {
i18n: {
strategy: 'prefix_and_default',
detectBrowserLanguage: {
alwaysRedirect: true,
redirectOn: 'no prefix'
}
}
}
})

test('alwaysRedirect: no prefix', async () => {
const blog = url('/about')
const page = await createPage(undefined, { locale: 'en' }) // set browser locale
await page.goto(blog)

// detect locale from navigator language
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en')

// click `fr` lang switch link
await page.locator('#set-locale-link-fr').click()
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')

// go to `en` home page
await page.goto(url('/ja/about'))
expect(page.url().endsWith('/ja/about'))
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('ja')

await page.goto(url('/about'))
expect(page.url().endsWith('/ja/about'))
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('ja')
})
7 changes: 5 additions & 2 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,11 @@ export function detectBrowserLanguage<Context extends NuxtApp = NuxtApp>(
if (finalLocale !== vueI18nLocale /* && path !== '/'*/) {
__DEBUG__ && console.log('detectBrowserLanguage: finalLocale !== vueI18nLocale', finalLocale)
return { locale: finalLocale, stat: true, from: localeFrom }
} else {
if (alwaysRedirect && path === '/') {
} else if (alwaysRedirect) {
const redirectOnRoot = path === '/'
const redirectOnAll = redirectOn === 'all'
const redirectOnNoPrefix = redirectOn === 'no prefix' && !path.match(getLocalesRegex(localeCodes as string[]))
if (redirectOnRoot || redirectOnAll || redirectOnNoPrefix) {
return { locale: finalLocale, stat: true, from: localeFrom }
}
}
Expand Down

0 comments on commit afd9ab2

Please sign in to comment.