main
vue 52 lines 1.13 KB
Raw
1 <template>
2 <n-popselect v-model:value="currentLocale" :options="list" :render-label>
3 <Icon :size="19" :name="MultiLanguageIcon" />
4 </n-popselect>
5 </template>
6
7 <script lang="ts" setup>
8 import type { SelectOption } from "naive-ui"
9 import type { VNodeChild } from "vue"
10 import { NPopselect } from "naive-ui"
11 import { computed, h } from "vue"
12 import { useI18n } from "vue-i18n"
13 import Icon from "@/components/common/Icon.vue"
14 import { useLocalesStore } from "@/stores/i18n"
15
16 const MultiLanguageIcon = "ion:language-outline"
17 const localesStore = useLocalesStore()
18 const { setLocale } = localesStore
19 const { t } = useI18n()
20
21 const list = computed(() =>
22 localesStore.availableLocales.map(i => ({
23 label: i,
24 value: i
25 }))
26 )
27
28 const currentLocale = computed({
29 get: () => localesStore.locale,
30 set: v => setLocale(v)
31 })
32
33 function renderLabel(option: SelectOption): VNodeChild {
34 return [
35 h(Icon, {
36 color: "#000",
37 style: {
38 verticalAlign: "-0.15em",
39 marginRight: "8px"
40 },
41 name: `circle-flags:${option.label}`
42 }),
43 h(
44 "span",
45 {},
46 {
47 default: () => t(`locales.${option.label}`, `${option.label}`)
48 }
49 )
50 ]
51 }
52 </script>