refactor: setup

This commit is contained in:
Anthony Fu 2022-12-28 04:50:29 +01:00
parent 970b6538e2
commit 3079867e2a
7 changed files with 38 additions and 42 deletions

View file

@ -1,7 +1,7 @@
import { pwaInfo } from 'virtual:pwa-info'
import type { Link } from '@unhead/schema'
import type { Directions } from 'vue-i18n-routing'
import { APP_NAME, COOKIE_MAX_AGE, STORAGE_KEY_LANG } from '~/constants'
import { APP_NAME } from '~/constants'
import type { LocaleObject } from '#i18n'
export function setupPageHeader() {
@ -46,37 +46,3 @@ export function setupPageHeader() {
link,
})
}
export async function setupI18n() {
const { locale, setLocale, locales } = useI18n()
const cookieLocale = useCookie(STORAGE_KEY_LANG, { maxAge: COOKIE_MAX_AGE })
const isFirstVisit = cookieLocale.value == null
if (process.client && isFirstVisit) {
const userLang = (navigator.language || 'en-US').toLowerCase()
// cause vue-i18n not explicit export LocaleObject type
const supportLocales = unref(locales) as { code: string }[]
const lang = supportLocales.find(locale => userLang.startsWith(locale.code.toLowerCase()))?.code
|| supportLocales.find(locale => userLang.startsWith(locale.code.split('-')[0]))?.code
cookieLocale.value = lang || 'en-US'
}
if (cookieLocale.value && cookieLocale.value !== locale.value)
await setLocale(cookieLocale.value)
if (process.client) {
watchEffect(() => {
cookieLocale.value = locale.value
})
}
}
export async function setupEmojis() {
if (process.client) {
const promise = import('@emoji-mart/data').then(r => r.default)
const { init } = await import('emoji-mart')
init({
data: () => promise,
})
}
}