NuxtLink
:
+ setLocale()
:
+ {{ locale.name }}
+ {{ locale }}
:
+ {{ code }}
+ {{ t('pages.blog.article') }}
+{{ code }}
+ This is blog index page
+This is cateory page on '{{ $route.params.slug }}'
+ diff --git a/specs/fixtures/multi_domains_locales/pages/ignore-routes/disable.vue b/specs/fixtures/multi_domains_locales/pages/ignore-routes/disable.vue new file mode 100644 index 000000000..5caff5764 --- /dev/null +++ b/specs/fixtures/multi_domains_locales/pages/ignore-routes/disable.vue @@ -0,0 +1,7 @@ + + + +ignore localized route disable test
+ diff --git a/specs/fixtures/multi_domains_locales/pages/ignore-routes/pick.vue b/specs/fixtures/multi_domains_locales/pages/ignore-routes/pick.vue new file mode 100644 index 000000000..4ca5d868b --- /dev/null +++ b/specs/fixtures/multi_domains_locales/pages/ignore-routes/pick.vue @@ -0,0 +1,9 @@ + + + +ignore localized route pick test
+ diff --git a/specs/fixtures/multi_domains_locales/pages/index.vue b/specs/fixtures/multi_domains_locales/pages/index.vue new file mode 100644 index 000000000..b2c625ac2 --- /dev/null +++ b/specs/fixtures/multi_domains_locales/pages/index.vue @@ -0,0 +1,66 @@ + + + +useAsyncData
:
+ {{ data }}
+ useHead
with useLocaleHead
:
+ {{ i18nHead }}
+ {{ t('my-module-exemple.hello') }}
+ This is profile page
+ diff --git a/specs/multi_domains_locales/multi_domains_locales_multi_locales.spec.ts b/specs/multi_domains_locales/multi_domains_locales_multi_locales.spec.ts new file mode 100644 index 000000000..3b48f0ba4 --- /dev/null +++ b/specs/multi_domains_locales/multi_domains_locales_multi_locales.spec.ts @@ -0,0 +1,83 @@ +import { test, expect, describe } from 'vitest' +import { fileURLToPath } from 'node:url' +import { setup, $fetch, undiciRequest } from '../utils' +import { getDom } from '../helper' + +const i18nDomains = ['nuxt-app.localhost', 'fr.nuxt-app.localhost', 'ja.nuxt-app.localhost'] + +await setup({ + rootDir: fileURLToPath(new URL(`../fixtures/multi_domains_locales`, import.meta.url)), + // overrides + nuxtConfig: { + i18n: { + baseUrl: 'http://localhost:3000', + locales: [ + { + code: 'en', + iso: 'en', + name: 'English', + domains: i18nDomains, + defaultForDomains: ['nuxt-app.localhost'] + }, + { + code: 'no', + iso: 'no-NO', + name: 'Norwegian', + domains: i18nDomains + }, + { + code: 'fr', + iso: 'fr-FR', + name: 'Français', + domains: i18nDomains, + defaultForDomains: ['fr.nuxt-app.localhost'] + }, + { + code: 'ja', + iso: 'jp-JA', + name: 'Japan', + domains: i18nDomains, + defaultForDomains: ['ja.nuxt-app.localhost'] + } + ], + multiDomainLocales: 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'], + ['/', 'fr', 'fr.nuxt-app.localhost', 'Accueil'], + ['/en', 'en', 'fr.nuxt-app.localhost', 'Homepage'], + ['/no', 'no', 'fr.nuxt-app.localhost', 'Hjemmeside'] + ])('%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') +}) diff --git a/src/constants.ts b/src/constants.ts index fc41cd3db..72f1d4b20 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -79,7 +79,8 @@ export const DEFAULT_OPTIONS = { skipSettingLocaleOnNavigate: false, types: 'composition', debug: false, - parallelPlugin: false + parallelPlugin: false, + multiDomainLocales: false } as const export const NUXT_I18N_TEMPLATE_OPTIONS_KEY = 'i18n.options.mjs' diff --git a/src/module.ts b/src/module.ts index 5538c0e77..38fb2d69a 100644 --- a/src/module.ts +++ b/src/module.ts @@ -155,7 +155,8 @@ export default defineNuxtModule