Skip to content

Commit

Permalink
fix: detectBrowserLanguage.redirectOn option (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Jul 20, 2023
1 parent f5e7ab5 commit a503e8b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
8 changes: 4 additions & 4 deletions specs/browser_language_detection/redirect_all.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ test('redirectOn: all', async () => {
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')

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

// navigate to home
await page.goto(url('/'))
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en')
Expand Down
13 changes: 7 additions & 6 deletions specs/browser_language_detection/redirect_no_prefix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ test('redirectOn: no prefix', async () => {
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')

// navigate to home
await page.goto(url('/'))
// click `en` lang switch link
await page.locator('#set-locale-link-en').click()
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en')

// navigate to pl blog
const plBlog = url('/pl/blog/article')
await page.goto(plBlog)
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('pl')
})
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export default defineNuxtConfig({
code: 'fr',
iso: 'fr-FR',
name: 'Français'
},
{
code: 'pl',
iso: 'pl-PL',
name: 'Polish'
}
],
defaultLocale: 'en',
Expand Down
2 changes: 1 addition & 1 deletion specs/fixtures/issues/2132/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export default defineNuxtConfig({
],
strategy: 'prefix',
defaultLocale: 'en',
// debug: true,
detectBrowserLanguage: {
useCookie: true,
cookieSecure: true,
alwaysRedirect: true,
fallbackLocale: 'en',
redirectOn: 'no prefix'
}
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,13 @@ export function getLocaleCookie(
let localeCode: string | undefined
if (process.client) {
localeCode = JsCookie.get(cookieKey)
__DEBUG__ && console.log('getLocaleCookie cookie (client) -', localeCode)
} else if (process.server) {
const cookie = useRequestHeaders(['cookie'])
if ('cookie' in cookie) {
const parsedCookie = parse((cookie as any)['cookie']) as Record<string, string>
localeCode = parsedCookie[cookieKey]
__DEBUG__ && console.log('getLocaleCookie cookie', parsedCookie[cookieKey])
__DEBUG__ && console.log('getLocaleCookie cookie (server) -', localeCode)
}
}

Expand Down Expand Up @@ -426,7 +427,7 @@ export function detectBrowserLanguage<Context extends NuxtApp = NuxtApp>(
}
__DEBUG__ &&
console.log(
'detectBrowserLanguage: first finaleLocale (finaleLocale, lcoaleForm) -',
'detectBrowserLanguage: first finaleLocale (finaleLocale, cookieLocale, localeFrom) -',
finalLocale,
cookieLocale,
localeFrom
Expand All @@ -446,6 +447,7 @@ export function detectBrowserLanguage<Context extends NuxtApp = NuxtApp>(
return { locale: finalLocale, stat: true, from: localeFrom }
}
}

if (alwaysRedirect) {
const redirectOnRoot = path === '/'
const redirectOnAll = redirectOn === 'all'
Expand All @@ -468,6 +470,10 @@ export function detectBrowserLanguage<Context extends NuxtApp = NuxtApp>(
return { locale: finalLocale, stat: true, from: localeFrom }
}

if (localeFrom === 'navigator_or_header' && finalLocale) {
return { locale: finalLocale, stat: true, from: localeFrom }
}

return { locale: '', stat: false, reason: 'not_found_match' }
}

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ export function detectLocale<Context extends NuxtApp = NuxtApp>(
return initialLocale
}

if (from === 'navigator_or_header' && browserLocale) {
return browserLocale
}

let finalLocale: string | undefined = browserLocale
__DEBUG__ && console.log('detectLocale: finaleLocale first (finaleLocale, strategy) -', finalLocale, strategy)
if (!finalLocale) {
Expand Down

0 comments on commit a503e8b

Please sign in to comment.