Skip to content

Commit

Permalink
refactor: use cookie service in project
Browse files Browse the repository at this point in the history
  • Loading branch information
er-lim committed May 25, 2023
1 parent 774f611 commit 293d79c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
15 changes: 2 additions & 13 deletions plugins/locale-observer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from '../config/environment'
import { setCookie } from '~/services/cookie'

export default function (context) {
const { app, isDev } = context
Expand All @@ -9,16 +9,5 @@ export default function (context) {

function _setLocaleCookie(locale, isDev) {
const canonicalName = Intl.getCanonicalLocales(locale)?.[0]

const localeCookieProperties = [
`locale=${canonicalName}`,
'path=/',
'max-age=31536000',
'SameSite=Strict',
]
if (!isDev) {
localeCookieProperties.push(`domain=${config.siteDomain}`)
}

document.cookie = localeCookieProperties.join('; ')
setCookie('locale', canonicalName, isDev)
}
9 changes: 2 additions & 7 deletions services/get-locale-from-cookie.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { getCookie } from '~/services/cookie'
import { localization } from '~/config/localization'

export function getLocaleFromCookie() {
const localeCookie = document.cookie
.split('; ')
.find((item) => item.startsWith('locale'))

if (!localeCookie) return null

try {
const chosenLocale = localeCookie.split('=')[1]
const chosenLocale = getCookie('locale')

if (!chosenLocale) return null

Expand Down

0 comments on commit 293d79c

Please sign in to comment.