2022-12-18 02:29:16 +03:00
|
|
|
import { pwaInfo } from 'virtual:pwa-info'
|
|
|
|
import type { Link } from '@unhead/schema'
|
2022-11-30 03:22:35 +03:00
|
|
|
import { APP_NAME, STORAGE_KEY_LANG } from '~/constants'
|
2022-11-28 12:01:14 +03:00
|
|
|
|
2022-11-30 03:22:35 +03:00
|
|
|
export function setupPageHeader() {
|
2022-12-07 00:09:47 +03:00
|
|
|
const isDev = process.dev
|
|
|
|
const isPreview = useRuntimeConfig().public.env === 'staging'
|
|
|
|
|
2022-11-30 00:13:43 +03:00
|
|
|
const i18n = useI18n()
|
2022-11-30 03:22:35 +03:00
|
|
|
|
2022-12-18 02:29:16 +03:00
|
|
|
const link: Link[] = []
|
|
|
|
|
|
|
|
if (pwaInfo && pwaInfo.webManifest) {
|
|
|
|
const { webManifest } = pwaInfo
|
|
|
|
if (webManifest) {
|
|
|
|
const { href, useCredentials } = webManifest
|
|
|
|
if (useCredentials) {
|
|
|
|
link.push({
|
|
|
|
rel: 'manifest',
|
|
|
|
href,
|
|
|
|
crossorigin: 'use-credentials',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
link.push({
|
|
|
|
rel: 'manifest',
|
|
|
|
href,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 01:49:25 +03:00
|
|
|
useHeadFixed({
|
2022-11-30 00:13:43 +03:00
|
|
|
htmlAttrs: {
|
|
|
|
lang: () => i18n.locale.value,
|
|
|
|
},
|
2022-11-28 12:01:14 +03:00
|
|
|
titleTemplate: title => `${title ? `${title} | ` : ''}${APP_NAME}${isDev ? ' (dev)' : isPreview ? ' (preview)' : ''}`,
|
2022-12-18 02:29:16 +03:00
|
|
|
link,
|
2022-11-28 12:01:14 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-expressions
|
|
|
|
isDark.value
|
|
|
|
}
|
2022-11-30 03:22:35 +03:00
|
|
|
|
|
|
|
export async function setupI18n() {
|
2022-12-02 05:17:41 +03:00
|
|
|
const { locale, setLocale, locales } = useI18n()
|
2022-12-17 19:55:29 +03:00
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
nuxtApp.hook('app:suspense:resolve', async () => {
|
|
|
|
const isFirstVisit = process.server ? false : !window.localStorage.getItem(STORAGE_KEY_LANG)
|
|
|
|
const localeStorage = process.server ? ref('en-US') : useLocalStorage(STORAGE_KEY_LANG, locale.value)
|
|
|
|
|
|
|
|
if (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
|
2022-12-02 05:17:41 +03:00
|
|
|
|| supportLocales.find(locale => userLang.startsWith(locale.code.split('-')[0]))?.code
|
2022-12-17 19:55:29 +03:00
|
|
|
localeStorage.value = lang || 'en-US'
|
|
|
|
}
|
2022-12-02 05:17:41 +03:00
|
|
|
|
2022-12-17 19:55:29 +03:00
|
|
|
if (localeStorage.value !== locale.value)
|
|
|
|
await setLocale(localeStorage.value)
|
2022-11-30 03:22:35 +03:00
|
|
|
|
2022-12-17 19:55:29 +03:00
|
|
|
watchEffect(() => {
|
|
|
|
localeStorage.value = locale.value
|
|
|
|
})
|
2022-11-30 03:22:35 +03:00
|
|
|
})
|
|
|
|
}
|