Skip to content

Commit 4819ce7

Browse files
author
Marcin Kwiatkowski
committed
refactor: refactored i18n plugins (#794)
* M2-295. Refactored i18n plugins * M2-300. Removed @vue-storefront/core dependency
1 parent d6f2f18 commit 4819ce7

File tree

10 files changed

+19
-141
lines changed

10 files changed

+19
-141
lines changed

.github/workflows/deploy-vue-storefront-cloud.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ jobs:
5353
uses: ./.github/workflows/deployment-template.yml
5454
needs: build
5555
if: github.ref == 'refs/heads/main'
56-
with:
56+
with:
5757
github_environment_name: production
5858
environment_code: demo-magento2
5959
target_url: https://demo-magento2.europe-west1.gcp.storefrontcloud.io
6060
secrets:
6161
cloud_username: ${{ secrets.CLOUD_USERNAME }}
6262
cloud_password: ${{ secrets.CLOUD_PASSWORD }}
63-
63+
6464
deploy-develop:
6565
uses: ./.github/workflows/deployment-template.yml
6666
needs: build
6767
if: github.ref == 'refs/heads/develop'
68-
with:
68+
with:
6969
github_environment_name: dev
7070
environment_code: demo-magento2-dev
7171
target_url: https://demo-magento2-dev.europe-west1.gcp.storefrontcloud.io
@@ -77,7 +77,7 @@ jobs:
7777
uses: ./.github/workflows/deployment-template.yml
7878
needs: build
7979
if: startsWith(github.ref, 'refs/heads/release')
80-
with:
80+
with:
8181
github_environment_name: canary
8282
environment_code: demo-magento2-canary
8383
target_url: https://demo-magento2-canary.europe-west1.gcp.storefrontcloud.io
@@ -89,7 +89,7 @@ jobs:
8989
uses: ./.github/workflows/deployment-template.yml
9090
needs: build
9191
if: github.ref == 'refs/heads/enterprise'
92-
with:
92+
with:
9393
github_environment_name: enterprise
9494
environment_code: demo-magento2-enterprise
9595
target_url: https://demo-magento2-enterprise.europe-west1.gcp.storefrontcloud.io

packages/theme/components/CurrencySelector/CurrenciesModal.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@
1414
<a
1515
href="/"
1616
:class="selectedCurrency === currency ? 'container__currency--selected-label' : ''"
17-
@click.prevent="handleChanges({
18-
callback: () => changeCurrency({id: currency}),
19-
redirect: false,
20-
refresh: true
21-
})"
17+
@click.prevent="changeCurrency({id: currency})"
2218
>
2319
<SfCharacteristic class="currency">
2420
<template #title>
@@ -42,7 +38,6 @@ import {
4238
import {
4339
useCurrency,
4440
} from '~/composables';
45-
import { useHandleChanges } from '~/helpers/magentoConfig/handleChanges';
4641
4742
export default defineComponent({
4843
name: 'CurrenciesModal',
@@ -63,8 +58,6 @@ export default defineComponent({
6358
load: loadCurrencies,
6459
} = useCurrency();
6560
66-
const { handleChanges } = useHandleChanges();
67-
6861
const availableCurrencies = computed(() => currencies.value?.available_currency_codes || []);
6962
7063
onMounted(() => {
@@ -74,7 +67,6 @@ export default defineComponent({
7467
7568
return {
7669
changeCurrency,
77-
handleChanges,
7870
availableCurrencies,
7971
};
8072
},

packages/theme/components/StoreSwitcher/StoresModal.vue

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@
1717
? 'container__store--selected'
1818
: ''
1919
"
20-
@click="
21-
handleChanges({
22-
callback: () => changeStore(store),
23-
redirect: false,
24-
windowRefresh: true,
25-
})
26-
"
20+
@click.prevent="changeStore(store)"
2721
>
2822
<SfCharacteristic class="language">
2923
<template #title>
@@ -57,7 +51,6 @@ import {
5751
} from '@storefront-ui/vue';
5852
import { useStore } from '~/composables';
5953
import { storeGetters, storeConfigGetters } from '~/getters';
60-
import { useHandleChanges } from '~/helpers/magentoConfig/handleChanges';
6154
6255
export default defineComponent({
6356
name: 'StoresModal',
@@ -73,8 +66,6 @@ export default defineComponent({
7366
},
7467
emits: ['closeModal'],
7568
setup() {
76-
const { handleChanges } = useHandleChanges();
77-
7869
const {
7970
stores,
8071
change: changeStore,
@@ -91,7 +82,6 @@ export default defineComponent({
9182
return {
9283
storeGetters,
9384
storeConfigGetters,
94-
handleChanges,
9585
availableStores,
9686
changeStore,
9787
};

packages/theme/composables/useCurrency/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const useCurrency = (): UseCurrency => {
4040

4141
try {
4242
app.$vsf.$magento.config.state.setCurrency(params.id);
43+
window.location.reload();
4344
} catch (err) {
4445
Logger.debug('[ERROR] useCurrency/change', err);
4546
error.value.change = err;

packages/theme/composables/useStore/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ const useStore: UseStore = (): UseStoreInterface => {
4343
app.$vsf.$magento.config.state.setStore(storeConfigGetters.getCode(store));
4444
app.$vsf.$magento.config.state.setCurrency(storeConfigGetters.getCurrency(store));
4545
app.$vsf.$magento.config.state.setLocale(storeConfigGetters.getCode(store));
46+
const newStoreUrl = app.switchLocalePath(storeConfigGetters.getCode(store));
47+
window.location.replace(newStoreUrl);
4648
} catch (err) {
4749
error.value.change = err;
4850
}

packages/theme/helpers/magentoConfig/handleChanges.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

packages/theme/modules/magento/index.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
11
/* eslint-disable unicorn/prefer-module */
22
const path = require('path');
33

4-
const mapI18nSettings = (i18n) => ({
5-
locales: i18n.locales.map(({ label, code }) => ({ name: code, label })),
6-
});
7-
8-
const isNuxtI18nUsed = (moduleOptions) => moduleOptions.i18n && moduleOptions.i18n.useNuxtI18nConfig;
9-
10-
const getMissingFields = (options) => [
11-
'locales',
12-
].filter((o) => options[o] === undefined);
13-
144
// eslint-disable-next-line func-names
15-
export default function (moduleOptions) {
16-
const options = isNuxtI18nUsed(moduleOptions)
17-
? {
18-
...moduleOptions,
19-
...mapI18nSettings(this.options.i18n),
20-
}
21-
: moduleOptions;
22-
23-
const missingFields = getMissingFields(options);
24-
25-
if (missingFields.length > 0) {
26-
throw new Error(`Please provide missing i18n fields: (${missingFields.join(', ')})`);
27-
}
28-
5+
export default function (options) {
296
this.extendBuild((config) => {
307
// eslint-disable-next-line no-param-reassign
318
config.resolve.alias['@vue-storefront/magento-api$'] = require.resolve('@vue-storefront/magento-api');

packages/theme/nuxt.config.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,21 @@ export default () => {
6767
'@nuxtjs/style-resources',
6868
'@nuxtjs/device',
6969
['@vue-storefront/nuxt', {
70-
// @core-development-only-start
71-
coreDevelopment: true,
72-
// @core-development-only-end
73-
useRawSource: {
74-
dev: [
75-
'@vue-storefront/core',
76-
],
77-
prod: [
78-
'@vue-storefront/core',
79-
],
80-
},
8170
// selectively disabling certain @vue-storefront/core plugins for migration
8271
context: false,
8372
logger: false,
8473
ssr: false,
8574
sfui: false,
75+
i18nExtension: false,
76+
e2e: true,
77+
performance: {
78+
httpPush: false,
79+
purgeCSS: {
80+
enabled: false,
81+
},
82+
},
8683
}],
8784
['~/modules/magento', {
88-
i18n: {
89-
useNuxtI18nConfig: true,
90-
},
9185
cookies,
9286
externalCheckout,
9387
defaultStore,
@@ -149,11 +143,6 @@ export default () => {
149143
},
150144
],
151145
defaultLocale: 'default',
152-
autoChangeCookie: {
153-
currency: false,
154-
locale: false,
155-
country: false,
156-
},
157146
lazy: true,
158147
seo: true,
159148
langDir: 'lang/',

packages/theme/plugins/__tests__/i18n.spec.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,5 @@ describe('i18n plugin', () => {
121121
expect(testCaseAppMock.$vsf.$magento.config.state.setLocale).toHaveBeenCalledWith('de_DE');
122122
expect(testCaseAppMock.$vsf.$magento.config.state.setStore).toHaveBeenCalledWith('de_DE');
123123
expect(testCaseAppMock.$vsf.$magento.config.state.setCurrency).toHaveBeenCalledWith('EUR');
124-
expect(callbackRequest.headers.cookie).toMatchInlineSnapshot(
125-
'"vsf-store=de_DE; vsf-locale=de_DE; vsf-currency=EUR; vsf-country=PL; vsf-customer=12fg45; vsf-cart=123 "',
126-
);
127124
});
128125
});

packages/theme/plugins/i18n.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,6 @@ const setDefaultLocale = async (i18n) => {
6060
await i18n.setLocale(i18n.defaultLocale);
6161
};
6262

63-
/**
64-
* Prepare new cookie string based on app state.
65-
*
66-
* @param apiState {ConfigState}
67-
* @param newStoreCode {string}
68-
* @param currency {string}
69-
* @returns {string}
70-
*/
71-
const prepareNewCookieString = (apiState: ConfigState, newStoreCode: string, currency: string) => {
72-
const customerTokenCookie = apiState.getCustomerToken();
73-
const cartIdCookie = apiState.getCartId();
74-
75-
let cookie = `vsf-store=${newStoreCode}; `;
76-
cookie += `vsf-locale=${newStoreCode}; `;
77-
cookie += `vsf-currency=${currency}; `;
78-
cookie += `vsf-country=${apiState.getCountry()}; `;
79-
80-
if (customerTokenCookie) {
81-
cookie += `vsf-customer=${customerTokenCookie}; `;
82-
}
83-
84-
if (cartIdCookie) {
85-
cookie += `vsf-cart=${cartIdCookie} `;
86-
}
87-
88-
return cookie;
89-
};
90-
9163
export default async ({ app, route }: Context) => {
9264
await app.$vsf.$magento.client.interceptors.request.use(async (request) => {
9365
const { i18n } = app;
@@ -110,9 +82,6 @@ export default async ({ app, route }: Context) => {
11082
apiState.setStore(i18nCurrentLocaleCode);
11183
apiState.setLocale(i18nCurrentLocaleCode);
11284
apiState.setCurrency(currency);
113-
114-
// eslint-disable-next-line no-param-reassign
115-
request.headers.cookie = prepareNewCookieString(apiState, i18nCurrentLocaleCode, currency);
11685
}
11786

11887
return request;

0 commit comments

Comments
 (0)