forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: does not redirect when the site is reload again (nuxt-modules#2263)
- Loading branch information
Showing
8 changed files
with
114 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<template> | ||
<div> | ||
<NuxtLayout> | ||
<NuxtPage></NuxtPage> | ||
</NuxtLayout> | ||
</div> | ||
</template> |
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,12 @@ | ||
export default defineI18nConfig(() => ({ | ||
legacy: false, | ||
locale: 'en', | ||
messages: { | ||
en: { | ||
welcome: 'Welcome' | ||
}, | ||
fr: { | ||
welcome: 'Bienvenue' | ||
} | ||
} | ||
})) |
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,5 @@ | ||
<template> | ||
<div> | ||
<slot /> | ||
</div> | ||
</template> |
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,25 @@ | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
export default defineNuxtConfig({ | ||
devtools: { enabled: true }, | ||
modules: ['@nuxtjs/i18n'], | ||
i18n: { | ||
locales: [ | ||
{ | ||
code: 'en', | ||
name: 'English' | ||
}, | ||
{ | ||
code: 'fr', | ||
name: 'Français' | ||
} | ||
], | ||
strategy: 'prefix_except_default', | ||
defaultLocale: 'en', | ||
// debug: true, | ||
detectBrowserLanguage: { | ||
useCookie: true, | ||
cookieKey: 'i18n_redirected', | ||
redirectOn: 'root' // recommended | ||
} | ||
} | ||
}) |
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,14 @@ | ||
{ | ||
"name": "nuxt3-test-issues-2255", | ||
"private": true, | ||
"scripts": { | ||
"build": "nuxt build", | ||
"dev": "nuxt dev", | ||
"generate": "nuxt generate", | ||
"preview": "nuxt preview" | ||
}, | ||
"devDependencies": { | ||
"@nuxtjs/i18n": "latest", | ||
"nuxt": "latest" | ||
} | ||
} |
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,21 @@ | ||
<script setup> | ||
const { locale, locales } = useI18n() | ||
const switchLocalePath = useSwitchLocalePath() | ||
const availableLocales = computed(() => { | ||
return locales.value.filter(i => i.code !== locale.value) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<NuxtLink | ||
:id="locale.code" | ||
v-for="locale in availableLocales" | ||
:key="locale.code" | ||
:to="switchLocalePath(locale.code)" | ||
>{{ locale.name }}</NuxtLink | ||
> | ||
</div> | ||
|
||
<p id="msg">{{ $t('welcome') }}</p> | ||
</template> |
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,27 @@ | ||
import { test, expect, describe } from 'vitest' | ||
import { fileURLToPath } from 'node:url' | ||
import { URL } from 'node:url' | ||
import { setup, url, createPage } from '../utils' | ||
import { getText } from '../helper' | ||
|
||
describe('#2255', async () => { | ||
await setup({ | ||
rootDir: fileURLToPath(new URL(`../fixtures/issues/2255`, import.meta.url)) | ||
}) | ||
|
||
test('redirect with browser language locale', async () => { | ||
const home = url('/') | ||
const page = await createPage(undefined, { locale: 'fr' }) | ||
await page.goto(home) | ||
|
||
expect(await getText(page, '#msg')).toEqual('Bienvenue') | ||
}) | ||
|
||
test('redirect with accept language header', async () => { | ||
const home = url('/') | ||
const page = await createPage(undefined, { extraHTTPHeaders: { 'Accept-Language': 'fr' } }) | ||
await page.goto(home) | ||
|
||
expect(await getText(page, '#msg')).toEqual('Bienvenue') | ||
}) | ||
}) |
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