Skip to content

Commit

Permalink
feat: replace locale cookie value with en when getting en-gb
Browse files Browse the repository at this point in the history
  • Loading branch information
er-lim authored May 26, 2023
1 parent 6a71155 commit 169ab40
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
16 changes: 15 additions & 1 deletion plugins/locale-observer.js
Original file line number Diff line number Diff line change
@@ -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) {}
}
25 changes: 25 additions & 0 deletions tests/plugins/locale-observer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 169ab40

Please sign in to comment.