Skip to content

Commit f35e5bc

Browse files
author
Marcin Kwiatkowski
committed
refactor(i18n): changed i18n middleware to plugin and fix store switc… (#425)
* refactor(i18n): changed i18n middleware to plugin and fix store switcher by url issues * refactor: cR fixes for i18n
1 parent a5b77ad commit f35e5bc

File tree

4 files changed

+22
-150
lines changed

4 files changed

+22
-150
lines changed
-224 KB
Binary file not shown.

docs/middlewares/index.md

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

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

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

packages/theme/middleware/i18n.js renamed to packages/theme/plugins/i18n.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,32 @@ const prepareNewCookieString = (apiState, newStoreCode) => {
5353
return cookie;
5454
};
5555

56-
export default async ({ app }) => {
57-
const { i18n } = app;
58-
const currentStoreCode = readStoreCookie(app);
56+
export default ({ app }) => {
57+
app.$vsf.$magento.client.interceptors.request.use(async (request) => {
5958

60-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
61-
if (!currentStoreCode || !findLocaleBasedOnStoreCode(currentStoreCode, i18n.locales)) {
62-
await setDefaultLocale(i18n);
59+
const { i18n } = app;
60+
const currentStoreCode = readStoreCookie(app);
6361

64-
return;
65-
}
62+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
63+
if (!currentStoreCode || !findLocaleBasedOnStoreCode(currentStoreCode, i18n.locales)) {
64+
await setDefaultLocale(i18n);
6665

67-
const i18nCurrentLocaleCode = i18n.locale;
68-
const localeCookie = app.$cookies.get(cookieNames.localeCookieName);
66+
return;
67+
}
6968

70-
if (i18nCurrentLocaleCode !== localeCookie) {
71-
const apiState = app.$vsf.$magento.config.state;
69+
const i18nCurrentLocaleCode = i18n.locale;
70+
const localeCookie = app.$cookies.get(cookieNames.localeCookieName);
7271

73-
apiState.setStore(i18nCurrentLocaleCode);
74-
apiState.setLocale(i18nCurrentLocaleCode);
72+
if (i18nCurrentLocaleCode !== localeCookie) {
73+
const apiState = app.$vsf.$magento.config.state;
7574

76-
// eslint-disable-next-line no-param-reassign
77-
app.$vsf.$magento.config.axios.headers.cookie = prepareNewCookieString(apiState, i18nCurrentLocaleCode);
78-
}
75+
apiState.setStore(i18nCurrentLocaleCode);
76+
apiState.setLocale(i18nCurrentLocaleCode);
77+
78+
// eslint-disable-next-line no-param-reassign
79+
request.headers.cookie = prepareNewCookieString(apiState, i18nCurrentLocaleCode);
80+
}
81+
82+
return request;
83+
});
7984
};

0 commit comments

Comments
 (0)