Skip to content

Commit 89c5fba

Browse files
fix: getDefaultLocaleForDomain should respect runtime config (#3693)
Co-authored-by: Bobbie Goede <bobbiegoede@gmail.com> fix: getDefaultLocaleForDomain should respect runtime-configured domains (#2931)
1 parent e3fbaa3 commit 89c5fba

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

specs/different_domains/different_domains.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ test.each([
151151
expect(dom.querySelector('#welcome-text').textContent).toEqual(header)
152152
})
153153

154+
test('(#2931) detect using runtimeConfig domain', async () => {
155+
const res = await undiciRequest('/', {
156+
headers: {
157+
host: 'kr.staging.nuxt-app.localhost'
158+
}
159+
})
160+
const dom = getDom(await res.body.text())
161+
expect(dom.querySelector('#welcome-text').textContent).toEqual('환영하다')
162+
})
163+
154164
test('(#2374) detect with x-forwarded-host on server', async () => {
155165
const html = await $fetch('/', {
156166
headers: {

specs/fixtures/different_domains/i18n.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ export default {
4040
article: 'Dette er bloggartikkelsiden'
4141
}
4242
}
43+
},
44+
kr: {
45+
welcome: '환영하다',
46+
home: '홈페이지',
47+
profile: '프로필',
48+
about: '회사 소개',
49+
posts: '게시물',
50+
dynamic: '동적',
51+
pages: {
52+
blog: {
53+
article: '여기는 블로그 게시물 페이지입니다'
54+
}
55+
}
4356
}
4457
},
4558
fallbackLocale: 'en'

src/runtime/domain.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,21 @@ export function setupMultiDomainLocales(runtimeI18n: I18nPublicRuntimeConfig, de
138138
* Returns default locale for the current domain, returns `defaultLocale` by default
139139
*/
140140
export function getDefaultLocaleForDomain(runtimeI18n: I18nPublicRuntimeConfig) {
141-
const { locales, defaultLocale, multiDomainLocales } = runtimeI18n
141+
const { locales, domainLocales, defaultLocale, multiDomainLocales } = runtimeI18n
142+
const host = getHost()
143+
142144
if (!multiDomainLocales) {
143-
return defaultLocale || ''
145+
const foundLocale = normalizedLocales.find(l => {
146+
const localeCode = isString(l) ? l : l.code
147+
const lang = normalizedLocales.find(locale => locale.code === localeCode)
148+
const domain = domainLocales?.[localeCode]?.domain ?? lang?.domain
149+
150+
return domain === host
151+
})
152+
153+
return foundLocale?.code ?? defaultLocale ?? ''
144154
}
145155

146-
const host = getHost()
147156
if (locales.some(l => !isString(l) && l.defaultForDomains != null)) {
148157
const findDefaultLocale = locales.find(
149158
(l): l is LocaleObject => !isString(l) && !!l.defaultForDomains?.includes(host)

0 commit comments

Comments
 (0)