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.
feat: Support multiple languages on one or more domains while having …
…different domains (nuxt-modules#2705) * support multiple languages on one or more domains * add debug log * fix after rebase * update docs
- Loading branch information
1 parent
eafe06b
commit b7a6c66
Showing
11 changed files
with
340 additions
and
15 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
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
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
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,86 @@ | ||
import { test, expect, describe } from 'vitest' | ||
import { fileURLToPath } from 'node:url' | ||
import { setup, $fetch, undiciRequest } from './utils' | ||
import { getDom } from './helper' | ||
|
||
await setup({ | ||
rootDir: fileURLToPath(new URL(`./fixtures/different_domains`, import.meta.url)), | ||
// overrides | ||
nuxtConfig: { | ||
i18n: { | ||
locales: [ | ||
{ | ||
code: 'en', | ||
iso: 'en', | ||
name: 'English', | ||
domain: 'nuxt-app.localhost', | ||
domainDefault: true | ||
}, | ||
{ | ||
code: 'no', | ||
iso: 'no-NO', | ||
name: 'Norwegian', | ||
domain: 'nuxt-app.localhost' | ||
}, | ||
{ | ||
code: 'fr', | ||
iso: 'fr-FR', | ||
name: 'Français', | ||
domain: 'fr.nuxt-app.localhost' | ||
} | ||
], | ||
differentDomains: true, | ||
strategy: 'prefix', | ||
detectBrowserLanguage: { | ||
useCookie: true | ||
} | ||
} | ||
} | ||
}) | ||
|
||
describe('detection locale with host on server', () => { | ||
test.each([ | ||
['en', 'nuxt-app.localhost', 'Homepage'], | ||
['fr', 'fr.nuxt-app.localhost', 'Accueil'] | ||
])('%s host', async (locale, host, header) => { | ||
const res = await undiciRequest('/' + locale, { | ||
headers: { | ||
Host: host | ||
} | ||
}) | ||
const dom = getDom(await res.body.text()) | ||
|
||
expect(dom.querySelector('#lang-switcher-current-locale code').textContent).toEqual(locale) | ||
expect(dom.querySelector('#home-header').textContent).toEqual(header) | ||
}) | ||
}) | ||
|
||
test('detection locale with x-forwarded-host on server', async () => { | ||
const html = await $fetch('/fr', { | ||
headers: { | ||
'X-Forwarded-Host': 'fr.nuxt-app.localhost' | ||
} | ||
}) | ||
const dom = getDom(html) | ||
|
||
expect(dom.querySelector('#lang-switcher-current-locale code').textContent).toEqual('fr') | ||
expect(dom.querySelector('#home-header').textContent).toEqual('Accueil') | ||
}) | ||
|
||
test('pass `<NuxtLink> to props', async () => { | ||
const res = await undiciRequest('/fr', { | ||
headers: { | ||
Host: 'fr.nuxt-app.localhost' | ||
} | ||
}) | ||
const dom = getDom(await res.body.text()) | ||
expect(dom.querySelector('#switch-locale-path-usages .switch-to-en a').getAttribute('href')).toEqual( | ||
`http://nuxt-app.localhost/en` | ||
) | ||
expect(dom.querySelector('#switch-locale-path-usages .switch-to-no a').getAttribute('href')).toEqual( | ||
`http://nuxt-app.localhost/no` | ||
) | ||
expect(dom.querySelector('#switch-locale-path-usages .switch-to-fr a').getAttribute('href')).toEqual( | ||
`http://fr.nuxt-app.localhost/fr` | ||
) | ||
}) |
95 changes: 95 additions & 0 deletions
95
specs/different_domains_multi_locales_prefix_except_default.spec.ts
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,95 @@ | ||
import { test, expect, describe } from 'vitest' | ||
import { fileURLToPath } from 'node:url' | ||
import { setup, $fetch, undiciRequest } from './utils' | ||
import { getDom } from './helper' | ||
|
||
await setup({ | ||
rootDir: fileURLToPath(new URL(`./fixtures/different_domains`, import.meta.url)), | ||
// overrides | ||
nuxtConfig: { | ||
i18n: { | ||
locales: [ | ||
{ | ||
code: 'en', | ||
iso: 'en', | ||
name: 'English', | ||
domain: 'nuxt-app.localhost', | ||
domainDefault: true | ||
}, | ||
{ | ||
code: 'no', | ||
iso: 'no-NO', | ||
name: 'Norwegian', | ||
domain: 'nuxt-app.localhost' | ||
}, | ||
{ | ||
code: 'fr', | ||
iso: 'fr-FR', | ||
name: 'Français', | ||
domain: 'fr.nuxt-app.localhost', | ||
domainDefault: true | ||
}, | ||
{ | ||
code: 'ja', | ||
iso: 'jp-JA', | ||
name: 'Japan', | ||
domain: 'ja.nuxt-app.localhost', | ||
domainDefault: true | ||
} | ||
], | ||
differentDomains: true, | ||
strategy: 'prefix_except_default', | ||
detectBrowserLanguage: { | ||
useCookie: true | ||
} | ||
} | ||
} | ||
}) | ||
|
||
describe('detection locale with host on server', () => { | ||
test.each([ | ||
['/', 'en', 'nuxt-app.localhost', 'Homepage'], | ||
['/no', 'no', 'nuxt-app.localhost', 'Hjemmeside'], | ||
['/', 'fr', 'fr.nuxt-app.localhost', 'Accueil'] | ||
])('%s host', async (path, locale, host, header) => { | ||
const res = await undiciRequest(path, { | ||
headers: { | ||
Host: host | ||
} | ||
}) | ||
const dom = getDom(await res.body.text()) | ||
|
||
expect(dom.querySelector('#lang-switcher-current-locale code').textContent).toEqual(locale) | ||
expect(dom.querySelector('#home-header').textContent).toEqual(header) | ||
}) | ||
}) | ||
|
||
test('detection locale with x-forwarded-host on server', async () => { | ||
const html = await $fetch('/', { | ||
headers: { | ||
'X-Forwarded-Host': 'fr.nuxt-app.localhost' | ||
} | ||
}) | ||
const dom = getDom(html) | ||
|
||
expect(dom.querySelector('#lang-switcher-current-locale code').textContent).toEqual('fr') | ||
expect(dom.querySelector('#home-header').textContent).toEqual('Accueil') | ||
}) | ||
|
||
test('pass `<NuxtLink> to props', async () => { | ||
const res = await undiciRequest('/', { | ||
headers: { | ||
Host: 'fr.nuxt-app.localhost' | ||
} | ||
}) | ||
const dom = getDom(await res.body.text()) | ||
expect(dom.querySelector('#switch-locale-path-usages .switch-to-en a').getAttribute('href')).toEqual( | ||
`http://nuxt-app.localhost` | ||
) | ||
expect(dom.querySelector('#switch-locale-path-usages .switch-to-no a').getAttribute('href')).toEqual( | ||
`http://nuxt-app.localhost/no` | ||
) | ||
expect(dom.querySelector('#switch-locale-path-usages .switch-to-fr a').getAttribute('href')).toEqual( | ||
`http://fr.nuxt-app.localhost` | ||
) | ||
}) |
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
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
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
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
Oops, something went wrong.