Skip to content

Commit

Permalink
Fixed Incorrect locale returned when using different domains (#2383)
Browse files Browse the repository at this point in the history
* Fixed a bug where the correct locale.code could not be obtained when differentDomains: true and locale.domain is set with a protocol header.

* add spec issues 2374

* fix getLocaleDomain

* Fixed a bug where the correct locale.code could not be obtained when differentDomains: true and locale.domain is set with a protocol header.

* fix: cannot resolve message for jit compilation (#2387)

* fix getLocaleDomain

---------

Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com>
  • Loading branch information
gaoxu529 and kazupon authored Sep 8, 2023
1 parent 9a24c86 commit c8d5efe
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 1 deletion.
5 changes: 5 additions & 0 deletions specs/fixtures/issues/2374/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<NuxtPage />
</div>
</template>
3 changes: 3 additions & 0 deletions specs/fixtures/issues/2374/lang/en/pc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
welcome: 'test issue 2374'
}
3 changes: 3 additions & 0 deletions specs/fixtures/issues/2374/lang/zh/pc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
welcome: '测试问题2374'
}
26 changes: 26 additions & 0 deletions specs/fixtures/issues/2374/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
i18n: {
locales: [
{
code: 'en',
name: 'English',
iso: 'en',
file: 'en/pc.js',
domain: 'en.nuxt-app.localhost'
},
{
code: 'zh-CN',
name: '简体中文',
iso: 'zh',
file: 'zh/pc.js',
domain: 'zh.nuxt-app.localhost'
}
],
differentDomains: true,
detectBrowserLanguage: false,
defaultLocale: 'en',
langDir: 'lang/'
}
})
14 changes: 14 additions & 0 deletions specs/fixtures/issues/2374/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "nuxt3-test-issues-2374",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@nuxtjs/i18n": "latest",
"nuxt": "latest"
}
}
11 changes: 11 additions & 0 deletions specs/fixtures/issues/2374/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts" setup>
const { t } = useI18n()
</script>

<template>
<div>
<div id="content">{{ t('welcome') }}</div>
</div>
</template>

<style scoped></style>
36 changes: 36 additions & 0 deletions specs/issues/2374.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { test, expect, describe } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, $fetch } from '../utils'
import { getDom } from '../helper'

describe('#2374', async () => {
await setup({
rootDir: fileURLToPath(new URL(`../fixtures/issues/2374`, import.meta.url))
})

describe('detection issues 2374 with host on server', () => {
test.each([
['en.nuxt-app.localhost', 'test issue 2374'],
['zh.nuxt-app.localhost', '测试问题2374']
])('%s host', async (host, header) => {
const html = await $fetch('/', {
headers: {
Host: host
}
})
const dom = getDom(html)
expect(dom.querySelector('#content').textContent).toEqual(header)
})
})

test('detection issues 2374 with x-forwarded-host on server', async () => {
const html = await $fetch('/', {
headers: {
'X-Forwarded-Host': 'zh.nuxt-app.localhost'
}
})
const dom = getDom(html)

expect(dom.querySelector('#content').textContent).toEqual('测试问题2374')
})
})
11 changes: 10 additions & 1 deletion src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,16 @@ export function getHost() {
export function getLocaleDomain(locales: LocaleObject[]): string {
let host = getHost() || ''
if (host) {
const matchingLocale = locales.find(locale => locale.domain === host)
const matchingLocale = locales.find(locale => {
if (locale && locale.domain) {
let domain = locale.domain
if (hasProtocol(locale.domain)) {
domain = locale.domain.replace(/(http|https):\/\//, '')
}
return domain === host
}
return false
})
if (matchingLocale) {
return matchingLocale.code
} else {
Expand Down

0 comments on commit c8d5efe

Please sign in to comment.