forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-i18n.ts
More file actions
32 lines (26 loc) · 1.06 KB
/
setup-i18n.ts
File metadata and controls
32 lines (26 loc) · 1.06 KB
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
import type { Locale } from '#i18n'
export default defineNuxtPlugin(async nuxt => {
const t = nuxt.vueApp.config.globalProperties.$t
const d = nuxt.vueApp.config.globalProperties.$d
const n = nuxt.vueApp.config.globalProperties.$n
nuxt.vueApp.config.globalProperties.$t = wrapI18n(t)
nuxt.vueApp.config.globalProperties.$d = wrapI18n(d)
nuxt.vueApp.config.globalProperties.$n = wrapI18n(n)
if (import.meta.client) {
const i18n = useNuxtApp().$i18n
const { setLocale, locales } = i18n
const { settings } = useSettings()
const lang = computed(() => settings.value.language as Locale)
const supportLanguages = unref(locales).map(locale => locale.code)
if (!supportLanguages.includes(lang.value))
settings.value.language = getDefaultLanguage(supportLanguages)
if (lang.value !== i18n.locale.value) await setLocale(settings.value.language as Locale)
watch(
[lang, isHydrated],
() => {
if (isHydrated.value && lang.value !== i18n.locale.value) setLocale(lang.value)
},
{ immediate: true },
)
}
})