2023-01-12 17:52:52 +00:00
|
|
|
import type { VueI18n } from 'vue-i18n'
|
|
|
|
import type { LocaleObject } from 'vue-i18n-routing'
|
|
|
|
import { COOKIE_KEY_LOCALE, COOKIE_MAX_AGE, DEFAULT_LANGUAGE } from '~/constants'
|
2022-12-28 03:50:29 +00:00
|
|
|
|
|
|
|
export default defineNuxtPlugin(async (nuxt) => {
|
2023-01-12 17:52:52 +00:00
|
|
|
const i18n = nuxt.vueApp.config.globalProperties.$i18n as VueI18n
|
|
|
|
const { setLocale, locales } = i18n
|
|
|
|
const supportLanguages = (locales as LocaleObject[]).map(locale => locale.code)
|
2023-01-06 17:39:37 +00:00
|
|
|
const cookieLocale = useCookie(COOKIE_KEY_LOCALE, { maxAge: COOKIE_MAX_AGE })
|
2023-01-12 17:52:52 +00:00
|
|
|
const userSettings = useUserSettings()
|
2022-12-28 03:50:29 +00:00
|
|
|
|
2023-01-12 17:52:52 +00:00
|
|
|
if (process.server) {
|
|
|
|
const headers = useRequestHeaders()
|
|
|
|
|
|
|
|
let lang = cookieLocale.value
|
|
|
|
if (!lang || !supportLanguages.includes(lang)) {
|
|
|
|
// first visit
|
|
|
|
if (headers['accept-language']) {
|
|
|
|
// detect language from header
|
|
|
|
const userLanguages = headers['accept-language'].split(',').map(lang => lang.split(';')[0].toLowerCase())
|
|
|
|
lang = matchLanguages(supportLanguages, userLanguages) || DEFAULT_LANGUAGE
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lang = DEFAULT_LANGUAGE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userSettings.value.language = cookieLocale.value = lang
|
2022-12-28 03:50:29 +00:00
|
|
|
|
2023-01-12 17:52:52 +00:00
|
|
|
if (lang !== i18n.locale)
|
|
|
|
await setLocale(cookieLocale.value)
|
2022-12-28 03:50:29 +00:00
|
|
|
|
2023-01-12 17:52:52 +00:00
|
|
|
return
|
2022-12-28 03:50:29 +00:00
|
|
|
}
|
2023-01-12 17:52:52 +00:00
|
|
|
|
|
|
|
// could be null if browser don't accept cookie
|
|
|
|
if (!cookieLocale.value || !supportLanguages.includes(cookieLocale.value))
|
|
|
|
cookieLocale.value = DEFAULT_LANGUAGE
|
|
|
|
userSettings.value.language = cookieLocale.value
|
|
|
|
|
|
|
|
watch(() => userSettings.value.language, (lang) => {
|
|
|
|
if (lang !== cookieLocale.value)
|
|
|
|
cookieLocale.value = lang
|
|
|
|
if (lang !== i18n.locale)
|
|
|
|
setLocale(lang)
|
|
|
|
}, { immediate: true })
|
2022-12-28 03:50:29 +00:00
|
|
|
})
|