Skip to content

Commit 7525cd7

Browse files
authored
feat: Added cookieDomain option to override locale cookie's domain (#599)
1 parent 3eb9fdb commit 7525cd7

File tree

5 files changed

+12
-1
lines changed

5 files changed

+12
-1
lines changed

docs/es/options-reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Aquí están todas las opciones disponibles al configurar el módulo y sus valor
6262
// preferred language to prevent subsequent redirections
6363
// Set to false to redirect every time
6464
useCookie: true,
65+
// Set to override the default domain of the cookie. Defaults to host of the site.
66+
cookieDomain: null
6567
// Cookie name
6668
cookieKey: 'i18n_redirected',
6769
// Set to always redirect to value stored in the cookie, not just once

docs/options-reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Here are all the options available when configuring the module and their default
6262
// preferred language to prevent subsequent redirections
6363
// Set to false to redirect every time
6464
useCookie: true,
65+
// Set to override the default domain of the cookie. Defaults to host of the site.
66+
cookieDomain: null
6567
// Cookie name
6668
cookieKey: 'i18n_redirected',
6769
// Set to always redirect to value stored in the cookie, not just once

src/helpers/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ exports.DEFAULT_OPTIONS = {
3535
rootRedirect: null,
3636
detectBrowserLanguage: {
3737
useCookie: true,
38+
cookieDomain: null,
3839
cookieKey: 'i18n_redirected',
3940
alwaysRedirect: false,
4041
fallbackLocale: null

src/plugins/main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default async (context) => {
8686
}, { preserveState: !!store.state[vuex.moduleName] })
8787
}
8888

89-
const { useCookie, cookieKey } = detectBrowserLanguage
89+
const { useCookie, cookieKey, cookieDomain } = detectBrowserLanguage
9090

9191
const getLocaleCookie = () => {
9292
if (useCookie) {
@@ -109,6 +109,11 @@ export default async (context) => {
109109
path: '/',
110110
sameSite: 'lax'
111111
}
112+
113+
if (cookieDomain) {
114+
cookieOptions.domain = cookieDomain
115+
}
116+
112117
if (process.client) {
113118
JsCookie.set(cookieKey, locale, cookieOptions)
114119
} else if (res) {

types/nuxt-i18n.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ declare namespace NuxtVueI18n {
2626

2727
interface DetectBrowserLanguageInterface {
2828
useCookie?: boolean
29+
cookieDomain?: string | null
2930
cookieKey?: string
3031
alwaysRedirect?: boolean
3132
fallbackLocale?: Locale | null

0 commit comments

Comments
 (0)