-
-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for multi domain locales (#3065)
Co-authored-by: Vincent de Vreede <vincentdevreede@live.nl> Co-authored-by: DarthGigi <47110839+DarthGigi@users.noreply.github.com>
- Loading branch information
1 parent
fe262c4
commit 8891600
Showing
51 changed files
with
1,809 additions
and
1,769 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
--- | ||
title: Multi domain locales | ||
description: Set up multiple domains for multiple locales. Use a different domain name for each language your app supports. | ||
--- | ||
|
||
How to set up multi domain locales: | ||
|
||
- Set the `multiDomainLocales` option to `true` | ||
- Configure the `locales` option as an array of objects: | ||
- Each object has a `domains` key whose value is a array of the domains you'd like to use for that locale. Optionally include a port (if non-standard) and/or a protocol. If the protocol is not provided then an attempt will be made to auto-detect it but that might not work correctly in some cases like when the pages are statically generated. | ||
- Optionally set for each object a `defaultForDomains` key whose value is a array of the default domains you'd like to use for that locale. Optionally include a port (if non-standard) and/or a protocol. If the protocol is not provided then an attempt will be made to auto-detect it but that might not work correctly in some cases like when the pages are statically generated. | ||
- Optionally set `detectBrowserLanguage` to `false`. When enabled (which it is by default), user can get redirected to a different domain on first visit. Set to `false` if you want to ensure that visiting given domain always shows page in the corresponding locale. | ||
|
||
```ts [nuxt.config.ts] | ||
const i18nDomains = ['mydomain.com', 'es.mydomain.com', 'fr.mydomain.com', 'http://pl.mydomain.com', 'https://ua.mydomain.com'] | ||
|
||
export default defineNuxtConfig({ | ||
i18n: { | ||
locales: [ | ||
{ | ||
code: 'en', | ||
domains: i18nDomains, | ||
defaultForDomains: ['mydomain.com'] | ||
}, | ||
{ | ||
code: 'es', | ||
domains: i18nDomains, | ||
defaultForDomains: ['es.mydomain.com'] | ||
}, | ||
{ | ||
code: 'fr', | ||
domains: i18nDomains, | ||
defaultForDomains: ['fr.mydomain.com'] | ||
}, | ||
{ | ||
code: 'pl', | ||
domains: i18nDomains, | ||
defaultForDomains: ['http://pl.mydomain.com'] | ||
}, | ||
{ | ||
code: 'ua', | ||
domains: i18nDomains, | ||
defaultForDomains: ['https://ua.mydomain.com'] | ||
}, | ||
{ | ||
code: 'nl', | ||
domains: i18nDomains | ||
}, | ||
{ | ||
code: 'de', | ||
domains: i18nDomains | ||
}, | ||
], | ||
multiDomainLocales: true | ||
} | ||
}) | ||
``` | ||
|
||
## Runtime environment variables | ||
|
||
Sometimes there's a need to change domains in different environments, e.g. staging and production. | ||
As `nuxt.config.ts` is used at build time it would be necessary to create different builds for different environments. | ||
|
||
```ts [locale-domains.config.ts] | ||
export const localeDomains = { | ||
uk: process.env.DOMAIN_UK, | ||
fr: process.env.DOMAIN_FR | ||
} | ||
``` | ||
|
||
```ts [nuxt.config.ts] | ||
import { localeDomains } from './locale-domains.config' | ||
|
||
const i18nDomains = [localeDomains.uk, localeDomains.fr] | ||
|
||
export default defineNuxtConfig({ | ||
modules: ['@nuxtjs/i18n'], | ||
|
||
i18n: { | ||
multiDomainLocales: true, | ||
locales: [ | ||
{ | ||
code: 'uk', | ||
domains: i18nDomains, | ||
defaultForDomains: [localeDomains.uk] | ||
}, | ||
{ | ||
code: 'fr', | ||
domains: i18nDomains, | ||
defaultForDomains: [localeDomains.fr] | ||
} | ||
] | ||
} | ||
}) | ||
``` | ||
|
||
With the above config, a build would have to be run for staging and production with different .env files that specify `DOMAIN_UK` and `DOMAIN_FR`. | ||
|
||
## Using different domains for only some of the languages | ||
|
||
If one or more of the domains need to host multiple languages, the default language of each domain needs to have `domainDefault: true` so there is a per domain fallback locale. | ||
The option `differentDomains` still need to be set to `true` though. | ||
|
||
```js {}[nuxt.config.js] | ||
const i18nDomains = ['mydomain.com', 'en.mydomain.com', 'es.mydomain.com', 'fr.mydomain.com', 'http://pl.mydomain.com', 'https://ua.mydomain.com'] | ||
|
||
export default defineNuxtConfig({ | ||
// ... | ||
i18n: { | ||
locales: [ | ||
{ | ||
code: 'en', | ||
domains: i18nDomains, | ||
defaultForDomains: ['mydomain.com', 'en.mydomain.com'] | ||
}, | ||
{ | ||
code: 'es', | ||
domains: i18nDomains, | ||
defaultForDomains: ['es.mydomain.com'] | ||
}, | ||
{ | ||
code: 'fr', | ||
domains: i18nDomains, | ||
defaultForDomains: ['fr.mydomain.com'] | ||
}, | ||
{ | ||
code: 'pl', | ||
domains: i18nDomains, | ||
defaultForDomains: ['http://pl.mydomain.com'] | ||
}, | ||
{ | ||
code: 'ua', | ||
domains: i18nDomains, | ||
defaultForDomains: ['https://ua.mydomain.com'] | ||
}, | ||
{ | ||
code: 'nl', | ||
domains: i18nDomains | ||
}, | ||
{ | ||
code: 'de', | ||
domains: i18nDomains | ||
}, | ||
], | ||
strategy: 'prefix', | ||
multiDomainLocales: true | ||
}, | ||
// ... | ||
}) | ||
``` | ||
|
||
Given above configuration with the `prefix` strategy, following requests will be: | ||
- https://mydomain.com -> https://mydomain.com/en (en language) | ||
- https://mydomain.com/pl -> https://mydomain.com/pl (pl language) | ||
- https://mydomain.com/ua -> https://mydomain.com/ua (ua language) | ||
- https://mydomain.com/nl -> https://mydomain.com/nl (nl language) | ||
- https://en.mydomain.com -> https://en.mydomain.com/en (en language) | ||
- https://es.mydomain.com -> https://es.mydomain.com/es (es language) | ||
- https://fr.mydomain.com -> https://fr.mydomain.com/fr (fr language) | ||
- https://fr.mydomain.com/de -> https://fr.mydomain.com/de (de language) | ||
|
||
The same requests when using the `prefix_except_default` strategy, will be: | ||
- https://mydomain.com -> https://mydomain.com (en language) | ||
- https://mydomain.com/pl -> https://mydomain.com/pl (pl language) | ||
- https://mydomain.com/ua -> https://mydomain.com/ua (ua language) | ||
- https://mydomain.com/nl -> https://mydomain.com/nl (nl language) | ||
- https://en.mydomain.com -> https://en.mydomain.com (en language) | ||
- https://es.mydomain.com -> https://es.mydomain.com (es language) | ||
- https://fr.mydomain.com -> https://fr.mydomain.com (fr language) | ||
- https://fr.mydomain.com/de -> https://fr.mydomain.com/de (de language) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
170 changes: 170 additions & 0 deletions
170
docs/content/docs/5.v9/2.guide/10.multi-domain-locales.md
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,170 @@ | ||
--- | ||
title: Multi domain locales | ||
description: Set up multiple domains for multiple locales. Use a different domain name for each language your app supports. | ||
--- | ||
|
||
How to set up multi domain locales: | ||
|
||
- Set the `multiDomainLocales` option to `true` | ||
- Configure the `locales` option as an array of objects: | ||
- Each object has a `domains` key whose value is a array of the domains you'd like to use for that locale. Optionally include a port (if non-standard) and/or a protocol. If the protocol is not provided then an attempt will be made to auto-detect it but that might not work correctly in some cases like when the pages are statically generated. | ||
- Optionally set for each object a `defaultForDomains` key whose value is a array of the default domains you'd like to use for that locale. Optionally include a port (if non-standard) and/or a protocol. If the protocol is not provided then an attempt will be made to auto-detect it but that might not work correctly in some cases like when the pages are statically generated. | ||
- Optionally set `detectBrowserLanguage` to `false`. When enabled (which it is by default), user can get redirected to a different domain on first visit. Set to `false` if you want to ensure that visiting given domain always shows page in the corresponding locale. | ||
|
||
```ts [nuxt.config.ts] | ||
const i18nDomains = ['mydomain.com', 'es.mydomain.com', 'fr.mydomain.com', 'http://pl.mydomain.com', 'https://ua.mydomain.com'] | ||
|
||
export default defineNuxtConfig({ | ||
i18n: { | ||
locales: [ | ||
{ | ||
code: 'en', | ||
domains: i18nDomains, | ||
defaultForDomains: ['mydomain.com'] | ||
}, | ||
{ | ||
code: 'es', | ||
domains: i18nDomains, | ||
defaultForDomains: ['es.mydomain.com'] | ||
}, | ||
{ | ||
code: 'fr', | ||
domains: i18nDomains, | ||
defaultForDomains: ['fr.mydomain.com'] | ||
}, | ||
{ | ||
code: 'pl', | ||
domains: i18nDomains, | ||
defaultForDomains: ['http://pl.mydomain.com'] | ||
}, | ||
{ | ||
code: 'ua', | ||
domains: i18nDomains, | ||
defaultForDomains: ['https://ua.mydomain.com'] | ||
}, | ||
{ | ||
code: 'nl', | ||
domains: i18nDomains | ||
}, | ||
{ | ||
code: 'de', | ||
domains: i18nDomains | ||
}, | ||
], | ||
multiDomainLocales: true | ||
} | ||
}) | ||
``` | ||
|
||
## Runtime environment variables | ||
|
||
Sometimes there's a need to change domains in different environments, e.g. staging and production. | ||
As `nuxt.config.ts` is used at build time it would be necessary to create different builds for different environments. | ||
|
||
```ts [locale-domains.config.ts] | ||
export const localeDomains = { | ||
uk: process.env.DOMAIN_UK, | ||
fr: process.env.DOMAIN_FR | ||
} | ||
``` | ||
|
||
```ts [nuxt.config.ts] | ||
import { localeDomains } from './locale-domains.config' | ||
|
||
const i18nDomains = [localeDomains.uk, localeDomains.fr] | ||
|
||
export default defineNuxtConfig({ | ||
modules: ['@nuxtjs/i18n'], | ||
|
||
i18n: { | ||
multiDomainLocales: true, | ||
locales: [ | ||
{ | ||
code: 'uk', | ||
domains: i18nDomains, | ||
defaultForDomains: [localeDomains.uk] | ||
}, | ||
{ | ||
code: 'fr', | ||
domains: i18nDomains, | ||
defaultForDomains: [localeDomains.fr] | ||
} | ||
] | ||
} | ||
}) | ||
``` | ||
|
||
With the above config, a build would have to be run for staging and production with different .env files that specify `DOMAIN_UK` and `DOMAIN_FR`. | ||
|
||
## Using different domains for only some of the languages | ||
|
||
If one or more of the domains need to host multiple languages, the default language of each domain needs to have `domainDefault: true` so there is a per domain fallback locale. | ||
The option `differentDomains` still need to be set to `true` though. | ||
|
||
```js {}[nuxt.config.js] | ||
const i18nDomains = ['mydomain.com', 'en.mydomain.com', 'es.mydomain.com', 'fr.mydomain.com', 'http://pl.mydomain.com', 'https://ua.mydomain.com'] | ||
|
||
export default defineNuxtConfig({ | ||
// ... | ||
i18n: { | ||
locales: [ | ||
{ | ||
code: 'en', | ||
domains: i18nDomains, | ||
defaultForDomains: ['mydomain.com', 'en.mydomain.com'] | ||
}, | ||
{ | ||
code: 'es', | ||
domains: i18nDomains, | ||
defaultForDomains: ['es.mydomain.com'] | ||
}, | ||
{ | ||
code: 'fr', | ||
domains: i18nDomains, | ||
defaultForDomains: ['fr.mydomain.com'] | ||
}, | ||
{ | ||
code: 'pl', | ||
domains: i18nDomains, | ||
defaultForDomains: ['http://pl.mydomain.com'] | ||
}, | ||
{ | ||
code: 'ua', | ||
domains: i18nDomains, | ||
defaultForDomains: ['https://ua.mydomain.com'] | ||
}, | ||
{ | ||
code: 'nl', | ||
domains: i18nDomains | ||
}, | ||
{ | ||
code: 'de', | ||
domains: i18nDomains | ||
}, | ||
], | ||
strategy: 'prefix', | ||
multiDomainLocales: true | ||
}, | ||
// ... | ||
}) | ||
``` | ||
|
||
Given above configuration with the `prefix` strategy, following requests will be: | ||
- https://mydomain.com -> https://mydomain.com/en (en language) | ||
- https://mydomain.com/pl -> https://mydomain.com/pl (pl language) | ||
- https://mydomain.com/ua -> https://mydomain.com/ua (ua language) | ||
- https://mydomain.com/nl -> https://mydomain.com/nl (nl language) | ||
- https://en.mydomain.com -> https://en.mydomain.com/en (en language) | ||
- https://es.mydomain.com -> https://es.mydomain.com/es (es language) | ||
- https://fr.mydomain.com -> https://fr.mydomain.com/fr (fr language) | ||
- https://fr.mydomain.com/de -> https://fr.mydomain.com/de (de language) | ||
|
||
The same requests when using the `prefix_except_default` strategy, will be: | ||
- https://mydomain.com -> https://mydomain.com (en language) | ||
- https://mydomain.com/pl -> https://mydomain.com/pl (pl language) | ||
- https://mydomain.com/ua -> https://mydomain.com/ua (ua language) | ||
- https://mydomain.com/nl -> https://mydomain.com/nl (nl language) | ||
- https://en.mydomain.com -> https://en.mydomain.com (en language) | ||
- https://es.mydomain.com -> https://es.mydomain.com (es language) | ||
- https://fr.mydomain.com -> https://fr.mydomain.com (fr language) | ||
- https://fr.mydomain.com/de -> https://fr.mydomain.com/de (de language) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.