From a503e8b068ab7e8defe5ede28ee48d352dad67fb Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Thu, 20 Jul 2023 19:22:54 +0900 Subject: [PATCH] fix: `detectBrowserLanguage.redirectOn` option (#2252) --- .../browser_language_detection/redirect_all.spec.ts | 8 ++++---- .../redirect_no_prefix.spec.ts | 13 +++++++------ .../redirect_no_prefix/nuxt.config.ts | 5 +++++ specs/fixtures/issues/2132/nuxt.config.ts | 2 +- src/runtime/internal.ts | 10 ++++++++-- src/runtime/utils.ts | 4 ++++ 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/specs/browser_language_detection/redirect_all.spec.ts b/specs/browser_language_detection/redirect_all.spec.ts index 9f7463161..c0f277eb6 100644 --- a/specs/browser_language_detection/redirect_all.spec.ts +++ b/specs/browser_language_detection/redirect_all.spec.ts @@ -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') diff --git a/specs/browser_language_detection/redirect_no_prefix.spec.ts b/specs/browser_language_detection/redirect_no_prefix.spec.ts index 395febce9..0a74772a6 100644 --- a/specs/browser_language_detection/redirect_no_prefix.spec.ts +++ b/specs/browser_language_detection/redirect_no_prefix.spec.ts @@ -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') }) diff --git a/specs/fixtures/browser_language_detection/redirect_no_prefix/nuxt.config.ts b/specs/fixtures/browser_language_detection/redirect_no_prefix/nuxt.config.ts index 62e87307f..361ff6806 100644 --- a/specs/fixtures/browser_language_detection/redirect_no_prefix/nuxt.config.ts +++ b/specs/fixtures/browser_language_detection/redirect_no_prefix/nuxt.config.ts @@ -17,6 +17,11 @@ export default defineNuxtConfig({ code: 'fr', iso: 'fr-FR', name: 'Français' + }, + { + code: 'pl', + iso: 'pl-PL', + name: 'Polish' } ], defaultLocale: 'en', diff --git a/specs/fixtures/issues/2132/nuxt.config.ts b/specs/fixtures/issues/2132/nuxt.config.ts index 75ef61530..eb86eb00b 100644 --- a/specs/fixtures/issues/2132/nuxt.config.ts +++ b/specs/fixtures/issues/2132/nuxt.config.ts @@ -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' } diff --git a/src/runtime/internal.ts b/src/runtime/internal.ts index f04f3736d..a7b41236e 100644 --- a/src/runtime/internal.ts +++ b/src/runtime/internal.ts @@ -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 localeCode = parsedCookie[cookieKey] - __DEBUG__ && console.log('getLocaleCookie cookie', parsedCookie[cookieKey]) + __DEBUG__ && console.log('getLocaleCookie cookie (server) -', localeCode) } } @@ -426,7 +427,7 @@ export function detectBrowserLanguage( } __DEBUG__ && console.log( - 'detectBrowserLanguage: first finaleLocale (finaleLocale, lcoaleForm) -', + 'detectBrowserLanguage: first finaleLocale (finaleLocale, cookieLocale, localeFrom) -', finalLocale, cookieLocale, localeFrom @@ -446,6 +447,7 @@ export function detectBrowserLanguage( return { locale: finalLocale, stat: true, from: localeFrom } } } + if (alwaysRedirect) { const redirectOnRoot = path === '/' const redirectOnAll = redirectOn === 'all' @@ -468,6 +470,10 @@ export function detectBrowserLanguage( 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' } } diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 4ccca83b4..973eaa9cd 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -247,6 +247,10 @@ export function detectLocale( 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) {