Skip to content

Commit 1fd9ad8

Browse files
authored
refactor(nuxt-module): added autoChangeCookie option to nuxt config (vuestorefront#6595)
* refactor(nuxt-module): added `autoChangeCookie` option to nuxt config This enables the possibility to control how the i18n plugin handles the cookies, so integrators can also control more finite the cookies without the plugin handling for then. * docs: added PR changelog file
1 parent cf78686 commit 1fd9ad8

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
description: 'Add the `autoChangeCookie` option on nuxt-i18n configuration to handle fine control on cookie',
3+
link: '',
4+
isBreaking: false,
5+
breakingChanges: [],
6+
author: 'Heitor Ramon',
7+
linkToGitHubAccount: 'https://github.com/bloodf'
8+
};

packages/core/docs/getting-started/internationalization.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,44 @@ You can provide your own i18n configuration just for a specific module by settin
8484
},
8585
];
8686
```
87+
88+
## Configuring the Auto Cookie Change
89+
You can control how the `@vue-storefront/nuxt` module will handle the cookies under the hood. This configuration will allow you to manage the fine control on the cookie changes after each locale changes.
90+
91+
The `autoChangeCookie` object holds three new properties, that are direct linked to `currency`, `locale`, and `country`. Those properties control how the module will handle the cookie changes. If set to `false`, the module won't change automatically any cookie based on configurations or browser locale, so the `{INTEGARTION}` will handle that.
92+
93+
```js
94+
// nuxt.config.js
95+
modules: [
96+
['@vue-storefront/{INTEGRATION}/nuxt', {
97+
api: {
98+
// api client configuration
99+
},
100+
i18n: {
101+
useNuxtI18nModule: true
102+
}
103+
}]
104+
],
105+
i18n: {
106+
locales: [
107+
{
108+
code: 'en',
109+
label: 'English',
110+
file: 'en.js',
111+
iso: 'en'
112+
},
113+
{
114+
code: 'de',
115+
label: 'German',
116+
file: 'de.js',
117+
iso: 'de'
118+
}
119+
],
120+
defaultLocale: 'en',
121+
autoChangeCookie: {
122+
currency: false,
123+
locale: false,
124+
country: false,
125+
},
126+
}
127+
```

packages/core/nuxt-module/lib/plugins/i18n-cookies.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const i18nCookiesPlugin = ({ $cookies, i18n, app, redirect }) => {
1313
}
1414
const cookieLocale = $cookies.get(cookieNames.locale);
1515
const cookieCurrency = $cookies.get(cookieNames.currency);
16+
const cookieCountry = $cookies.get(cookieNames.country);
17+
const autoChangeCookie = {
18+
locale: true,
19+
currency: true,
20+
country: true,
21+
...i18nOptions.autoChangeCookie,
22+
};
1623

1724
const getCurrencyByLocale = (locale) =>
1825
i18n.numberFormats?.[locale]?.currency?.currency
@@ -70,19 +77,27 @@ const i18nCookiesPlugin = ({ $cookies, i18n, app, redirect }) => {
7077
throw new Error(`Following fields are missing in the i18n configuration: ${missingFields.join(', ')}`);
7178
}
7279

73-
if (cookieLocale !== settings.locale) {
80+
if ((cookieLocale !== settings.locale && autoChangeCookie.locale) || !cookieLocale) {
7481
$cookies.set(cookieNames.locale, settings.locale, cookieOptions);
7582
}
7683

77-
if (cookieCurrency !== settings.currency) {
84+
if ((cookieCurrency !== settings.currency && autoChangeCookie.currency) || !cookieCurrency) {
7885
$cookies.set(cookieNames.currency, settings.currency, cookieOptions);
7986
}
8087

81-
!$cookies.get(cookieNames.country) && $cookies.set(cookieNames.country, settings.country, cookieOptions);
88+
if ((cookieCountry !== settings.country && autoChangeCookie.country) || !cookieCountry) {
89+
$cookies.set(cookieNames.country, settings.country, cookieOptions);
90+
}
8291

8392
i18n.onBeforeLanguageSwitch = (oldLocale, newLocale, isInitialSetup, context) => {
84-
$cookies.set(cookieNames.locale, newLocale, cookieOptions);
85-
$cookies.set(cookieNames.currency, getCurrencyByLocale(newLocale), cookieOptions);
93+
if (autoChangeCookie.locale) {
94+
$cookies.set(cookieNames.locale, newLocale, cookieOptions);
95+
}
96+
97+
if (autoChangeCookie.currency) {
98+
$cookies.set(cookieNames.currency, getCurrencyByLocale(newLocale), cookieOptions);
99+
}
100+
86101
window.location.href = context.route.fullPath;
87102
}
88103
}

0 commit comments

Comments
 (0)