Skip to content

Commit 7f7eaf2

Browse files
DotwoodMediaVincentdevreede
andauthoredDec 3, 2024··
fix: default language multi domain locales (#3250)
Co-authored-by: Vincent de Vreede <vincentdevreede@live.nl> Co-authored-by: Dotwood Media <info@dotwood.media>
1 parent ea7aa14 commit 7f7eaf2

File tree

7 files changed

+62
-11
lines changed

7 files changed

+62
-11
lines changed
 

‎docs/content/docs/2.guide/10.multi-domain-locales.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ With the above config, a build would have to be run for staging and production w
9898

9999
## Using different domains for only some of the languages
100100

101-
If one or more of the domains need to host multiple languages, the default language of each domain needs to have `domainDefault: true`{lang="ts"} so there is a per domain fallback locale.
102-
The option `differentDomains` still need to be set to `true`{lang="ts"} though.
101+
If multiple domains share the same default language, you can specify them all using `defaultForDomains`, which supports multiple domains.
103102

104103
```js {}[nuxt.config.js]
105104
const i18nDomains = ['mydomain.com', 'en.mydomain.com', 'es.mydomain.com', 'fr.mydomain.com', 'http://pl.mydomain.com', 'https://ua.mydomain.com']

‎specs/fixtures/multi_domains_locales/i18n.config.ts

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export default {
1313
blog: {
1414
article: "Cette page d'article de blog"
1515
}
16+
},
17+
parent: {
18+
text: 'Test de la voie parentale',
19+
child: {
20+
text: 'Test de parcours pour enfants'
21+
}
1622
}
1723
},
1824
en: {
@@ -26,6 +32,12 @@ export default {
2632
blog: {
2733
article: 'This is blog article page'
2834
}
35+
},
36+
parent: {
37+
text: 'Parent route test',
38+
child: {
39+
text: 'Child route test'
40+
}
2941
}
3042
},
3143
no: {
@@ -39,6 +51,12 @@ export default {
3951
blog: {
4052
article: 'Dette er bloggartikkelsiden'
4153
}
54+
},
55+
parent: {
56+
text: 'Forældrerutetest',
57+
child: {
58+
text: 'Børns rute test'
59+
}
4260
}
4361
}
4462
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script setup lang="ts">
2+
const { t } = useI18n()
3+
</script>
4+
5+
<template>
6+
<p id="parent-text">{{ t('parent.text') }}</p>
7+
<NuxtPage />
8+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
const { t } = useI18n()
3+
</script>
4+
5+
<template>
6+
<p id="child-text">{{ t('parent.child.text') }}</p>
7+
</template>

‎specs/multi_domains_locales/multi_domains_locales_multi_locales.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,21 @@ test('detection locale with x-forwarded-host on server', async () => {
8181
expect(dom.querySelector('#lang-switcher-current-locale code').textContent).toEqual('fr')
8282
expect(dom.querySelector('#home-header').textContent).toEqual('Accueil')
8383
})
84+
85+
describe('detection locale with child routes', () => {
86+
test.each([
87+
['/parent/child', 'nuxt-app.localhost', 'Parent route test', 'Child route test'],
88+
['/no/parent/child', 'nuxt-app.localhost', 'Forældrerutetest', 'Børns rute test'],
89+
['/fr/parent/child', 'nuxt-app.localhost', 'Test de la voie parentale', 'Test de parcours pour enfants']
90+
])('%s host', async (path, host, parentText, childText) => {
91+
const res = await undiciRequest(path, {
92+
headers: {
93+
Host: host
94+
}
95+
})
96+
const dom = getDom(await res.body.text())
97+
98+
expect(dom.querySelector('#parent-text').textContent).toEqual(parentText)
99+
expect(dom.querySelector('#child-text').textContent).toEqual(childText)
100+
})
101+
})

‎src/routing.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export function localizeRoutes(routes: NuxtPage[], options: LocalizeRoutesParams
175175
) {
176176
localizedRoutes.push({
177177
...localized,
178-
name: `${localized.name}___default`
178+
name: `${localized.name}___${options.defaultLocaleRouteNameSuffix}`
179179
})
180180
}
181181

‎src/runtime/internal.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -397,21 +397,22 @@ export function setupMultiDomainLocales(nuxtContext: NuxtApp, defaultLocaleDomai
397397
const router = useRouter()
398398
const defaultRouteSuffix = [routesNameSeparator, defaultLocaleRouteNameSuffix].join('')
399399

400-
// remove or rename default routes if not applicable for domain
400+
// Adjust routes to match the domain's locale and structure
401401
for (const route of router.getRoutes()) {
402402
const routeName = getRouteName(route.name)
403403

404-
if (!routeName.includes(defaultRouteSuffix)) continue
404+
if (routeName.endsWith(defaultRouteSuffix)) {
405+
router.removeRoute(routeName)
406+
continue
407+
}
405408

406409
const routeNameLocale = routeName.split(routesNameSeparator)[1]
407410
if (routeNameLocale === defaultLocaleDomain) {
408-
route.name = routeName.replace(defaultRouteSuffix, '')
409-
continue
411+
router.addRoute({
412+
...route,
413+
path: route.path === `/${routeNameLocale}` ? '/' : route.path.replace(`/${routeNameLocale}`, '')
414+
})
410415
}
411-
412-
// use `route.name` directly as `routeName` stringifies `Symbol`
413-
// @ts-expect-error type mismatch
414-
router.removeRoute(route.name)
415416
}
416417
}
417418

0 commit comments

Comments
 (0)
Please sign in to comment.