diff --git a/plugins/locale-observer.js b/plugins/locale-observer.js index 308f0f812..a48a5fe9d 100644 --- a/plugins/locale-observer.js +++ b/plugins/locale-observer.js @@ -1,13 +1,27 @@ -import { setCookie } from '~/services/cookie' +import { getCookie, setCookie } from '~/services/cookie' export default function (context) { const { app, isDev } = context app.i18n.onBeforeLanguageSwitch = (oldLocale, newLocale) => { _setLocaleCookie(newLocale, isDev) } + + _handleLocaleCookie(isDev) } function _setLocaleCookie(locale, isDev) { const canonicalName = Intl.getCanonicalLocales(locale)?.[0] setCookie('locale', canonicalName, isDev) } + +function _handleLocaleCookie(isDev) { + const localeCookie = getCookie('locale') + + if (!localeCookie) return + + try { + if (Intl.getCanonicalLocales(localeCookie)[0] === 'en-GB') { + setCookie('locale', 'en', isDev) + } + } catch (error) {} +} diff --git a/tests/plugins/locale-observer.test.js b/tests/plugins/locale-observer.test.js index 136effe68..a39639467 100644 --- a/tests/plugins/locale-observer.test.js +++ b/tests/plugins/locale-observer.test.js @@ -70,6 +70,31 @@ describe('Plugins | locale-observer', () => { ) }) }) + + describe('when user already has an "en-GB" locale cookie value', () => { + it('updates locale cookie value with "en"', () => { + // given + document.cookie = + 'locale=en-GB; path=/; max-age=31536000; SameSite=Strict' + const context = { + isDev: true, + app: { + i18n: {}, + }, + route: { + path: '/', + }, + } + + // when + localeObserver(context) + + // then + expect(document.cookie).toEqual( + 'locale=en; path=/; max-age=31536000; SameSite=Strict' + ) + }) + }) }) describe('when in production mode', () => {