update language select dropdown
This commit is contained in:
parent
c5ccc6137d
commit
caa78bda51
@ -1,12 +1,14 @@
|
|||||||
import { createI18n } from 'vue-i18n'
|
import { createI18n } from 'vue-i18n'
|
||||||
|
import { sStorage } from '@/utils'
|
||||||
|
|
||||||
import messages from './messages'
|
import messages from './messages'
|
||||||
|
|
||||||
|
const currentLocale = sStorage.get('locale')
|
||||||
|
|
||||||
const i18n = createI18n({
|
const i18n = createI18n({
|
||||||
legacy: false,
|
legacy: false,
|
||||||
globalInjection: true,
|
globalInjection: true,
|
||||||
locale: 'en',
|
locale: currentLocale || 'en',
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
messages: messages
|
messages: messages
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<n-dropdown :options="options" @select="handleChangeLocale" v-model="locale">
|
<n-dropdown :options="options" @select="handleChangeLocale">
|
||||||
<n-icon mr-20 size="18" style="cursor: pointer">
|
<n-icon mr-20 size="18" style="cursor: pointer">
|
||||||
<icon-mdi:globe/>
|
<icon-mdi:globe/>
|
||||||
</n-icon>
|
</n-icon>
|
||||||
@ -8,18 +8,23 @@
|
|||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import {useI18n} from 'vue-i18n'
|
import {useI18n} from 'vue-i18n'
|
||||||
|
import {useAppStore} from "@/store";
|
||||||
|
|
||||||
const {availableLocales, locale,} = useI18n()
|
const store = useAppStore()
|
||||||
|
const { availableLocales, t } = useI18n()
|
||||||
|
|
||||||
const options = []
|
const options = computed(() => {
|
||||||
availableLocales.forEach(locale => {
|
let select = []
|
||||||
options.push({
|
availableLocales.forEach(locale => {
|
||||||
label: locale,
|
select.push({
|
||||||
|
label: t('lang', 1, {'locale': locale}),
|
||||||
key: locale
|
key: locale
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
return select
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleChangeLocale = (value) => {
|
const handleChangeLocale = (value) => {
|
||||||
locale.value = value
|
store.setLocale(value)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@ -1,5 +1,10 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { useDark } from '@vueuse/core'
|
import { useDark } from '@vueuse/core'
|
||||||
|
import { sStorage } from '@/utils'
|
||||||
|
import i18n from '~/i18n'
|
||||||
|
|
||||||
|
const currentLocale = sStorage.get('locale')
|
||||||
|
const {locale} = i18n.global
|
||||||
|
|
||||||
const isDark = useDark()
|
const isDark = useDark()
|
||||||
export const useAppStore = defineStore('app', {
|
export const useAppStore = defineStore('app', {
|
||||||
@ -11,7 +16,7 @@ export const useAppStore = defineStore('app', {
|
|||||||
/** keepAlive路由的key,重新赋值可重置keepAlive */
|
/** keepAlive路由的key,重新赋值可重置keepAlive */
|
||||||
aliveKeys: {},
|
aliveKeys: {},
|
||||||
isDark,
|
isDark,
|
||||||
locale: 'en'
|
locale: currentLocale || 'en'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
@ -46,8 +51,10 @@ export const useAppStore = defineStore('app', {
|
|||||||
toggleDark() {
|
toggleDark() {
|
||||||
this.isDark = !this.isDark
|
this.isDark = !this.isDark
|
||||||
},
|
},
|
||||||
setLocale(locale) {
|
setLocale(newLocale) {
|
||||||
this.locale = locale
|
this.locale = newLocale
|
||||||
|
locale.value = newLocale
|
||||||
|
sStorage.set('locale', newLocale)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user