Skip to content

Commit

Permalink
fix: not work detectBrowserLanguage on client-side correctly (#2475)
Browse files Browse the repository at this point in the history
* fix: not work detectBrowserLanguage on client-side correctly

* fix
  • Loading branch information
kazupon authored Oct 4, 2023
1 parent 5c40ec1 commit f351510
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 1 deletion.
3 changes: 3 additions & 0 deletions specs/fixtures/issues/2473/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<NuxtPage />
</template>
15 changes: 15 additions & 0 deletions specs/fixtures/issues/2473/locales/en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Meta": {
"locale": "English"
},
"Pages": {
"Home": {
"title": "Home page",
"description": "Home page description"
},
"About": {
"title": "About page",
"description": "About page description"
}
}
}
15 changes: 15 additions & 0 deletions specs/fixtures/issues/2473/locales/fr-FR.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Meta": {
"locale": "French"
},
"Pages": {
"Home": {
"title": "Page d'accueil",
"description": "Page d'accueil description"
},
"About": {
"title": "À propos",
"description": "A propos description"
}
}
}
22 changes: 22 additions & 0 deletions specs/fixtures/issues/2473/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { LocaleObject } from '#i18n'

const locales = [
{ code: 'en', iso: 'en-US', file: 'en-US.json' },
{ code: 'fr', iso: 'fr-FR', file: 'fr-FR.json' }
] as LocaleObject[]

const defaultLocale = locales[0]

export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
i18n: {
locales,
defaultLocale: defaultLocale.code,
langDir: 'locales/',
lazy: true,
strategy: 'no_prefix',
detectBrowserLanguage: {
fallbackLocale: defaultLocale.code
}
}
})
14 changes: 14 additions & 0 deletions specs/fixtures/issues/2473/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "nuxt3-test-issues-2473",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@nuxtjs/i18n": "latest",
"nuxt": "latest"
}
}
6 changes: 6 additions & 0 deletions specs/fixtures/issues/2473/pages/about.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<h1 id="title" v-text="$t('Pages.About.title')" />
<p v-text="$t('Pages.About.description')" />
<p v-text="$t('Meta.locale')" />
<NuxtLinkLocale id="locale" to="/">{{ $t('Pages.Home.title') }}</NuxtLinkLocale>
</template>
6 changes: 6 additions & 0 deletions specs/fixtures/issues/2473/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<h1 id="title" v-text="$t('Pages.Home.title')" />
<p v-text="$t('Pages.Home.description')" />
<p v-text="$t('Meta.locale')" />
<NuxtLinkLocale id="locale" to="/about">{{ $t('Pages.About.title') }}</NuxtLinkLocale>
</template>
28 changes: 28 additions & 0 deletions specs/issues/2473.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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('#2473', async () => {
await setup({
rootDir: fileURLToPath(new URL(`../fixtures/issues/2473`, import.meta.url))
})

test('should be respected detect browser language', async () => {
const home = url('/')
const page = await createPage(undefined, { locale: 'fr' })
await page.goto(home)

expect(await getText(page, '#title')).toEqual(`Page d'accueil`)

// change page
await page.locator('#locale').click()
await page.waitForURL('**/about')
expect(await getText(page, '#title')).toEqual(`À propos`)

// one more change page
await page.locator('#locale').click()
expect(await getText(page, '#title')).toEqual(`Page d'accueil`)
})
})
2 changes: 1 addition & 1 deletion src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export function detectBrowserLanguage<Context extends NuxtApp = NuxtApp>(

// browser locale detection happens during first access only
if (!firstAccess) {
return { locale: '', stat: false, reason: 'first_access_only' }
return { locale: strategy === 'no_prefix' ? locale : '', stat: false, reason: 'first_access_only' }
}

const { redirectOn, alwaysRedirect, useCookie, fallbackLocale } =
Expand Down

0 comments on commit f351510

Please sign in to comment.