-
-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support detect browser language fully (#1534)
* feat: support detect browser language fully * fix * refactor * fix lint warnings * update snapshot * remove comment * fix: redirection on `setLocale` * fix: browser language detection fully supporting * update deps
- Loading branch information
Showing
35 changed files
with
637 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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: 'no_prefix', | ||
detectBrowserLanguage: { | ||
useCookie: false | ||
} | ||
} | ||
} | ||
}) | ||
|
||
test('detection with browser', async () => { | ||
const home = url('/') | ||
const page = await createPage(undefined, { locale: 'fr' }) // set browser locale | ||
await page.goto(home) | ||
|
||
// detect locale from navigator language | ||
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 blog/article | ||
await page.goto(url('/blog/article')) | ||
|
||
// locale in blog/article | ||
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr') | ||
|
||
// navigate with home | ||
await page.goto(url('/')) | ||
|
||
// locale in home | ||
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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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: 'no_prefix', | ||
detectBrowserLanguage: { | ||
useCookie: true, | ||
cookieKey: 'my_custom_cookie_name', | ||
redirectOn: 'root', | ||
cookieCrossOrigin: true, | ||
cookieSecure: true | ||
} | ||
} | ||
} | ||
}) | ||
|
||
test('detection with cookie', async () => { | ||
const home = url('/') | ||
const page = await createPage(undefined, { locale: 'en' }) | ||
await page.goto(home) | ||
const ctx = await page.context() | ||
|
||
// click `fr` lang switch link | ||
await page.locator('#set-locale-link-fr').click() | ||
expect(await ctx.cookies()).toMatchObject([ | ||
{ name: 'my_custom_cookie_name', value: 'fr', secure: true, sameSite: 'None' } | ||
]) | ||
|
||
// navigate to about | ||
await page.goto(url('/about')) | ||
|
||
// detect locale from persisted cookie | ||
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr') | ||
|
||
// navigate with home link | ||
await page.locator('#link-home').click() | ||
|
||
// locale in home | ||
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr') | ||
|
||
// click `fr` lang switch link | ||
await page.locator('#set-locale-link-en').click() | ||
expect(await ctx.cookies()).toMatchObject([{ name: 'my_custom_cookie_name', value: 'en' }]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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: 'no_prefix', | ||
detectBrowserLanguage: false | ||
} | ||
} | ||
}) | ||
|
||
test('disable', async () => { | ||
const home = url('/') | ||
const page = await createPage(undefined, { locale: 'en' }) | ||
await page.goto(home) | ||
const ctx = await page.context() | ||
|
||
// click `fr` lang switch link | ||
await page.locator('#set-locale-link-fr').click() | ||
expect(await ctx.cookies()).toMatchObject([]) | ||
|
||
// navigate to about | ||
await page.goto(url('/about')) | ||
|
||
// set default locale | ||
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en') | ||
|
||
// click `fr` lang switch link | ||
await page.locator('#set-locale-link-fr').click() | ||
|
||
// navigate with home link | ||
await page.locator('#link-home').click() | ||
|
||
// set default locale | ||
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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: 'no_prefix', | ||
defaultLocale: 'en', | ||
detectBrowserLanguage: { | ||
useCookie: false, | ||
fallbackLocale: 'fr' | ||
} | ||
} | ||
} | ||
}) | ||
|
||
test('fallback', async () => { | ||
const home = url('/') | ||
const page = await createPage(undefined, { locale: 'ja' }) // set browser locale | ||
await page.goto(home) | ||
|
||
// detect fallback locale with navigator language | ||
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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_except_default', | ||
detectBrowserLanguage: { | ||
redirectOn: 'all' | ||
} | ||
} | ||
} | ||
}) | ||
|
||
test('redirectOn: all', async () => { | ||
const blog = url('/blog/article') | ||
const page = await createPage(undefined, { locale: 'fr' }) // 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') | ||
|
||
// navigate to home | ||
await page.goto(url('/')) | ||
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en') | ||
}) |
36 changes: 36 additions & 0 deletions
36
specs/browser_language_detection/redirect_no_prefix.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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: false, | ||
redirectOn: 'no prefix' | ||
} | ||
} | ||
} | ||
}) | ||
|
||
test('redirectOn: no prefix', async () => { | ||
const blog = url('/blog/article') | ||
const page = await createPage(undefined, { locale: 'fr' }) // 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') | ||
|
||
// navigate to home | ||
await page.goto(url('/')) | ||
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en') | ||
}) |
Oops, something went wrong.