This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
60 lines (55 loc) · 1.81 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<template>
<transition name="fade" mode="out-in">
<div :key="locale" v-if="!isIncorrectLocale">
<NuxtPage/>
</div>
</transition>
</template>
<script setup>
const { $router } = useNuxtApp()
const currentRoute = computed(() => $router.currentRoute.value)
const { locale } = useI18n()
const isIncorrectLocale = computed(() => !['ru', 'en'].includes(locale.value))
/** Set language */
watch(
() => currentRoute.value.params.lang,
() => (locale.value = currentRoute.value.params.lang),
{ immediate: true }
)
/** Need to detect language */
watch(
() => currentRoute.value.matched,
() => {
const userLanguage = process.client ? String(navigator?.language || navigator?.userLanguage || 'en').toLowerCase() : 'en'
if (currentRoute.value.matched.length === 0 || !['ru', 'en'].includes(currentRoute.value.params.lang)) {
const redurectLanguage = userLanguage.includes('ru') || userLanguage.includes('ua') ? 'ru' : 'en'
const routeName = currentRoute.value.name
if (currentRoute.value.matched.length === 0 || currentRoute.value.path === '/' || routeName === 'index') {
$router.replace(`/${redurectLanguage}`)
} else if (routeName) {
$router.replace({
name: routeName,
params: {
...currentRoute.value.params,
lang: redurectLanguage
}
})
}
}
},
{ immediate: true }
)
/** Windows system detection */
useMeta({
htmlAttrs: {
lang: locale
},
script: [
{
/** Windows system detection */
type: 'application/javascript',
src: `data:application/javascript,${encodeURIComponent(`String(navigator.userAgentData ? navigator.userAgentData.platform : navigator.userAgent).toLowerCase().includes('windows') ? document.body.classList.add('root--hide-flags') : null;`)}`
}
]
})
</script>